【发布时间】:2020-04-08 07:57:26
【问题描述】:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
//activity A intent
Intent intent = new Intent(Intent.ACTION_PICK,null);
intent.setType("image/*");
intent.putExtra("return-data", true);
startActivityForResult(intent,Constant.ACTION.LOAD_GALLERY_ACTION);
//this is onActicityResult function
case Constant.ACTION.LOAD_GALLERY_ACTION:
Uri uri = data.getData();
if(mImage.size()<maxAddPic) {
mImage.add(copyGalleryPic(uri));
adapter.notifyDataSetChanged();
}
break;
//to copy the gallery photo,return the new file uri.
private Uri copyGalleryPic(Uri uri) {
String filePath = Environment.getExternalStorageDirectory() + "/images/"+"diary"+System.currentTimeMillis()+".jpg";
File outputFile = new File(filePath);
if (!outputFile.getParentFile().exists()) {
outputFile.getParentFile().mkdirs();
}
Bitmap bitmap;
try (FileOutputStream fos = new FileOutputStream(outputFile)) {
bitmap=MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
bitmap.compress(Bitmap.CompressFormat.JPEG,100,fos);
} catch (IOException e) {
// ... handle IO exception
}
Uri newUri = FileProvider.getUriForFile(CreateDiaryActivity.this,"com.example.diary.cr",outputFile);
return newUri;
}
但有一些错误
intent 返回 uri
“content://com.google.android.apps.photos.contentprovider/-1/1/content%3A%2F%2Fmedia%2Fexternal%2Fimages%2Fmedia%2F29/ORIGINAL/NONE/41130179”
但调试信息显示代码
"位图=MediaStore.Images.Media.getBitmap(getContentResolver(), uri);" 错误:java.io.FileNotFoundException:打开失败:ENOENT (没有这样的文件或目录)
请帮帮我!爱你们!
我的英语不好。请使用简单的词。谢谢!
【问题讨论】:
-
您是否申请了运行时权限?
-
谢谢。我找到了Android Studio可能有一些错误的答案。