错误未找到活动来处理Intent act = android.media.action.IMAGE_CAPTURE [英] Error No Activity found to handle Intent act=android.media.action.IMAGE_CAPTURE

查看:682
本文介绍了错误未找到活动来处理Intent act = android.media.action.IMAGE_CAPTURE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我继承了在<中起作用的代码。 api 23.我必须升级它才能使用最新的api,由于存在公开的文件安全性问题,因此我现在必须使用FileProvider。没什么但是,现在相机无法正常工作了(不一定是由于FileProvider的缘故,由于升级了api,它恰好在需要使用FileProvider的同时停止了工作)



代码在MediaStore.ACTION_IMAGE_CAPTURE意图的创建意图调用上终止,并显示以下错误:


android.content .ActivityNotFoundException:未找到处理
目的的活动{act = android.media.action.IMAGE_CAPTURE
dat = content://com.company.appname/external_files/temp.jpg flg = 0x1}


我已经做了很多搜索,但是每个人(尤其是到目前为止的stackoverflow)似乎都认为添加代码来检查是否相机存在将解决此问题,但事实并非如此。如果未检测到相机,则仅检查不运行代码。我用来测试的所有设备都配有相机。我已经在Android模拟器上进行了调试(并通过使用默认的照相机应用程序对自己的照片进行拍照来验证模拟的照相机是否已连接到便携式计算机)以及我的手机,该手机具有前置和后置摄像头,两者均可正常工作。因此,请没有解决该问题的答案。我知道我有相机,不需要检查。我需要帮助来确定为什么我的代码没有找到MediaStore的意图。



AndroidManifest.xml

 <使用权限android:name = android.permission.CAMERA /> 
< uses-feature android:name = android.hardware.camera />

 < provider 
android:name =。Providers.GenericFileProvider
android:authorities = com.company.myapp
android:exported = false
android:grantUriPermissions = true>
<元数据
android:name = android.support.FILE_PROVIDER_PATHS
android:resource = @ xml / provider_paths />
< / provider>

provider_paths.xml

 <?xml version = 1.0 encoding = utf-8?> 
< paths xmlns:android = http://schemas.android.com/apk/res/android>
< external-path name = external_files path =。 />
< / paths>

MainActivity.java

 文件f =新文件(Environment.getExternalStorageDirectory(), temp.jpg); 
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE,
FileProvider.getUriForFile(MainActivity.this,AUTHORITY,f));
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivityForResult(intent,REQUEST_CAMERA);

代码在启动活动时在最后一行抛出异常。因为这是继承的代码,所以我很想废弃它们用于相机捕获的内容并重新开始,但是在将其升级为> api 23之前,这些代码曾经可以正常工作。



仅供参考,升级之前的上一个调用看起来像是

  Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(f));

但不再起作用(由于暴露了文件uri而被api阻止了)



有人发现了如何使它起作用吗?



样式说明



为简洁起见,我编辑了代码,生产代码中有异常处理。

解决方案

< blockquote>

仅供参考,升级之前的上一个调用看起来像是


该代码使用了 EXTRA_OUTPUT ,这是通过 ACTION_IMAGE_CAPTURE 传递 Uri 的正确方法。它的问题是 Uri.fromFile()部分,一旦 targetSdkVersion 升高,该部分就无法在Android 7.0+上运行如您所指出的,达到24或更高。



正确的代码是两者的组合:

  File f = new File(Environment.getExternalStorageDirectory(), temp.jpg); 
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,FileProvider.getUriForFile(MainActivity.this,AUTHORITY,f));
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivityForResult(intent,REQUEST_CAMERA);

请参见此示例应用,了解将 ACTION_IMAGE_CAPTURE FileProvider一起使用的完整示例


I have code that I inherited that worked in < api 23. I had to upgrade it to work with the latest apis, and it necessitated me to use a FileProvider now, due to a exposed file security issue. No bigs. However, now the camera doesn't work (and it's not necessarily due to the FileProvider, it just happened to have stopped working at the same time as needing to use FileProvider, due to the upgraded api)

The code dies on the create intent call for the MediaStore.ACTION_IMAGE_CAPTURE intent with the error:

android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.media.action.IMAGE_CAPTURE dat=content://com.company.appname/external_files/temp.jpg flg=0x1 }

I've done a ton of searching for this, but everyone (especially stackoverflow so far) seems to think that adding code to check whether a camera is present will solve this, but it doesn't. It's just a check to not run the code if a camera is not detected. All my devices I'm using to test have cameras. I've debugged on the Android emulator (and verified the emulated camera is connected to my laptop camera by taking a picture of myself using the default camera application) as well as my phone, which has front and back cameras, both of which work. So, no answers that circumvent the problem, please. I know I have cameras and don't need to check for them. I need help determining why my code isn't finding an intent for MediaStore.ACTION_IMAGE_CAPTURE.

AndroidManifest.xml

<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />

and

<provider
    android:name=".Providers.GenericFileProvider"
    android:authorities="com.company.myapp"
    android:exported="false"
    android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths"/>
    </provider>

provider_paths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external_files" path="."/>
</paths>

MainActivity.java

File f = new File(Environment.getExternalStorageDirectory(), "temp.jpg");
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE, 
    FileProvider.getUriForFile(MainActivity.this, AUTHORITY, f));
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivityForResult(intent, REQUEST_CAMERA);

The code throws an exception on the last line when it starts the activity. Because this was inherited code, I'm tempted to scrap what they have for the camera capture and start fresh, but the code used to work before it was upgraded for > api 23.

Just an fyi, the previous call before the upgrade looked like

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));

but that no longer works (blocked by api because of the exposure of the file uri)

Has anyone found out how to make this work?

Style Note

I edited the code for brevity, there is exception handling in the production code.

解决方案

Just an fyi, the previous call before the upgrade looked like

That code uses EXTRA_OUTPUT, which is the correct way to pass the Uri with ACTION_IMAGE_CAPTURE. Its problem is the Uri.fromFile() part, which will not work on Android 7.0+, once your targetSdkVersion rises to 24 or higher, as you noted.

The correct code is the combination of the two:

File f = new File(Environment.getExternalStorageDirectory(), "temp.jpg");
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
intent.putExtra(MediaStore.EXTRA_OUTPUT, FileProvider.getUriForFile(MainActivity.this, AUTHORITY, f));
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivityForResult(intent, REQUEST_CAMERA);

See this sample app for a complete example of using ACTION_IMAGE_CAPTURE with FileProvider.

这篇关于错误未找到活动来处理Intent act = android.media.action.IMAGE_CAPTURE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆