2016-09-13 30 views
0

這是我的代碼,我在我的LG G3上運行這個應用程序,它的工作原理,但在屏幕上出現「圖像無效」的圖像不出現在屏幕上。ImageView在Android 5上工作,但沒有在Android 6上工作,我該如何解決?

  selectedImage = null; 
      orientation = -1; 
      selectedImage = data.getData(); 
      String[] filePathColumn = {MediaStore.Images.Media.DATA}; 

      // Get the cursor 
      Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null); 
      Cursor cursorF = getContentResolver().query(selectedImage, new String[]{MediaStore.Images.ImageColumns.ORIENTATION}, null, null, null); 
      // Move to first row 
      cursor.moveToFirst(); 

      if (cursorF == null || cursorF.getCount() != 1) { 
       orientation = 90; //Assuming it was taken portrait 
      } else { 
       cursorF.moveToFirst(); 
       orientation = cursorF.getInt(0); 
      } 
      //Toast.makeText(this, "orientation: " + orientation, Toast.LENGTH_LONG).show(); 
      int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
      imgDecodableString = cursor.getString(columnIndex);//imgDecodableString = percorso completo immagine 
      cursor.close(); 

      bmpImage = BitmapFactory.decodeFile(imgDecodableString); 
      bmpImage = RotateBitmap.rotateImageIfRequiredFile(bmpImage, orientation); 

      ImageView imgView = (ImageView) findViewById(R.id.imgView); 
      // Set the Image in ImageView after decoding the String 
      imgView.setImageBitmap(bmpImage); 
      //name of image 
      String sFilePath = getRealPathFromURI(selectedImage); 
      String saPathParts[] = sFilePath.split("/"); 
      String sFileName = saPathParts[saPathParts.length - 1]; 
      filename = sFileName; 
      Toast.makeText(this, "filename:" + filename, Toast.LENGTH_LONG).show(); 
      if (bmpImage == null) { 
       Toast.makeText(getBaseContext(), "Image invalid", Toast.LENGTH_SHORT).show(); 
      } 

     } 

回答

1

我認爲這是因爲從Android 6開始,您需要在使用它們時請求權限,而不是在安裝應用程序時請求權限。您似乎正在訪問需要許可的用戶存儲。搜索Android Marshmallow運行時權限。

+0

這可能是原因,但我不知道如何解決?你能給我更具體的建議嗎? –

+0

@PasqualeMoramarco看看這裏:https://developer.android.com/training/permissions/requesting.html – amitairos

+0

我讀過這個頁面,但他們不是很有經驗,我應該更改代碼中的內存讀取權限?非常感謝你 –

相關問題