2017-03-03 281 views
2

我有一個關於BitMapFactory.decodeFile的問題。BitmapFactory:無法解碼流:java.io.FileNotFoundException

在我的應用程序中,我希望用戶能夠從他/她的設備中選擇圖像或拍攝照片。這必須然後顯示在一個ImageView的

下面是代碼片段:

Uri selectedImage = data.getData(); 
String[] filePathColumn = {MediaStore.Images.Media.DATA}; 
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null); 
        cursor.moveToFirst(); 
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
        String picturePath = cursor.getString(columnIndex); 
        cursor.close(); 
        MyImage image = new MyImage(); 
        image.setTitle("Test"); 
        image.setDescription("test choose a photo from gallery and add it to " + "list view"); 
        image.setDatetime(System.currentTimeMillis()); 
        image.setPath(picturePath); 

而且我得到這個異常:

BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/WhatsApp/Media/WhatsApp Images/IMG-20170302-WA0012.jpg: open failed: EACCES (Permission denied) 

如何解決it.Please幫助me.Thanks提前..

回答

12

它的權限問題,你需要在外部讀取存儲的清單中添加權限,然後你就可以使用它,如果你使用os 6.0以上,那麼你需要使用容易許可。

對於寫:

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

對於閱讀:

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

高於6.0:

private String[] galleryPermissions = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}; 

if (EasyPermissions.hasPermissions(this, galleryPermissions)) { 
      pickImageFromGallery(); 
     } else { 
      EasyPermissions.requestPermissions(this, "Access for storage", 
        101, galleryPermissions); 
     } 

感謝

+2

這是EasyPermissions,如果它是類或字段? – Monali

+3

它的庫編譯'pub.devrel:easypermissions:0.2.1' –

+0

非常感謝..你節省了我的一天.. – Monali

0

你問讀 - 外部運行一次許可存儲當你開始y我們的相機意圖:

final int MyVersion = Build.VERSION.SDK_INT; 
     if (MyVersion > Build.VERSION_CODES.LOLLIPOP_MR1) { 
      if (!checkIfAlreadyhavePermission()) { 
       ActivityCompat.requestPermissions(new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE}, 1); 
      } else { 
       startYourCameraIntent(); 
      } 
     } 

checkIfAlreadyhavePermission()方法:

private boolean checkIfAlreadyhavePermission() { 
     int result = ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE); 
     return result == PackageManager.PERMISSION_GRANTED; 
    } 

手柄權限對話框 「允許」 和 「拒絕」 按鈕的動作在onRequestPermission():

@Override 
     public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { 
      super.onRequestPermissionsResult(requestCode, permissions, grantResults); 
     switch (requestCode) { 
      case 1: { 
       if (grantResults.length > 0 
         && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
        startYourCameraIntent(); 

       } else { 
        Toast.makeText(getActivity(), "Please give your permission.", Toast.LENGTH_LONG).show(); 
       } 
       break; 
      } 
     } 
    } 

並添加此到您的Manifest的應用程序標籤:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
相關問題