将相册照片复制到我的应用文件夹中。

huangapple 未分类评论51阅读模式
英文:

Copy the gallery photo into my app's folder

问题

//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;
}

However, there are some errors:

The intent returns the URI:

"content://com.google.android.apps.photos.contentprovider/-1/1/content%3A%2F%2Fmedia%2Fexternal%2Fimages%2Fmedia%2F29/ORIGINAL/NONE/41130179"

But the debug info shows the error in this line:

"bitmap=MediaStore.Images.Media.getBitmap(getContentResolver(), uri);"
Error: java.io.FileNotFoundException: open failed: ENOENT (No such file or directory)

Please help me! Love you guys!

My English is not very good. Please use simple words. Thank you!

英文:
&lt;uses-permission android:name=&quot;android.permission.WRITE_EXTERNAL_STORAGE&quot;/&gt;
&lt;uses-permission android:name=&quot;android.permission.READ_EXTERNAL_STORAGE&quot;/&gt;
//activity A intent
Intent intent = new Intent(Intent.ACTION_PICK,null);
intent.setType(&quot;image/*&quot;);
intent.putExtra(&quot;return-data&quot;, 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()&lt;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() + &quot;/images/&quot;+&quot;diary&quot;+System.currentTimeMillis()+&quot;.jpg&quot;;
	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,&quot;com.example.diary.cr&quot;,outputFile);
	return newUri;
}

but there are some errors

the intent returns uri

> "content://com.google.android.apps.photos.contentprovider/-1/1/content%3A%2F%2Fmedia%2Fexternal%2Fimages%2Fmedia%2F29/ORIGINAL/NONE/41130179"

but debug info shows the code

> "bitmap=MediaStore.Images.Media.getBitmap(getContentResolver(),
> uri);
" error : java.io.FileNotFoundException: open failed: ENOENT
> (No such file or directory)

please help me ! Love you guys!

My English is not well. please use Simple words. Thank you!

答案1

得分: 0

 * 你使用的是哪个安卓版本和哪个SDK版本?我有一个在安卓2.2上无法在安卓7下接收意图权限的应用,请参考:https://github.com/k3b/intent-intercept/issues/4。我认为重新使用更新的安卓SDK进行重新编译可能会有帮助。
 * 如果您使用InputStream通过[getContentResolver().openInputStream(data.getData(uri))]来读取选定的图像,您的程序是否正常工作?(链接:https://developer.android.com/reference/android/content/ContentResolver#openInputStream(android.net.Uri))
 * 自从安卓4.4起,ACTION_PICK被ACTION_OPEN_DOCUMENT取代。我最近使用ACTION_OPEN_DOCUMENT以及openInputStream的应用程序都能如预期般工作。
英文:
  • which android version and which sdk-version are you using? I have an android-2.2 app that cannot receive intent permissions under android 7 see: https://github.com/k3b/intent-intercept/issues/4. I assume that recompiling with a more recent android-sdk might help.
  • Does you program work if you use an InputStream to read the picked image via getContentResolver().openInputStream(data.getData(uri)).
  • Since android-4.4 ACTION_PICK was replaced by ACTION_OPEN_DOCUMENT. My recent apps that use ACTION_OPEN_DOCUMENT together with openInputStream work as expected

huangapple
  • 本文由 发表于 2020年4月8日 15:57:26
  • 转载请务必保留本文链接:https://java.coder-hub.com/61095877.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定