Home > Software design >  MediaStore.ACTION_IMAGE_CAPTURE Output is 0 Bytes
MediaStore.ACTION_IMAGE_CAPTURE Output is 0 Bytes

Time:01-27

I am having issues capturing photos on a CAT S42 running Android 11. My code worked fine on older phones so I suspect it is an Android version thing. The following code creates the JPG file but it is always zero bytes:

    private void startCamera() {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (intent.resolveActivity(getPackageManager()) != null) {
            File mNewPhoto = new File(getFilesDir(), "data-input-image.jpg");
            mNewPhoto.delete();
            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mNewPhoto));
            startActivityForResult(intent, 1);
        }
    }

I've been experimenting with using different save locations and permissions but I can't fix it.

I've got these permissions in my manifest:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.NFC" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_SYNC_SETTINGS" />
<uses-permission android:name="android.permission.READ_SYNC_STATS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS" />

CodePudding user response:

Do not use Uri.fromFile() to get an uri.

Instead use FileProvider.getUriForFile().

CodePudding user response:

AndroidManifest.xml

<application>
      ...
      <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="${applicationId}.fileProvider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>
 </application>

file_paths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <files-path
        name="files"
        path="." />
    <external-path
        name="external"
        path="." />
</paths>

Java code

Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE).addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
String filename = getExternalFilesDir(Environment.DIRECTORY_PICTURES).getPath()   "/wallpaper.jpg";
Uri fileProvider = FileProvider.getUriForFile(this, getPackageName()   ".fileProvider", new File(filename));
cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, fileProvider);
Intent chooserIntent = Intent.createChooser(cameraIntent, "Image Chooser");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Parcelable[]{cameraIntent});
chooserIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivityForResult(chooserIntent, 1);
  • Related