我绝望地在我的代码中寻找错误,并且非常感谢sb可以帮助我。 我正在尝试将图像保存到我的三星Galaxy Tab s2的SD卡中 但无论我使用什么代码,我似乎都无法对SD卡进行任何更改。
下面的代码段落表明,尽管清单发生了变化,但我无权在SD卡上写入数据。
有没有人遇到同样的问题或知道这里发生了什么?
(android:minSdkVersion =" 19&#34 ;; android: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)
答案 0 :(得分: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
}
多数民众赞成。快乐的编码:)
答案 1 :(得分: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);
}