这是我的图片选择器代码(本地和谷歌照片)
这是我的意向请求
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
if(fileType!=null) {
if(fileType.equals("video")) {
photoPickerIntent.setType("video/*");
}else if(fileType.equals("image"))
{
photoPickerIntent.setType("image/*");
}
}else
{
photoPickerIntent.setType("*/*");
}
startActivityForResult(photoPickerIntent, RESULT_LOAD_IMAGE);
这是我的onActivityResult
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK) {
Uri selectedImage = data.getData();
if (selectedImage.contains("mediakey")){
InputStream is=null;
try {
is = getContentResolver().openInputStream(Uri.parse(data.getData().toString()));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Bitmap bitmap = BitmapFactory.decodeStream(is);
String tempSep="holder";
File downloaded_file = null;
try{
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "Plates");
OutputStream fOut = null;
downloaded_file = new File(mediaStorageDir.getPath() + File.separator +
"IMG_" + tempSep + ".jpg");
fOut = new FileOutputStream(downloaded_file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
fOut.flush();
fOut.close();
}catch (Exception e) {
e.printStackTrace();
}
picturePath=downloaded_file.toString();
String extension = picturePath.substring(picturePath.lastIndexOf("."));
extension = extension.toLowerCase();
Log.e("Extension", extension);
if (extension.equals(".mp4") || extension.equals(".jpg") || extension.equals(".png") || extension.equals(".jpeg")) {
} else {
imagePick();
Toast.makeText(select_image_camera.this, "Wrong format !", Toast.LENGTH_SHORT).show();
return;
}
}else {//Process local images}
}
else {
Intent i=new Intent(this,camera.class);
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(i);
}
}
以上代码适用于大多数谷歌云照片,但对于某些人来说,它失败并在尝试通过
下载时引发异常InputStream is=null;
try {
is = getContentResolver().openInputStream(Uri.parse(data.getData().toString()));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
代码有什么问题或我可以添加的东西吗?谢谢你的帮助