Android编程实现调用相册、相机及拍照后直接裁剪的方法

作者:Jacob-wj 时间:2023-12-19 10:27:08 

本文实例讲述了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,相册,相机,拍照
0
投稿

猜你喜欢

  • C++中的String的常用函数用法

    2023-03-26 17:52:45
  • Java 实现repalceAll只替换第二个匹配到的字符串

    2021-06-12 11:56:20
  • autojs模仿QQ长按弹窗菜单实现示例详解二

    2022-10-20 07:04:37
  • C#实现获取一年中是第几个星期的方法

    2023-12-04 02:02:46
  • 详解android与服务端交互的两种方式

    2022-11-08 00:24:51
  • JavaWeb核心技术中Session与Cookie浅析

    2023-06-07 20:43:17
  • 详解c#与python的交互方式

    2023-12-13 09:16:11
  • android计算器实现两位数的加减乘除

    2022-11-08 06:46:32
  • Java模拟计算机的整数乘积计算功能示例

    2022-01-30 19:25:51
  • C# 系统热键注册实现代码

    2021-10-07 00:12:14
  • SpringBoot深入浅出分析初始化器

    2022-07-06 09:05:59
  • SpringBoot整合Shiro两种方式(总结)

    2021-09-08 21:28:24
  • Android实现GPS定位代码实例

    2022-07-14 17:26:21
  • SpringBoot实现接口数据的加解密功能

    2023-06-30 00:11:01
  • C#数据结构之单链表(LinkList)实例详解

    2021-09-15 21:40:14
  • IntelliJ IDEA maven 构建简单springmvc项目(图文教程)

    2021-09-13 05:40:40
  • Android读取资源文件的方法

    2022-09-25 22:11:53
  • C#设计模式之ChainOfResponsibility职责链模式解决真假美猴王问题实例

    2023-04-01 00:39:00
  • 浅谈springcloud常用依赖和配置

    2023-11-24 07:50:02
  • SpringBoot配置mybatis驼峰命名规则自动转换的实现

    2023-07-26 17:47:43
  • asp之家 软件编程 m.aspxhome.com