"No Activity found to handle Intent" error when trying to save picture to external storage in Android Java

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

"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), &quot;&quot;);
                File newFile = new File(imagePath, &quot;default_image.jpg&quot;);
                Uri contentUri = FileProvider.getUriForFile(getContext(), &quot;ca.kevincook.trackmytrip.fileprovider&quot;, newFile);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, contentUri);
                intent.setData(contentUri);
                getContext().grantUriPermission(&quot;ca.kevincook.trackmytrip&quot;, contentUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
                getContext().grantUriPermission(&quot;ca.kevincook.trackmytrip&quot;, 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(&quot;Enter Picture Name&quot;);
    }

Activity declaration in manifest file:

        &lt;activity   android:name=&quot;.MapsActivity&quot; android:label=&quot;@string/title_activity_maps&quot;&gt;
            &lt;intent-filter&gt;
                &lt;action android:name=&quot;android.intent.action.IMAGE_CAPTURE&quot; /&gt;
                &lt;category android:name=&quot;android.intent.category.DEFAULT&quot; /&gt;
            &lt;/intent-filter&gt;
        &lt;/activity&gt;

Permissions in manifest file:

&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;
&lt;uses-feature android:name = &quot;android.hardware.camera&quot;
        android:required=&quot;true&quot;/&gt;

provider_paths.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;paths xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;&gt;
    &lt;external-path name=&quot;external_public_path&quot; path=&quot;.&quot; /&gt;
&lt;/paths&gt;

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

&lt;provider
    android:name=&quot;.fileprovider&quot;
    android:authorities=&quot;com.xxxx.xxxx.xxxx&quot; //Your package name
    android:enabled=&quot;true&quot;
    android:exported=&quot;false&quot; /&gt;

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 &amp;&amp; resultCode == RESULT_OK) {

            File out = new File(getFilesDir(), &quot;newImage.jpg&quot;);
            if (!out.exists()) {
                Toast.makeText(getBaseContext(), &quot;Error while capturing image&quot;, Toast.LENGTH_LONG).show();

                return;
            }

            Bitmap mBitmap =     BitmapFactory.decodeFile(compressedImage.getAbsolutePath());
           
           ivShowImage.setImageBitmap(mBitmap);
    }
}

huangapple
  • 本文由 发表于 2020年4月10日 10:02:56
  • 转载请务必保留本文链接:https://java.coder-hub.com/61132972.html
匿名

发表评论

匿名网友

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

确定