每当我试图从Intent获取uri时,只需从图像选择器中选择一个图像,它在这里不起作用就是我的代码。请尽快回答
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.share) {
Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
i.setType("image/*");
startActivityForResult(i, 1);
} else if (id == R.id.logOut) {
ParseUser.logOut();
Intent i = new Intent(this, MainActivity.class);
startActivity(i);
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == Activity.RESULT_OK && data!=null) {
try {
Uri selectedImage = data.getData();
// this is the line which is causing error
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
Toast.makeText(getApplication().getBaseContext(),"test",Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
}
}
}
这是日志: I /错误:java.lang.SecurityException:Permission Denial:从pid = 5506读取com.android.providers.media.MediaProvider uri content:// media / external / images / media / 27,uid = 10043需要android.permission。 READ_EXTERNAL_STORAGE或grantUriPermission()
已经给出的许可是: uses-permission android:name =“android.permission.READ_EXTERNAL_STORAGE”
注意:不能在此处使用适当的标签,因为stackover flow给出错误
清单文件:
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="Share Pics"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data
android:name="com.parse.APPLICATION_ID"
android:value="X6I00RcSXvAnvvV8iaR6ftEsHo2o7sHXmzkZlb03" />
<meta-data
android:name="com.parse.CLIENT_KEY"
android:value="MyKnxnrPXRan1z56fpSmkxPx7hgcM44NfvzdC4q5" />
<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=".UserList" />
<activity
android:name=".UsersFeed"
android:label="@string/title_activity_users_feed"
android:parentActivityName=".UserList"
android:theme="@style/AppTheme.NoActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.anshuman.myinstagram.UserList"/>``
</activity>
</application>
答案 0 :(得分:0)
将此添加到您的清单:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.pc.myapplication" >
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
...
如果您使用的是模拟器,请检查以下链接:https://developer.android.com/training/permissions/requesting.html 并检查此代码:
// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.READ_CONTACTS)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
Manifest.permission.READ_CONTACTS)) {
// Show an expanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.READ_CONTACTS},
MY_PERMISSIONS_REQUEST_READ_CONTACTS);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}