Android编程实现调用相册、相机及拍照后直接裁剪的方法
本文实例讲述了Android编程实现调用相册、相机及拍照后直接裁剪的方法。分享给大家供大家参考,具体如下:
package com.cvte.health.phone; import java.io.File; import java.text.SimpleDateFormat; import java.util.Date; import android.app.Activity; import android.content.ContentResolver; import android.content.ContentUris; import android.content.Intent; import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; import com.cvte.health.AccountManager; import com.cvte.health.HealthApplication; import com.cvte.health.R; import com.cvte.health.api.ImageUpdater; import com.cvte.health.database.User; public class ChangePortraitActivity extends Activity { private ImageView mImageView; private Button mButtonCamera; private Button mButtonPhoto; private Button mButtonBack; private Button mButtonSave; private User mUser = null; private File mCurrentPhotoFile; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_change_portrait); HealthApplication.getInstance().addActivity(this); mUser = AccountManager.getInstance().getCurrentUser(); mImageView = (ImageView) this.findViewById(R.id.imageview_preview); mButtonCamera = (Button) this.findViewById(R.id.button_cameraButton); mButtonPhoto = (Button) this.findViewById(R.id.button_photoButton); mButtonBack = (Button) findViewById(R.id.button_back); mButtonSave = (Button) findViewById(R.id.button_save); ImageUpdater.getInstance(this).updateUserPhoto(mUser, mImageView); mButtonCamera.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); mCurrentPhotoFile = new File("mnt/sdcard/DCIM/Camera/", getPhotoFileName()); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mCurrentPhotoFile)); startActivityForResult(intent, Activity.DEFAULT_KEYS_DIALER); /* * Intent intent = new * Intent("android.media.action.IMAGE_CAPTURE"); * intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new * File(Environment .getExternalStorageDirectory(), * "camera.jpg"))); * intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0); * startActivityForResult(intent, 10); */ } }); mButtonPhoto.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.addCategory(Intent.CATEGORY_OPENABLE); intent.setType("image/*"); intent.putExtra("crop", "true"); intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); intent.putExtra("outputX", 300); intent.putExtra("outputY", 300); intent.putExtra("return-data", true); startActivityForResult(intent, 11); } }); mButtonBack.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { finish(); } }); mButtonSave.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(ChangePortraitActivity.this, UserProfileActivity.class); mImageView.setDrawingCacheEnabled(Boolean.TRUE); intent.putExtra("BITMAP", mImageView.getDrawingCache()); // 这里可以放一个bitmap startActivity(intent); finish(); overridePendingTransition(R.anim.activity_in_from_left, R.anim.activity_out_from_right); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == 10 && resultCode == Activity.RESULT_OK) { mImageView.setImageDrawable(Drawable.createFromPath(new File( Environment.getExternalStorageDirectory(), "camera.jpg") .getAbsolutePath())); } else if (requestCode == 11 && resultCode == Activity.RESULT_OK) { Bitmap cameraBitmap = (Bitmap) data.getExtras().get("data"); mImageView.setImageBitmap(cameraBitmap); } else if (requestCode == 1 && resultCode == Activity.RESULT_OK) { Uri imgUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; ContentResolver cr = ChangePortraitActivity.this .getContentResolver(); Uri fileUri = Uri.fromFile(mCurrentPhotoFile); sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, fileUri)); try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } Cursor cursor = cr.query(imgUri, null, MediaStore.Images.Media.DISPLAY_NAME + "='" + mCurrentPhotoFile.getName() + "'", null, null); Uri uri = null; if (cursor != null && cursor.getCount() > 0) { cursor.moveToLast(); long id = cursor.getLong(0); uri = ContentUris.withAppendedId(imgUri, id); } final Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, "image/*"); intent.putExtra("crop", "true"); intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); intent.putExtra("outputX", 300); intent.putExtra("outputY", 300); intent.putExtra("return-data", true); ChangePortraitActivity.this.startActivityForResult(intent, 3); } else if (requestCode == 3 && resultCode == Activity.RESULT_OK) { Bitmap cameraBitmap = (Bitmap) data.getExtras().get("data"); mImageView.setImageBitmap(cameraBitmap); } } private String getPhotoFileName() { Date date = new Date(System.currentTimeMillis()); SimpleDateFormat dateFormat = new SimpleDateFormat( "'IMG'_yyyyMMdd_HHmmss"); return dateFormat.format(date) + ".jpg"; } }
更多关于Android相关内容感兴趣的读者可查看本站专题:《Android拍照与图片处理技巧总结》、《Android图形与图像处理技巧总结》、《Android开发入门与进阶教程》、《Android调试技巧与常见问题解决方法汇总》、《Android基本组件用法总结》、《Android视图View技巧总结》、《Android布局layout技巧总结》及《Android控件用法总结》
希望本文所述对大家Android程序设计有所帮助。
相关文章
Android自定义有限制区域的图例角度自识别涂鸦工具类完结篇
这篇文章主要为大家介绍了Android自定义有限制区域的图例角度自识别涂鸦工具类完结篇,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2023-02-02Android网络请求库android-async-http介绍
这篇文章主要介绍了Android网络请求库android-async-http介绍,本文讲解了android-async-http的概念、特征以及使用实例,需要的朋友可以参考下2015-06-06Android开发中避免应用无响应的方法(Application Not Responding、ANR)
这篇文章主要介绍了Android开发中避免应用无响应的方法,即避免弹出Application Not Responding(ANR)对话框,需要的朋友可以参考下2014-06-06Kotlin开发中open关键字与类名函数名和变量名的使用方法浅析
这篇文档中,我们将解释如何以及为什么将 open 关键字与类名、函数名和变量名一起使用,了解内部原理是为了帮助我们做扩展,同时也是验证了一个人的学习能力,如果你想让自己的职业道路更上一层楼,这些底层的东西你是必须要会的2023-02-02Android 中 FrameLayout 布局及属性的使用详解
这篇文章主要介绍了Android 中 FrameLayout 布局及属性的使用,FrameLayout 在实现简单布局时非常方便,特别适用于叠加式布局,如显示叠加的图层或浮动按钮等,需要的朋友可以参考下2024-03-03Android程序开发通过HttpURLConnection上传文件到服务器
这篇文章主要介绍了Android程序开发通过HttpURLConnection上传文件到服务器的相关资料,需要的朋友可以参考下2016-01-01
最新评论