Error No Activity found to handle Intent act=android.media.action.IMAGE_CAPTURE

后端 未结 1 1096
梦毁少年i
梦毁少年i 2021-01-22 01:20

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 fil

1条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-22 01:51

    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.

    0 讨论(0)
提交回复
热议问题