系统相册做兼容性测试的时候,遇到神坑手机Redmi Note4。
先上调用的代码:
//调用相册
private void go2Gallery() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
try {
startActivityForResult(intent, PICK_IMAGE_DATA);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
}
}
在其他手机都能正确的打开相册,但是在这个手机会出现下面这个截图
然后点击相册,没有任何反应,看了log也没有任何的报错,然后就开始各种百度使用了下面的调用方式:
//调用相册
private void go2Gallery() {
Intent intent = new Intent();
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
if (Build.VERSION.SDK_INT < 19) {
intent.setAction(Intent.ACTION_GET_CONTENT);
} else {
intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
}
try {
startActivityForResult(intent, PICK_IMAGE_DATA);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
}
}
怀着即将解决的心情,打开测试手机,尼玛这次直接打开的是个树形的文件管理器。内心奔溃中,差点怀疑人生。然后对比了前面的两个方法,无非就是intent的action不一样,然后就接着百度终于还有一个action叫做Intent.ACTION_PICK。然后换了下面的代码:
//调用相册
private void go2Gallery() {
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
try {
startActivityForResult(intent, PICK_IMAGE_DATA);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
}
}
哎呀妈呀,终于成了,然后又试了试其它的手机,包括6.0,7.0的Android手机,都正常。问题总算是解决了。
下面是完整的代码,仅供参考~
//调用系统相机
private void doTakePhoto() {
mCurrentPhotoFile = new File(yourpath);
if (!mCurrentPhotoFile.exists()) {
try {
mCurrentPhotoFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE, null);
intent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(mCurrentPhotoFile));
try {
startActivityForResult(intent, CAMERA_WITH_DATA);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
}
}
//调用系统相册
private void go2Gallery() {
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
try {
startActivityForResult(intent, PICK_IMAGE_DATA);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
if (requestCode == CAMERA_WITH_DATA) {// 从拍照界面回来
Uri uri = data.getData();
if (uri != null) {
String path = Util.getGoogleImagePath(mActivity, uri);
//...do your code
}
} else if (requestCode == PICK_IMAGE_DATA) {// 从相册选择图片回来
Uri uri = data.getData();
if (uri != null) {
String path = Util.getGoogleImagePath(mActivity, uri);
//...do your code
}
}
}
}
private String getGoogleImagePath(Context context, Uri uri) {
File cacheDir = null;
if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {
cacheDir = new File(android.os.Environment.getExternalStorageDirectory(), ".OCFL311");
} else {
cacheDir = context.getCacheDir();
}
if (!cacheDir.exists()) {
cacheDir.mkdirs();
}
File file = new File(cacheDir, "store_image.jpg");
try {
InputStream is = context.getContentResolver().openInputStream(uri);
if (is.available() == 0)
return null;
OutputStream os = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int length = -1;
while ((length = is.read(buffer)) != -1) {
os.write(buffer, 0, length);
}
is.close();
os.close();
return file.getAbsolutePath();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}