0

在我的應用程序中,當用戶在'ImageView'上按下時,我創建一個選擇器意圖,將促進用戶更改圖片。覆蓋選擇器意圖行爲

這裏是我使用的代碼時,在ImageView的用戶點擊

userProfileIMG.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      Intent photoPickerIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
      photoPickerIntent.setType("image/*"); 

      Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
      if (takePhotoIntent.resolveActivity(getPackageManager()) != null) 
      { 
       try 
       { 
        photoFile = Create_ImageFile(); 
       } 
       catch (Exception e) 
       { 
        Log.e("file", e.toString()); 
       } 
       if (photoFile != null) 
       { 
        takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile.getAbsoluteFile())); 
       } 
      } 

      String pickTitle = getString(R.string.select_or_take_new_picture); 
      Intent chooserIntent = Intent.createChooser(photoPickerIntent, pickTitle); 
      chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[]{takePhotoIntent}); 

      startActivityForResult(chooserIntent, ACTIVITY_SELECT_PICTURE); 
     } 
    }); 

這是結果

enter image description here

我的問題是,當上「畫廊」用戶請選擇6.0上的圖像它的安全例外,因爲我想請求<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>只有當他按'畫廊'有什麼方法來覆蓋用戶按'畫廊'時的行爲,以便我請求權限,然後如果他被授予我讓他繼續去畫廊否則我否認他? 或者我必須手動構建相同的選擇器,而不取決於系統爲我構建對話框?

+0

相機需要許可。您需要請求動態訪問攝像頭的權限。 –

+0

更好的方法是在用戶進入配置文件頁面時請求權限。僅在授予Camera和EXTERNAL_STORAGE權限時才顯示對話框以選擇選項。 –

+0

@RaguSwaminathan我不能訪問相機,我讓相機自己拍照,並且不需要許可。 實際上,這是您讓系統應用程序爲您完成工作的權限的方式。 更多https://developer.android.com/training/permissions/best-practices.html –

回答

0

在Android 6.0及以上版本中從外部存儲器寫入和讀取需要動態請求權限。這是一個在android 6.0以後版本[Marshmallow]中引入的特殊功能。

雖然你聲明的

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
在Android清單

,在Android 6.0上運行時,會檢查是否存在授予您的應用程序寫入到外部存儲權限。 [請參閱設置 - >應用程序中的應用程序權限詳細信息 - > youapp - >權限]。如果它被禁用,你可能會遇到崩潰。

,並瞭解更多詳情檢查android developer portal

也可參考下面的帖子,會給你一些想法。

Storage permission error in Marshmallow

Android 6.0 Marshmallow. Cannot write to SD Card

更新:通過訪問意圖還需要實行許可制度。 Image from Gallery in Android 6(Marshmallow)

讓我知道的查詢。

+0

我知道這一點,我的問題與如何申請Android 6.0的權限無關012但是,而是問如何檢測用戶何時按下「圖庫」,因此我可以問問滲透。 –

+0

你試圖讀取設備存儲? –

+0

是的,我正在嘗試,我知道它需要6.0的運行時權限,但我的問題不是如何請求訪問設備存儲的權限。 我的問題是另一個不同的話題,請仔細閱讀我的問題以及我要求的 –