2017-01-16 134 views
3

我是新手,在android中使用圖像。我想從內部存儲器加載圖像,但它給了我權限被拒絕的錯誤,然後我已經將權限添加到Android清單文件。但我仍然無法完成我的任務。如何顯示從內部存儲選擇的imageView上的圖像?

這裏是我的代碼:

import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.widget.ImageView; 

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     ImageView image = (ImageView)findViewById(R.id.imageView); 
     Bitmap bm = BitmapFactory.decodeFile("/storage/emulated/0/DCIM/Camera/IMG_20151102_193132.jpg"); 
     image.setImageBitmap(bm); 

    } 
} 

的logcat:

01-16 15:16:11.345 6533-6533/com.example.jaytanna.imagemap E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/DCIM/Camera/IMG_20151102_193132.jpg: open failed: EACCES (Permission denied) 

它不顯示任何圖像。請幫助我,謝謝。

+0

你能後的logcat的 – vishnumm93

回答

2

入住這

 File imgFile = new File("/storage/emulated/0/DCIM/Camera/IMG_20151102_193132.jpg"); 
     if(imgFile.exists()){ 

     Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath()); 

     ImageView myImage = (ImageView) findViewById(R.id.imageviewTest); 

     myImage.setImageBitmap(myBitmap); 

     }; 

,包括此權限清單文件:

<manifest> 
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    ... 
    <application> 
     ... 
     <activity> 
      ... 
     </activity> 
    </application> 
</manifest> 

對於API 23+,你需要請求讀/寫權限,即使它們已經在您的清單中。

// Storage Permissions 
private static final int REQUEST_EXTERNAL_STORAGE = 1; 
private static String[] PERMISSIONS_STORAGE = { 
     Manifest.permission.READ_EXTERNAL_STORAGE, 
     Manifest.permission.WRITE_EXTERNAL_STORAGE 
}; 

/** 
* Checks if the app has permission to write to device storage 
* 
* If the app does not has permission then the user will be prompted to grant permissions 
* 
* @param activity 
*/ 
public static void verifyStoragePermissions(Activity activity) { 
    // Check if we have write permission 
    int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE); 

    if (permission != PackageManager.PERMISSION_GRANTED) { 
     // We don't have permission so prompt the user 
     ActivityCompat.requestPermissions(
       activity, 
       PERMISSIONS_STORAGE, 
       REQUEST_EXTERNAL_STORAGE 
     ); 
    } 
} 

和處理的性反應,一些示例:

@Override 
public void onRequestPermissionsResult(int requestCode, 
     String permissions[], int[] grantResults) { 
    switch (requestCode) { 
     case MY_PERMISSIONS_REQUEST_READ_CONTACTS: { 
      // If request is cancelled, the result arrays are empty. 
      if (grantResults.length > 0 
       && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 

       // permission was granted, yay! Do the 
       // contacts-related task you need to do. 

      } else { 

       // permission denied, boo! Disable the 
       // functionality that depends on this permission. 
      } 
      return; 
     } 

     // other 'case' lines to check for other 
     // permissions this app might request 
    } 
} 

有關請求權限的API 23+的官方文檔,檢查https://developer.android.com/training/permissions/requesting.html

+0

我的文件是內部存儲。 –

+0

更改文件路徑到您的內部存儲並運行代碼,如果有任何錯誤讓我知道,並顯示android studio錯誤日誌 –

+0

01-16 15:46:51.260 20918-20918/com.example.jaytanna.imagemap E/BitmapFactory:Unable解碼流:java.io.FileNotFoundException:/storage/emulated/0/DCIM/Camera/IMG_20151102_193132.jpg:打開失敗:EACCES(權限被拒絕) –

0

您是否在應用程序設置中啓用了權限? (READ_EXTERNAL_STORAGE,或WRITE_EXTERNAL_STORAGE都將顯示爲 'storage')

+0

我已經啓用這個權限 –

1

你需要添加運行permisssion檢查這樣的:

public boolean isStoragePermissionGranted() { 
    if (Build.VERSION.SDK_INT >= 23) { 
     if (checkSelfPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE) 
       == PackageManager.PERMISSION_GRANTED) { 
      Log.v(TAG,"Permission is granted"); 
      return true; 
     } else { 

      Log.v(TAG,"Permission is revoked"); 
      ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1); 
      return false; 
     } 
    } 
    else { //permission is automatically granted on sdk<23 upon installation 
     Log.v(TAG,"Permission is granted"); 
     return true; 
    } 
} 

使用它像:

if(isStoragePermissionGranted()){ 
Bitmap bm = BitmapFactory.decodeFile("/storage/emulated/0/DCIM/Camera/IMG_20151102_193132.jpg"); 
image.setImageBitmap(bm); 

} 

趕上結果

@Override 
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { 
    super.onRequestPermissionsResult(requestCode, permissions, grantResults); 
    if(grantResults[0]== PackageManager.PERMISSION_GRANTED){ 
     Log.v(TAG,"Permission: "+permissions[0]+ "was "+grantResults[0]); 
     //resume tasks needing this permission 
     Bitmap bm = BitmapFactory.decodeFile("/storage/emulated/0/DCIM/Camera/IMG_20151102_193132.jpg"); 
     image.setImageBitmap(bm); 
    } 
} 
+0

在哪裏添加此代碼片段 –

+1

在活動 – rafsanahmad007

+1

中將「onCreate」的第一個和最後一個代碼片段複製到您的'oncreate()'{{Bitmap bm = BitmapFactory .decodeFile( 「/存儲/模擬/ 0/DCIM /相機/ IMG_20151102_193132.jpg」); image.setImageBitmap(bm);}' – rafsanahmad007

相關問題