英文:
"No Activity found to handle Intent" error when trying to save picture to external storage in Android Java
问题
我试图从MediaStore.ACTION_IMAGE_CAPTURE
意图中将图片保存到外部存储,但一直收到以下错误:
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.media.action.IMAGE_CAPTURE dat=content://ca.kevincook.trackmytrip.fileprovider/DCIM/default_image.jpg flg=0x3 clip={text/uri-list U:content://ca.kevincook.trackmytrip.fileprovider/DCIM/default_image.jpg} (has extras) }
以下是我的代码:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File imagePath = new File(getContext().getExternalFilesDir(Environment.DIRECTORY_DCIM), "");
File newFile = new File(imagePath, "default_image.jpg");
Uri contentUri = FileProvider.getUriForFile(getContext(), "ca.kevincook.trackmytrip.fileprovider", newFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, contentUri);
intent.setData(contentUri);
getContext().grantUriPermission("ca.kevincook.trackmytrip", contentUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
getContext().grantUriPermission("ca.kevincook.trackmytrip", contentUri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivityForResult(intent, 0);
onActivityResult
方法:
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 0) {
if (resultCode == Activity.RESULT_OK) {
Uri uri = data.getData();
GalleryAdapter.addImage(BitmapFactory.decodeFile(uri.toString()));
}
}
SaveImagePopup("Enter Picture Name");
}
在清单文件中声明的活动:
<activity android:name=".MapsActivity" android:label="@string/title_activity_maps">
<intent-filter>
<action android:name="android.intent.action.IMAGE_CAPTURE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
清单文件中的权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera" android:required="true"/>
provider_paths.xml
:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_public_path" path="." />
</paths>
我将代码完全从 Android 开发者网站复制了一遍:
https://developer.android.com/reference/androidx/core/content/FileProvider
我在这里使用了 getContext()
,因为我是在一个片段中运行它。我也尝试过用 getActivity()
替代它,但没有奏效。
英文:
I am trying to save a picture to external storage from a MediaStore.ACTION_IMAGE_CAPTURE intent.
I keep getting this error:
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.media.action.IMAGE_CAPTURE dat=content://ca.kevincook.trackmytrip.fileprovider/DCIM/default_image.jpg flg=0x3 clip={text/uri-list U:content://ca.kevincook.trackmytrip.fileprovider/DCIM/default_image.jpg} (has extras) }
Here is my code:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File imagePath = new File(getContext().getExternalFilesDir(Environment.DIRECTORY_DCIM), "");
File newFile = new File(imagePath, "default_image.jpg");
Uri contentUri = FileProvider.getUriForFile(getContext(), "ca.kevincook.trackmytrip.fileprovider", newFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, contentUri);
intent.setData(contentUri);
getContext().grantUriPermission("ca.kevincook.trackmytrip", contentUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
getContext().grantUriPermission("ca.kevincook.trackmytrip", contentUri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivityForResult(intent, 0);
onActivityResult method:
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 0)
{
if (resultCode == Activity.RESULT_OK)
{
Uri uri = data.getData();
GalleryAdapter.addImage(BitmapFactory.decodeFile(uri.toString()));
}
}
SaveImagePopup("Enter Picture Name");
}
Activity declaration in manifest file:
<activity android:name=".MapsActivity" android:label="@string/title_activity_maps">
<intent-filter>
<action android:name="android.intent.action.IMAGE_CAPTURE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Permissions in manifest file:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-feature android:name = "android.hardware.camera"
android:required="true"/>
provider_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_public_path" path="." />
</paths>
I copied the code exactly from the android developer website:
https://developer.android.com/reference/androidx/core/content/FileProvider
I used getContext() because I'm running it from a fragment. I also tried getActivity() in its place, but that didn't work.
答案1
得分: 0
Sure, here's the translated code:
你是否已将清单中提供的文件添加进去?如果没有,请按照以下方式使用:
<provider
android:name=".fileprovider"
android:authorities="com.xxxx.xxxx.xxxx" // 你的包名
android:enabled="true"
android:exported="false" />
并且可以在活动结果中进行处理。
在 onActivityResult 中的代码将会是这样的:
```java
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CAMERA && resultCode == RESULT_OK) {
File out = new File(getFilesDir(), "newImage.jpg");
if (!out.exists()) {
Toast.makeText(getBaseContext(), "捕获图像时出错", Toast.LENGTH_LONG).show();
return;
}
Bitmap mBitmap = BitmapFactory.decodeFile(compressedImage.getAbsolutePath());
ivShowImage.setImageBitmap(mBitmap);
}
}
英文:
Have you added the file provided in the manifest? If not Use it in this way
<provider
android:name=".fileprovider"
android:authorities="com.xxxx.xxxx.xxxx" //Your package name
android:enabled="true"
android:exported="false" />
and can handle in on the activity result
The code in onActivityResult will be like this,
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CAMERA && resultCode == RESULT_OK) {
File out = new File(getFilesDir(), "newImage.jpg");
if (!out.exists()) {
Toast.makeText(getBaseContext(), "Error while capturing image", Toast.LENGTH_LONG).show();
return;
}
Bitmap mBitmap = BitmapFactory.decodeFile(compressedImage.getAbsolutePath());
ivShowImage.setImageBitmap(mBitmap);
}
}
专注分享java语言的经验与见解,让所有开发者获益!
评论