gpt4 book ai didi

java - 尝试在 Android Java 中将图片保存到外部存储时出现 "No Activity found to handle Intent"错误

转载 作者:行者123 更新时间:2023-12-01 17:30:22 30 4
gpt4 key购买 nike

我正在尝试从 MediaStore.ACTION_IMAGE_CAPTURE Intent 将图片保存到外部存储。我不断收到此错误:

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");
}

list 文件中的 Activity 声明:

        <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>

list 文件中的权限:

<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() 因为我是从 fragment 运行它。我还尝试了 getActivity() 来代替它,但这不起作用。

最佳答案

您是否添加了 list 中提供的文件?如果没有就这样使用

<provider
android:name=".fileprovider"
android:authorities="com.xxxx.xxxx.xxxx" //Your package name
android:enabled="true"
android:exported="false" />

并且可以处理 Activity 结果

onActivityResult中的代码将是这样的,

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 - 尝试在 Android Java 中将图片保存到外部存储时出现 "No Activity found to handle Intent"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61132972/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com