2017-03-27 126 views
-2

我正在尋找我的代碼中的錯誤,如果某人能幫助我,我會很感激。 我試圖將圖像保存到我的三星Galaxy Tab s2的SD卡s2 ,但無論使用什麼代碼,我似乎都無法對SD卡進行任何更改。沒有權限寫在SD卡上

下面的代碼段說明儘管清單發生了變化,但我沒有權限在SD卡上寫入數據。

有沒有人有同樣的問題或知道這裏發生了什麼?

(機器人:的minSdkVersion = 「19」;機器人:targetSdkVersion = 「25」)

CODE:

File filepath = Environment.getExternalStorageDirectory(); 
    File dir = new File(filepath.getAbsolutePath() + "/SaveImage"); 

    MediaStore.Images.Media.insertImage(getContentResolver(), bmp ,"Image.jpg", "IMAGE"); 

清單:

<manifest 

xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.ma.singhalja.saveimage"> 

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

<application 

LOG:

03-28 01:16:54.647 14292-14292/com.ma.singhalja.saveimage E/MediaStore: Failed to insert image 
                    java.lang.SecurityException: Permission Denial: writing com.android.providers.media.MediaProvider uri content://media/external/images/media from pid=14292, uid=10141 requires android.permission.WRITE_EXTERNAL_STORAGE, or grantUriPermission() 
                     at android.os.Parcel.readException(Parcel.java:1620) 
+3

您是否添加運行時權限? – Redman

回答

5

你必須處理運行時允許這一點,看看在這個步驟

拷貝類:

public class PermissionHandler { 


// Run Time Permission List 
     private static final String MNC = "MNC"; 

     // Calendar group. 
     public static final String READ_CALENDAR = Manifest.permission.READ_CALENDAR; 
     public static final String WRITE_CALENDAR = Manifest.permission.WRITE_CALENDAR; 

     // Camera group. 
     public static final String CAMERA = Manifest.permission.CAMERA; 

     // Contacts group. 
     public static final String READ_CONTACTS = Manifest.permission.READ_CONTACTS; 
     public static final String WRITE_CONTACTS = Manifest.permission.WRITE_CONTACTS; 


     // Location group. 
     public static final String ACCESS_FINE_LOCATION = Manifest.permission.ACCESS_FINE_LOCATION; 
     public static final String ACCESS_COARSE_LOCATION = Manifest.permission.ACCESS_COARSE_LOCATION; 

     // Microphone group. 
     public static final String RECORD_AUDIO = Manifest.permission.RECORD_AUDIO; 

     // Phone group. 
     public static final String READ_PHONE_STATE = Manifest.permission.READ_PHONE_STATE; 
     public static final String CALL_PHONE = Manifest.permission.CALL_PHONE; 
     public static final String READ_CALL_LOG = Manifest.permission.READ_CALL_LOG; 
     public static final String WRITE_CALL_LOG = Manifest.permission.WRITE_CALL_LOG; 
     public static final String ADD_VOICEMAIL = Manifest.permission.ADD_VOICEMAIL; 
     public static final String USE_SIP = Manifest.permission.USE_SIP; 
     public static final String PROCESS_OUTGOING_CALLS = Manifest.permission.PROCESS_OUTGOING_CALLS; 

     // Sensors group. 
     public static final String BODY_SENSORS = Manifest.permission.BODY_SENSORS; 
     public static final String USE_FINGERPRINT = Manifest.permission.USE_FINGERPRINT; 

     // SMS group. 
     public static final String SEND_SMS = Manifest.permission.SEND_SMS; 
     public static final String RECEIVE_SMS = Manifest.permission.RECEIVE_SMS; 
     public static final String READ_SMS = Manifest.permission.READ_SMS; 
     public static final String RECEIVE_WAP_PUSH = Manifest.permission.RECEIVE_WAP_PUSH; 
     public static final String RECEIVE_MMS = Manifest.permission.RECEIVE_MMS; 
     public static final String READ_CELL_BROADCASTS = "android.permission.READ_CELL_BROADCASTS"; 

     // Bookmarks group. 
     public static final String READ_HISTORY_BOOKMARKS = "com.android.browser.permission.READ_HISTORY_BOOKMARKS"; 
     public static final String WRITE_HISTORY_BOOKMARKS = "com.android.browser.permission.WRITE_HISTORY_BOOKMARKS"; 


     public static boolean isPermissionGranted(Activity mContext, String Permission, String Text, int PermissionCode) { 

      if (ContextCompat.checkSelfPermission(mContext, Permission) != PackageManager.PERMISSION_GRANTED) { 
       reqPermission(mContext, Text, PermissionCode, Permission); 
       return false; 
      } 

      return true; 
     } 


     public static void reqPermission(Activity mContext, String Text, int PermissionCode, String Permission) { 
      if (!ActivityCompat.shouldShowRequestPermissionRationale(mContext, Permission)) { 
       ActivityCompat.requestPermissions(mContext, new String[]{Permission}, PermissionCode); 
      } else { 
       openAlertDialog(mContext, Text); 
      } 
     } 

     public static void openAlertDialog(final Context mContext, String Text) { 

      new AlertDialog.Builder(mContext) 
        .setTitle("Permission") 
        .setMessage(mContext.getResources().getString(R.string.app_name) + " needs to access " + Text + " Permission for using this features from Setting >> Permissions.") 
        .setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int which) { 
          // Your code 
          String packageName = "you.App.Id"; 
          try { 
           //Open the specific App Info page: 
           Intent intent = new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS); 
           intent.setData(Uri.parse("package:" + packageName)); 
           mContext.startActivity(intent); 

          } catch (ActivityNotFoundException e) { 
           //e.printStackTrace(); 
           //Open the generic Apps page: 
           Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_APPLICATIONS_SETTINGS); 
           mContext.startActivity(intent); 

          } 

         } 
        }) 
        .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int which) { 
          // do nothing 
         } 
        }) 
        .show(); 


     } 

    } 

之前保存的圖像,你需要檢查這個

if (PermissionHandler.isPermissionGranted((Activity) mContext, Manifest.permission.WRITE_EXTERNAL_STORAGE, "External Storage", 1000)) { 
         // Save your Image code 
        } 

那就是它吧。快樂編碼:)

+0

問題解決。我不知道該怎麼感謝你才足夠! :) – OangUtanKlaus

+0

你最歡迎的@OangUtanKlaus兄弟 – RushDroid

1

添加運行時間封鎖離子訪問

Add library: 
compile 'pub.devrel:easypermissions:0.2.1' 

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); 
    }