简体   繁体   English

android.provider.MediaStore.Images.Media.insertImage返回null

[英]android.provider.MediaStore.Images.Media.insertImage returns null

I have following code: 我有以下代码:

Uri u_img = Uri.parse(android.provider.MediaStore.Images.Media.insertImage(
getContentResolver(),f.getAbsolutePath(), null, null));

but android.provider.MediaStore.Images.Media.insertImage returns null on Galaxy S6 6.0.1. android.provider.MediaStore.Images.Media.insertImage在Galaxy S6 6.0.1上返回null。

I have tried following possible solutions but no luck: 我尝试了以下可能的解决方案,但没有运气:

  1. Checking permission of Writing to External Storage -> OK 检查写入外部存储的权限->确定
  2. Try to do the checking of directory is there or not by f.exists() and it returns true 尝试通过f.exists()检查目录是否存在,并返回true

What could be another reason? 可能是另一个原因吗?

public  void addImageToGallery(final String filePath, final Context context) {

    ContentValues values = new ContentValues();

    values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis());
    values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
    values.put(MediaStore.MediaColumns.DATA, filePath);

    context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
}

you are encountering this problem in android Marshmallow, as you said on Galaxy S6 6.0.1. 正如您在Galaxy S6 6.0.1上所说的那样,您在Android棉花糖中遇到了这个问题。 This is because of permissions.you need to request for write permission as MediaStore.Images.Media.insertImage try to write on sd card. 这是因为权限。您需要请求写入权限,因为MediaStore.Images.Media.insertImage尝试在SD卡上写入。 Open a Dialog using the code below: 使用以下代码打开一个对话框:

ActivityCompat.requestPermissions(MainActivity.this,
                new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                1);

Also this link can help you with permission in android 6(Marshmallow) https://developer.android.com/training/permissions/requesting.html 此外,此链接还可以帮助您在android 6(棉花糖) https://developer.android.com/training/permissions/requesting.html中获得许可

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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