简体   繁体   中英

MediaStore.Images.Media.insertImage is returning null when trying to get image uri

I need to get the uri from image bitmap so i use this method to get the uri

public Uri getImageUri(Context inContext, Bitmap inImage) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    fixMediaDir();
    String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
    return Uri.parse(path);
}

but MediaStore.Images.Media.insertImage return null in some devices

note : I have added permissions to the manifest

<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

and added the runtime permissions for camera and storage as well.

I also added FileProvider in the manifest:

<provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="com.mvc.imagepicker.provider"
        android:exported="false"
        android:grantUriPermissions="true"
        tools:replace="android:authorities">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths"
            tools:replace="android:resource"/>
    </provider>

notice that the authorities on the provider is "com.mvc.imagepicker.provider" because i use Mariovc/ImagePicker and used the same authorities like his.

I saw in other posts, people mentioned to add this code before calling

MediaStore.Images.Media.insertImage

public void fixMediaDir() {
    File sdcard = Environment.getExternalStorageDirectory();
    Log.d("a111", "sdcard: "+sdcard);
    if (sdcard != null) {
        File mediaDir = new File(sdcard, "DCIM/Camera");
        if (!mediaDir.exists()) {
            mediaDir.mkdirs();
        }
    }
}

so it will create a directory if not excists, but it didnt help me.

so im posting this here maybe i'm missing something else and somebody can help me out.

Since you are saying it is returning null in some devices, I think this is because the permissions are not provided. Setting permissions in manifest only specify the permissions required. It is up to the device or the operating system to grant them. WRITE_EXTERNAL_STORAGE and READ_EXTERNAL_STORAGE are not provided by default in some operating systems. Try this:

private final String[] permissions = new String[]{Manifest.permission.CAMERA, 
                                                Manifest.permission.READ_EXTERNAL_STORAGE, 
                                                Manifest.permission.WRITE_EXTERNAL_STORAGE};
private static final int REQUEST_CODE = 12345; //Some random number


...
...

@Override
protected void onCreate(Bundle savedInstanceState) {

    ...

    if (ActivityCompat.checkSelfPermission(SplashScreenActivity.this, permissions[0]) != PackageManager.PERMISSION_GRANTED ||
            ActivityCompat.checkSelfPermission(SplashScreenActivity.this, permissions[1]) != PackageManager.PERMISSION_GRANTED ||
            ActivityCompat.checkSelfPermission(SplashScreenActivity.this, permissions[2]) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(SplashScreenActivity.this, permissions, REQUEST_CODE);
    }

    ...
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == REQUEST_CODE) {
        if (grantResults.length == 3 && grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED && grantResults[2] == PackageManager.PERMISSION_GRANTED) {
            Toast.makeText(SplashScreenActivity.this, "Permission granted!!!", Toast.LENGTH_LONG).show();
        }
        else {
            Toast.makeText(SplashScreenActivity.this, "Necessary permissions not granted...", Toast.LENGTH_LONG).show();
            finish();
        }
    }
}

Add this to the first activity in your app. This will ask the user to grant the permissions if not already provided.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM