2017-07-17 108 views
1

我必须通过代码来获得外部所有图像由MediaStore.Images.Media.EXTERNAL_CONTENT_URI和内部相同(但内部而不是外部)MediaStore.Images.Media.INTERNAL_CONTENT_URI返回空指针

在我的设备的Android 7.0版外部所有图像和内部外部光标一样问题的标题内返回空指针返回,我是100%,我有大量的图片在内部

大怪习题 相同的代码工作正常与其他设备的Android 5.1版内部收益内部图像和外部回报它是自己的imgs所以问题在哪里我不'弄不明白....

如何相同的代码工作在不同的2个实设备(这里没有模拟器)

两个光标的代码如下

cursorEx = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 
        projection, null, null, DATE_DESC_SORT_ORDER); 

cursorIn = getContentResolver().query(MediaStore.Images.Media.INTERNAL_CONTENT_URI, 
        projection, null, null, DATE_DESC_SORT_ORDER); 

回答

0

也许你没有问在Android 7权限。它的强制性!您可以申请许可,或针对一个SDK小于22

if (ContextCompat.checkSelfPermission(ProfileActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) 
      != PackageManager.PERMISSION_GRANTED) { 

     // Should we show an explanation? 
     if (ActivityCompat.shouldShowRequestPermissionRationale(ProfileActivity.this, 
       Manifest.permission.READ_EXTERNAL_STORAGE)) { 
      // Explain to the user why we need to read the contacts 
     } 

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

     // MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE is an 
     // app-defined int constant that should be quite unique 

     return; 
    }else { 
     Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
     intent.setType("image/*"); 
     startActivityForResult(Intent.createChooser(intent, 
       "Selecione a foto "), select_image_code); 
    } 
+0

当然,我请求允许否则会抛出异常......对不起,但不是解决..谢谢 –