我认为我的权限位于AndroidManifest.xml
。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ganedu.intent">
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="23" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".NextActivity"
android:label="This is next_activity"/>
</application>
</manifest>
我有&#34; android.permission.READ_EXTERNAL_STORAGE
&#34;和&#34; android.permission.WRITE_EXTERNAL_STORAGE
&#34;在清单之后。
但错误仍然存在。
引起:java.lang.SecurityException:Permission Denial:读取 com.android.providers.media.MediaProvider uri content:// media / external / images / media / 40 from pid = 2473,uid = 10073 需要android.permission.READ_EXTERNAL_STORAGE,或 grantUriPermission()
这是我的MainActivity.java
public class MainActivity extends AppCompatActivity {
public static final int IMAGE_GALLERY_REQUEST = 20;
Button btnStart;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void gallery_open(View view) {
Intent galleryPickerIntent = new Intent(Intent.ACTION_PICK);
File pictureDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
String pictureDirectoryPath = pictureDirectory.getPath();
Uri data = Uri.parse(pictureDirectoryPath);
galleryPickerIntent.setDataAndType(data,"image/*");
startActivityForResult(galleryPickerIntent, IMAGE_GALLERY_REQUEST);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent){
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
if(resultCode == Activity.RESULT_OK){
if(requestCode == IMAGE_GALLERY_REQUEST) {
onSelectedFromGalleryResult(imageReturnedIntent);
}
}
}
private void onSelectedFromGalleryResult(Intent data){
Bitmap bm = null;
if(data != null){
try{
bm = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), data.getData());
}catch(IOException e){
e.printStackTrace();
}
//ivImage.setImageBitmap(bm);
ImageView imgView = (ImageView)findViewById(R.id.ivImage);
imgView.setImageBitmap(bm);
}
}
答案 0 :(得分:2)
James我想您在Android 6.0或更高版本上遇到此错误。您需要在这些版本中明确处理权限。 请找到android开发者链接
https://developer.android.com/guide/topics/permissions/requesting.html
答案 1 :(得分:1)
您需要提供android marshmallow版本的运行时权限
您可以在onCreate()或任何点击事件中添加此代码,请尝试以下操作:
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1){
// requestPermission();
requestAppPermissions(new String[]{
Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE },
R.string.app_name, REQUEST_PERMISSIONS);
//this code will be executed on devices running ICS or later
}
还添加此功能以请求运行时权限
public void requestAppPermissions(final String[] requestedPermissions,
final int stringId, final int requestCode) {
mErrorString.put(requestCode, stringId);
int permissionCheck = PackageManager.PERMISSION_GRANTED;
boolean shouldShowRequestPermissionRationale = false;
for (String permission : requestedPermissions) {
permissionCheck = permissionCheck + ContextCompat.checkSelfPermission(this, permission);
shouldShowRequestPermissionRationale = shouldShowRequestPermissionRationale || ActivityCompat.shouldShowRequestPermissionRationale(this, permission);
}
if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
if (shouldShowRequestPermissionRationale) {
Snackbar.make(findViewById(android.R.id.content), stringId,
Snackbar.LENGTH_INDEFINITE).setAction("GRANT",
new View.OnClickListener() {
@Override
public void onClick(View v) {
ActivityCompat.requestPermissions(DashBoardActivity.this, requestedPermissions, requestCode);
}
}).show();
} else {
ActivityCompat.requestPermissions(this, requestedPermissions, requestCode);
}
} else {
onPermissionsGranted(requestCode);
}
}
授予权限时调用此函数,您可以读取内部和外部存储
public void onPermissionsGranted(final int requestCode) {
Toast.makeText(this, "Permissions Received.", Toast.LENGTH_LONG).show();
}
以下用于检查请求的权限结果的代码
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
int permissionCheck = PackageManager.PERMISSION_GRANTED;
for (int permission : grantResults) {
permissionCheck = permissionCheck + permission;
}
if ((grantResults.length > 0) && permissionCheck == PackageManager.PERMISSION_GRANTED) {
onPermissionsGranted(requestCode);
} else {
Snackbar.make(findViewById(android.R.id.content), mErrorString.get(requestCode),
Snackbar.LENGTH_INDEFINITE).setAction("ENABLE",
new View.OnClickListener() {
@Override
public void onClick(View v) {
}
}).show();
}
}