声明:
本文的Demo可用于从本地获取用户头像时使用,解决了有些手机系统相机拍照后获取不到拍摄照片的问题,以及解决小米miui系统调用系统裁剪图片功能camera.action.CROP后崩溃或重新打开app的问题。
主活动文件ChooseImageMainActivity
package com.example.no_clay.demolist.ChooseImage;import android.content.Context;import android.content.Intent;import android.content.SharedPreferences;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.drawable.BitmapDrawable;import android.graphics.drawable.Drawable;import android.net.Uri;import android.os.Bundle;import android.os.Environment;import android.provider.MediaStore;import android.support.annotation.Nullable;import android.support.v7.app.AppCompatActivity;import android.util.Log;import android.view.Gravity;import android.view.View;import android.widget.Button;import android.widget.ImageView;import android.widget.Toast;import com.example.no_clay.demolist.R;import java.io.File;import java.io.IOException;import cn.bmob.v3.Bmob;import cn.bmob.v3.datatype.BmobFile;import cn.bmob.v3.listener.SaveListener;import cn.bmob.v3.listener.UploadFileListener;/** * Created by 寒 on 2016/6/4. */public class ChooseImageMainActivity extends AppCompatActivity { private ImageView imageView; private SelectPicPopupWindow menuWindow; private static final int REQUEST_CODE_PICK_IMAGE = 0; private static final int REQUEST_CODE_CAPTURE_CAMEIA = 1; private static final int RESIZE_REQUEST_CODE = 2; private static final String TAG = "ChooseImageMainActivity"; private Uri userImageUri; private Context context = ChooseImageMainActivity.this; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.choose_image_main_layout); imageView = (ImageView) findViewById(R.id.choose_image_image); imageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { menuWindow = new SelectPicPopupWindow(context, new View.OnClickListener() { @Override public void onClick(View v) { menuWindow.dismiss(); switch (v.getId()){ case R.id.takePhotoBtn: { String state = Environment.getExternalStorageState(); if (state.equals(Environment.MEDIA_MOUNTED)) { Intent getImageByCamera = new Intent("android.media.action.IMAGE_CAPTURE"); startActivityForResult(getImageByCamera, REQUEST_CODE_CAPTURE_CAMEIA); } else { Toast.makeText(getApplicationContext(), "请确认已经插入SD卡", Toast.LENGTH_LONG).show(); } break; } case R.id.pickPhotoBtn: Intent intent = new Intent("android.intent.action.GET_CONTENT"); intent.setType("image/*"); startActivityForResult(intent, REQUEST_CODE_PICK_IMAGE); break; case R.id.cancelBtn:{ break; } } } }); menuWindow.showAtLocation(findViewById(R.id.mainLayout), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { Uri imageUri = null; if(resultCode == RESULT_CANCELED){ Toast.makeText(context,"获取失败",Toast.LENGTH_SHORT).show(); imageView.setImageBitmap(null); }else if(resultCode == RESULT_OK) { if (requestCode == REQUEST_CODE_PICK_IMAGE) { imageUri = data.getData(); Log.d(TAG, "onActivityResult: " + imageUri); reSizeImage(imageUri); } else if (requestCode == REQUEST_CODE_CAPTURE_CAMEIA) { Log.d(TAG, "onActivityResult: from photo"); imageUri = data.getData(); Log.d(TAG, "onActivityResult: " + imageUri); if (imageUri == null) { Bundle bundle = data.getExtras(); if (bundle != null) { Bitmap bitMap = (Bitmap) bundle.get("data"); imageUri = Uri.parse(MediaStore.Images.Media. insertImage(getContentResolver(), bitMap, null,null)); Log.d(TAG, "onActivityResult: bndle != null" + imageUri); reSizeImage(imageUri); } else { Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show(); } } }else if(requestCode == RESIZE_REQUEST_CODE){ Log.d(TAG, "onActivityResult: " + userImageUri); showImage(userImageUri); } } } private void reSizeImage(Uri uri) { 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", 100); intent.putExtra("outputY", 100); /** * 此方法返回的图片只能是小图片(测试为高宽160px的图片) * 故将图片保存在Uri中,调用时将Uri转换为Bitmap,此方法还可解决miui系统不能return data的问题 */ userImageUri = Uri.parse("file://"+ Environment.getExternalStorageDirectory().getPath() + "/" + "small.jpg"); intent.putExtra(MediaStore.EXTRA_OUTPUT, userImageUri); intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()); startActivityForResult(intent, RESIZE_REQUEST_CODE); } private void showImage(Uri uri) { Log.d(TAG, "showImage: "); try { Log.d(TAG, "showImage: " + uri.toString()); SharedPreferences.Editor editor = getSharedPreferences("image",MODE_PRIVATE).edit(); editor.putString("imageUri",uri.toString()); editor.commit(); Bitmap photo = MediaStore.Images.Media.getBitmap(context .getContentResolver(), uri); imageView.setImageDrawable(new BitmapDrawable(photo)); Toast.makeText(context,"头像设置成功",Toast.LENGTH_SHORT).show(); } catch (IOException e) { Toast.makeText(context,"头像设置失败",Toast.LENGTH_SHORT).show(); e.printStackTrace(); } }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
【注解】:
1.Environment.getExternalStorageState是用来获取外部存储设备的状态,即SD卡的状态,这里为大家推荐一篇博文:[Android中的Environment.getExternalStorageState使用]
(http://blog.csdn.net/yuzhiboyi/article/details/8645730)
3.Itent.ACTION_PICK 、Intent.ACTION_GET_CONTENT
两者都可以弹出一张选择列表, 两者都可以当作我们获取手机本地资源的途径,通过设置Intent.setType(String type);即可实现不同资源的选取,不同的是Intent.ACTION_GET_CONTENT会弹出对话框选择资源的来源。常用的类型有:
//选择图片 requestCode 返回的标识 Intent innerIntent = new Intent(Intent.ACTION_GET_CONTENT); //"Android.intent.action.GET_CONTENT" innerIntent.setType(contentType); //查看类型 String IMAGE_UNSPECIFIED = "image/*"; Intent wrapperIntent = Intent.createChooser(innerIntent, null); ((Activity) context).startActivityForResult(wrapperIntent, requestCode); //视频 Intent innerIntent = new Intent(Intent.ACTION_GET_CONTENT); innerIntent.setType(contentType); //String VIDEO_UNSPECIFIED = "video/*"; Intent wrapperIntent = Intent.createChooser(innerIntent, null); ((Activity) context).startActivityForResult(wrapperIntent, requestCode); //添加音频 Intent innerIntent = new Intent(Intent.ACTION_GET_CONTENT); innerIntent.setType(contentType); //String VIDEO_UNSPECIFIED = "video/*"; Intent wrapperIntent = Intent.createChooser(innerIntent, null); ((Activity) context).startActivityForResult(wrapperIntent, requestCode); //录音 Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType(ContentType.AUDIO_AMR); //String AUDIO_AMR = "audio/amr"; intent.setClassName("com.android.soundrecorder", "com.android.soundrecorder.SoundRecorder"); ((Activity) context).startActivityForResult(intent, requestCode);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
更详细的使用请看博客:android之Itent.ACTION_PICK Intent.ACTION_GET_CONTENT妙用
5.在onActivityResult(int requestCode, int resultCode, Intent data)中为什么要写出两种方法来获取相机拍照后的Uri?
前边通过图片选择返回一个Uri,但是部分机型在拍照在Intent中返回的为Uri为空,于是我们需要将这些机型的获取Uri方法写出来,通过获取存储在bundle中的缩略图的Uri实现。(有些机型利用Bundle来返回照片数据)
6.reSizeImage(Uri uri)中为何不使用下列方法:
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", 100)intent.putExtra("outputY", 100)intent.putExtra("return-data", true)intent.putExtra("output", Uri.fromFile(new File("/mnt/sdcard/temp")))startActivityForResult(intent, RESIZE_REQUEST_CODE)
上述方法中,裁剪后的图片通过Intent的putExtra(“return-data”,true)方法进行传递,miui系统问题就出在这里,return-data的方式只适用于小图,miui系统默认的裁剪图片可能裁剪得过大,或对return-data分配的资源不足,造成return-data失败。
解决思路是:裁剪后,将裁剪的图片保存在Uri中,在onActivityResult()方法中,再提取对应的Uri图片转换为Bitmap使用。
其实大家直观也能感觉出来,Intent主要用于不同Activity之间通信,是一种动态的小巧的资源占用,类似于Http请求中的GET,并不适用于传递图片之类的大数据。于是当A生成一个大数据要传递给B,往往不是通过Intent直接传递,而是在A生成数据的时候将数据保存到C,B再去调用C,C相当于一个转换的中间件。
即以下方法:
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", 100) intent.putExtra("outputY", 100) userImageUri = Uri.parse("file://"+ Environment.getExternalStorageDirectory().getPath() + "/" + "small.jpg") intent.putExtra(MediaStore.EXTRA_OUTPUT, userImageUri) intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()) startActivityForResult(intent, RESIZE_REQUEST_CODE)
主界面布局代码
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:id="@+id/mainLayout" android:backgroundTint="@color/bisque" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:textColor="@color/white" android:paddingLeft="20dp" android:gravity="center_vertical|left" android:background="@color/brown" android:textSize="20sp" android:layout_width="match_parent" android:layout_height="50dp" android:text="选择图片" /> <ImageView android:id="@+id/choose_image_image" android:layout_marginTop="50dp" android:layout_gravity="center_horizontal" android:layout_width="100dp" android:layout_height="100dp" /></LinearLayout>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
弹出窗口代码:
package com.example.no_clay.demolist.ChooseImageimport android.annotation.SuppressLintimport android.content.Contextimport android.graphics.drawable.ColorDrawableimport android.view.LayoutInflaterimport android.view.MotionEventimport android.view.Viewimport android.view.ViewGroupimport android.widget.Buttonimport android.widget.PopupWindowimport com.example.no_clay.demolist.Rclass SelectPicPopupWindow extends PopupWindow { private Button takePhotoBtn, pickPhotoBtn, cancelBtn private View mMenuView @SuppressLint("InflateParams") public SelectPicPopupWindow(Context context, View.OnClickListener itemsOnClick) { super(context) LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE) mMenuView = inflater.inflate(R.layout.choose_image_layout_dialog_pic, null) takePhotoBtn = (Button) mMenuView.findViewById(R.id.takePhotoBtn) pickPhotoBtn = (Button) mMenuView.findViewById(R.id.pickPhotoBtn) cancelBtn = (Button) mMenuView.findViewById(R.id.cancelBtn) cancelBtn.setOnClickListener(itemsOnClick) pickPhotoBtn.setOnClickListener(itemsOnClick) takePhotoBtn.setOnClickListener(itemsOnClick) this.setContentView(mMenuView) this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT) this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT) this.setFocusable(true) this.setAnimationStyle(R.style.PopupAnimation) ColorDrawable dw = new ColorDrawable(0x80000000) this.setBackgroundDrawable(dw) mMenuView.setOnTouchListener(new View.OnTouchListener() { @Override @SuppressLint("ClickableViewAccessibility") public boolean onTouch(View v, MotionEvent event) { int height = mMenuView.findViewById(R.id.pop_layout).getTop() int y = (int) event.getY() if (event.getAction() == MotionEvent.ACTION_UP) { if (y < height) { dismiss() } } return true } }) }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
弹出窗口布局choose_image_layout_dialog_pic.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:orientation="vertical" > <LinearLayout android:id="@+id/pop_layout" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:background="#312E3F" android:gravity="center_horizontal" android:orientation="vertical" > <Button android:id="@+id/takePhotoBtn" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dip" android:layout_marginRight="10dip" android:layout_marginTop="10dp" android:background="#373447" android:padding="10dp" android:text="拍照" android:textColor="#CEC9E7" android:textSize="18sp" android:textStyle="bold" /> <Button android:id="@+id/pickPhotoBtn" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dip" android:layout_marginRight="10dip" android:layout_marginTop="5dp" android:background="#373447" android:padding="10dp" android:text="从相册选择" android:textColor="#CEC9E7" android:textSize="18sp" android:textStyle="bold" /> <Button android:id="@+id/cancelBtn" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="15dip" android:layout_marginLeft="10dip" android:layout_marginRight="10dip" android:layout_marginTop="20dp" android:background="@android:color/white" android:padding="10dp" android:text="取消" android:textColor="#373447" android:textSize="18sp" android:textStyle="bold" /> </LinearLayout></RelativeLayout>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61