2016-10-05 92 views
-3

我有這個應用程序,將選擇圖片到畫廊,並顯示它到使用Imageview的測試。我的問題是它不會在Android M上工作。我可以選擇圖像,但不會在我的測試中顯示。他們說我需要詢問權限才能訪問android M上的圖像,但不知道如何。請幫忙。如何詢問訪問Android M上的圖庫的權限?

+0

問的問題在這裏只是閱讀開發商的網站一旦 – Piyush

回答

4

從Android 6.0(API級別23)開始,用戶在應用程序運行時爲應用程序授予權限,而不是在安裝應用程序時授予應用程序權限。

類型1-當您的應用程序請求權限時,系統會向用戶顯示一個對話框。當用戶響應時,系統會調用您的應用程序的onRequestPermissionsResult()方法,並將其傳遞給用戶響應。您的應用必須重寫該方法才能確定是否授予了該權限。該回調將傳遞給您傳遞給requestPermissions()的相同請求代碼。

private static final int PICK_FROM_GALLERY = 1; 

ChoosePhoto.setOnClickListener(new View.OnClickListener() 
{ 
    @Override 
    public void onClick (View v){ 
    try { 
     if (ActivityCompat.checkSelfPermission(EditProfileActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { 
      ActivityCompat.requestPermissions(EditProfileActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, PICK_FROM_GALLERY); 
     } else { 
      Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
      startActivityForResult(galleryIntent, PICK_FROM_GALLERY); 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    } 
}); 


@Override 
    public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) 
    { 
     switch (requestCode) { 
      case PICK_FROM_GALLERY: 
       // If request is cancelled, the result arrays are empty. 
       if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
        Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
        startActivityForResult(galleryIntent, PICK_FROM_GALLERY); 
       } else { 
        //do something like displaying a message that he didn`t allow the app to access gallery and you wont be able to let him select from gallery 
       } 
       break; 
     } 
    } 

類型2-如果你想給運行時允許回在一個地方那時候你可以按照下面的鏈接

Android 6.0 multiple permissions

而且在清單中添加權限您的要求

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.CAMERA" /> 
<uses-feature android:name="android.hardware.camera" /> 
<uses-feature android:name="android.hardware.camera.autofocus" /> 

注 -如果Manife st.permission.READ_EXTERNAL_STORAGE產生錯誤,請將其替換爲android.Manifest.permission.READ_EXTERNAL_STORAGE。

==>如果您想了解更多有關運行許可,則請按照下面的鏈接

https://developer.android.com/training/permissions/requesting.html

+0

好感謝前。我會試試這個.. – Crissy

+1

如果你有WRITE_EXTERNAL_STORAGE,你不需要READ_EXTERNAL_STORAGE。 WRITE包含READ。 –

0
public void pickFile() { 
     int permissionCheck = ContextCompat.checkSelfPermission(getActivity(), 
       CAMERA); 
     if (permissionCheck != PackageManager.PERMISSION_GRANTED) { 
      ActivityCompat.requestPermissions(
        getActivity(), 
        new String[]{CAMERA}, 
        PERMISSION_CODE 
      ); 
      return; 
     } 
     openCamera(); 
    } 

    @Override 
    public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], 
              @NonNull int[] grantResults) { 
     if (requestCode == PERMISSION_CODE) { 
      if (grantResults.length > 0 
        && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
       openCamera(); 
      } 
     } 
    } 

    private void openCamera() { 
     Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
     startActivityForResult(intent, CAMERA_CODE); 
    } 

Add permissions in Manifest Also 
<uses-permission android:name="android.permission.CAMERA" />