系統(tǒng)相冊做兼容性測試的時候,遇到神坑手機Redmi Note4。
先上調(diào)用的代碼:
//調(diào)用相冊
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();
}
}
在其他手機都能正確的打開相冊,但是在這個手機會出現(xiàn)下面這個截圖

image.png
然后點擊相冊,沒有任何反應,看了log也沒有任何的報錯,然后就開始各種百度使用了下面的調(diào)用方式:
//調(diào)用相冊
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();
}
}
懷著即將解決的心情,打開測試手機,尼瑪這次直接打開的是個樹形的文件管理器。內(nèi)心奔潰中,差點懷疑人生。然后對比了前面的兩個方法,無非就是intent的action不一樣,然后就接著百度終于還有一個action叫做Intent.ACTION_PICK。然后換了下面的代碼:
//調(diào)用相冊
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手機,都正常。問題總算是解決了。
下面是完整的代碼,僅供參考~
//調(diào)用系統(tǒng)相機
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();
}
}
//調(diào)用系統(tǒng)相冊
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;
}
}