Android Q: Intent(MediaStore.ACTION_IMAGE_CAPTURE) - No Activity found to handle Intent
The MIME type on an Intent
forms part of the criteria for determining what activities have an <intent-filter>
that matches the Intent
. Adding a MIME type on an Intent
, where that MIME type is unexpected, will lead to problems like you are seeing.
The ACTION_IMAGE_CAPTURE
documentation says nothing about supplying a MIME type on the request. As a result, camera apps with ACTION_IMAGE_CAPTURE
<intent-filter>
structures will not be advertising that they support any particular MIME types. And, as a result, when Android tries finding a matching activity for your Intent
, it comes up empty.
So, remove the MIME type to clear up the ActivityNotFoundException
.
How to get Image URI by intent MediaStore.ACTION_IMAGE_CAPTURE in Android 10 and above
This approach worked for me
In Manifest
file
<application>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.example.android.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths">
</meta-data>
</provider>
...
</application
created a file /res/xml/file_paths.xml
and specified path in that
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-files-path name="my_images" path="Pictures" />
</paths>
In my activity
created a global variable var cameraPhotoFilePath: Uri? = null
this is how I started Camera acitivity for results
val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
val photoFile: File? = try {
createImageFileInAppDir()
} catch (ex: IOException) {
// Error occurred while creating the File
null
}
photoFile?.also { file ->
val photoURI: Uri = FileProvider.getUriForFile(
this,
"com.example.android.provider",
file
)
cameraPhotoFilePath = photoURI
intent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI)
}
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
startActivityForResult(intent, ACTION_REQUEST_CAMERA)
here is a helper function that i used in above code
@Throws(IOException::class)
private fun createImageFileInAppDir(): File {
val timeStamp: String = SimpleDateFormat("yyyyMMdd_HHmmss").format(Date())
val imagePath = getExternalFilesDir(Environment.DIRECTORY_PICTURES)
return File(imagePath, "JPEG_${timeStamp}_" + ".jpg")
}
At the end in onActivityResult
thats how I got image Uri
if (resultCode == RESULT_OK && requestCode == ACTION_REQUEST_CAMERA) {
cameraPhotoFilePath?.let { uri ->
// here uri is image Uri that was captured by camera
}
}
Using intent to use Camera in Android
Try request code 1337.
startActivityForResult(cameraIntent , 1337);
capturing images with MediaStore.ACTION_IMAGE_CAPTURE intent in android
Photos taken by the ACTION_IMAGE_CAPTURE are not registered in the MediaStore automatically on all devices.
The official Android guide gives that example:
http://developer.android.com/guide/topics/media/camera.html#intent-receive
But that does not work either on all devices.
The only reliable method I am aware of consists in saving the path to the picture in a local variable. Beware that your app may get killed while in background (while the camera app is running), so you must save the path during onSaveInstanceState.
Edit after comment:
Create a temporary file name where the photo will be stored when starting the intent.
File tempFile = File.createTempFile("my_app", ".jpg");
fileName = tempFile.getAbsolutePath();
Uri uri = Uri.fromFile(tempFile);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(intent, PICTURE_REQUEST_CODE);
fileName
is a String, a field of your activity. You must save it that way:
@Override
public void onSaveInstanceState(Bundle bundle)
{
super.onSaveInstanceState(bundle);
bundle.putString("fileName", fileName);
}
and recover it in onCreate():
public void onCreate(Bundle savedInstanceState)
{
if (savedInstanceState != null)
fileName = savedInstanceState.getString("fileName");
// ...
}
Now, at the time of onActivityResult, you know the name of the file where the photo was stored (fileName
). You can do anything you wish with it, and then delete it.
2013-09-19 edit: It seems that some camera apps ignore the putExtra uri, and store the pic in a different place. So, before using the value of fileName
you should check whether the intent is null or not. If you get a non-null intent, then you should prefer intent.getData()
over fileName
. Use fileName
as a backup solution only when it is needed.
Android 12 (SDK 29) Extra_Output for ACTION_IMAGE_CAPTURE not working
Add these lines in Manifests files
<queries>
<intent>
<action android:name="android.media.action.IMAGE_CAPTURE" />
</intent>
</queries>
ACTION_IMAGE_CAPTURE returns imagefile as Extra instead of Data
Usually I can simply call getData() on the intent I receive in onActivityResult and get the content uri for the MediaProvider.
Your code will fail on many of them, including all recent Nexus devices. ACTION_IMAGE_CAPTURE
is not supposed to return a Uri
. Quoting the documentation:
If the EXTRA_OUTPUT is not present, then a small sized image is returned as a Bitmap object in the extra field. This is useful for applications that only need a small image. If the EXTRA_OUTPUT is present, then the full-sized image will be written to the Uri value of EXTRA_OUTPUT.
Since you are not including EXTRA_OUTPUT
, you will get a data
extra (getData().getExtra("data")
) with a thumbnail image.
the intent I get in onActivityResult has no data, but instead, it has a parcellable extra
Given your ACTION_IMAGE_CAPTURE
request, that is what you are supposed to get.
If you want a full-size image, include EXTRA_OUTPUT
, perhaps pointing to a FileProvider
in your app, such as I demonstrate in this sample app. Then, you know the Uri
where the photo should go, because you specified that Uri
.
Related Topics
Checking If an Android Application Is Running in the Background
How Exactly Does the Android:Onclick Xml Attribute Differ from Setonclicklistener
How to Filter Specific Apps For Action_Send Intent (And Set a Different Text For Each App)
Communicating Between a Fragment and an Activity - Best Practices
Android - Dynamically Add Views into View
Android Min Sdk Version Vs. Target Sdk Version
How to Convert an Image into a Base64 String
How to Send Post Request in Json Using Httpclient in Android
How to Detect Orientation Change in Layout in Android
Find Location of a Removable Sd Card
Fullscreen Activity in Android
How to Set the Part of the Text View Is Clickable
How to Check a Certain Data Already Exists in Firestore or Not
Navigationview Get/Find Header Layout