我正在尝试从图库中选择图像,但是我的应用程序除了“出错了”消息之外.我以为我正确设置了android WRITE_EXTERNAL_STORAGE和READ_EXTERNAL_STORAGE权限,但我一直收到错误我应该怎么做让它工作?
这是我的Log cat错误
06-07 12:07:27.567 1692-1711/? E/DatabaseUtils﹕ Writing exception to parcel java.lang.SecurityException: Permission Denial: reading com.androID.provIDers.media.MediaProvIDer uri content://media/external/images/media/359 from pID=2818, uID=10057 requires androID.permission.READ_EXTERNAL_STORAGE, or grantUriPermission() at androID.content.ContentProvIDer.enforceReadPermissionInner(ContentProvIDer.java:605) at androID.content.ContentProvIDer$Transport.enforceReadPermission(ContentProvIDer.java:480) at androID.content.ContentProvIDer$Transport.query(ContentProvIDer.java:211) at androID.content.ContentProvIDerNative.onTransact(ContentProvIDerNative.java:112) at androID.os.Binder.execTransact(Binder.java:453)
这是我的活动代码
public class MainActivity extends Activity { private static int RESulT_LOAD_img = 1; String imgDecodableString; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); } public voID loadImagefromgallery(VIEw vIEw) { // Create intent to Open Image applications like gallery, Google Photos Intent galleryIntent = new Intent(Intent.ACTION_PICK, androID.provIDer.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); // Start the Intent startActivityForResult(galleryIntent, RESulT_LOAD_img); } @OverrIDe protected voID onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); try { // When an Image is picked if (requestCode == RESulT_LOAD_img && resultCode == RESulT_OK && null != data) { // Get the Image from data Uri selectedImage = data.getData(); String[] filePathColumn = { MediaStore.Images.Media.DATA }; // Get the cursor Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null); // Move to first row cursor.movetoFirst(); int columnIndex = cursor.getColumnIndex(filePathColumn[0]); imgDecodableString = cursor.getString(columnIndex); cursor.close(); ImageVIEw imgVIEw = (ImageVIEw) findVIEwByID(R.ID.imgVIEw); // Set the Image in ImageVIEw after deCoding the String imgVIEw.setimageBitmap(BitmapFactory .decodefile(imgDecodableString)); } else { Toast.makeText(this, "You haven't picked Image", Toast.LENGTH_LONG).show(); } } catch (Exception e) { Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG) .show(); } }}
这是我的Menifest.xml文件代码
<?xml version="1.0" enCoding="utf-8"?><manifest xmlns:androID="http://schemas.androID.com/apk/res/androID" package="com.example.tazeen.image_fromgallery" > <uses-permission androID:name="androID.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission androID:name="androID.permission.READ_EXTERNAL_STORAGE" /> <application androID:allowBackup="true" androID:icon="@mipmap/ic_launcher" androID:label="@string/app_name" androID:theme="@style/Apptheme" > <activity androID:name=".MainActivity" androID:label="@string/app_name" > <intent-filter> <action androID:name="androID.intent.action.MAIN" /> <category androID:name="androID.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest>
解决方法:
要检查API级别23及更高级别的手动权限,请使用此代码.
public static final int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 123;public boolean checkPermissionREAD_EXTERNAL_STORAGE( final Context context) { int currentAPIVersion = Build.VERSION.SDK_INT; if (currentAPIVersion >= androID.os.Build.VERSION_CODES.M) { if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { if (ActivityCompat.shouldShowRequestPermissionRationale( (Activity) context, Manifest.permission.READ_EXTERNAL_STORAGE)) { showDialog("External storage", context, Manifest.permission.READ_EXTERNAL_STORAGE); } else { ActivityCompat .requestPermissions( (Activity) context, new String[] { Manifest.permission.READ_EXTERNAL_STORAGE }, MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE); } return false; } else { return true; } } else { return true; } }
的ShowDialog()
public voID showDialog(final String msg, final Context context, final String permission) { AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context); alertBuilder.setCancelable(true); alertBuilder.setTitle("Permission necessary"); alertBuilder.setMessage(msg + " permission is necessary"); alertBuilder.setPositivebutton(androID.R.string.yes, new DialogInterface.OnClickListener() { public voID onClick(DialogInterface dialog, int which) { ActivityCompat.requestPermissions((Activity) context, new String[] { permission }, MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE); } }); AlertDialog alert = alertBuilder.create(); alert.show(); }
在你的活动检查这样.
if (checkPermissionREAD_EXTERNAL_STORAGE(this)) { // do your stuff.. }
并且不要忘记添加onRequestPermissionsResult.
@OverrIDe public voID onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { switch (requestCode) { case MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE: if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { // do your stuff } else { Toast.makeText(Login.this, "GET_ACCOUNTS DenIEd", Toast.LENGTH_SHORT).show(); } break; default: super.onRequestPermissionsResult(requestCode, permissions, grantResults); } }
快乐编码..
总结以上是内存溢出为你收集整理的java.lang.SecurityException:Permission Denial:在Android中读取com.android.providers.media.MediaProvider同时全部内容,希望文章能够帮你解决java.lang.SecurityException:Permission Denial:在Android中读取com.android.providers.media.MediaProvider同时所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)