我在manifest.xml上添加了权限
但我仍然有同样的错误:
权限拒绝:阅读com.android.providers.media.MediaProvider
请帮帮我。
我的java类
package com.example.maymaa.mytest;
+import..
public class Image extends AppCompatActivity {
private static final int PICK_IMAGE =100;
Uri imageUri;
ImageView imageview1;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image);
imageview1=(ImageView)findViewById(R.id.imageview1);
button=(Button)findViewById(R.id.btphoto);
assert button != null;
button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
openGallery();
}}
);}
private void openGallery()
{
Intent gallery= new Intent
(Intent.ACTION_PICK,MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(gallery, PICK_IMAGE);
}
@Override
protected void onActivityResult
(int requestcode,int resultcode,Inten data)
{
super.onActivityResult(requestcode,resultcode,data);
if(resultcode == RESULT_OK && requestcode==PICK_IMAGE){
imageUri =data.getData();
imageview1.setImageURI(imageUri);
}
}
}
我的活动
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:padding="10dp"
android:weightSum="1">
<ImageView
android:layout_width="361dp"
android:layout_height="255dp"
android:id="@+id/imageview1"
android:layout_weight="0.14" />
<Button
android:layout_width="180dp"
android:layout_height="wrap_content"
android:text="Choisir"
android:layout_gravity="center_horizontal"
android:onClick="btnclick"
android:id="@+id/btphoto"
/>
</LinearLayout>
我的代码无效 谢谢
答案 0 :(得分:0)
在清单文件中添加外部读/写权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
答案 1 :(得分:0)
试试这个,在setContentView之后将此代码放在onCreate中,并将onCreate的剩余代码放在else部分中:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
int hasWritePermission = checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE);
int hasReadPermission = checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE);
List<String> permissions = new ArrayList<String>();
if (hasWritePermission != PackageManager.PERMISSION_GRANTED) {
permissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
} else {
preferencesUtility.setString("storage", "true");
}
if (hasReadPermission != PackageManager.PERMISSION_GRANTED) {
permissions.add(Manifest.permission.READ_EXTERNAL_STORAGE);
} else {
preferencesUtility.setString("storage", "true");
}
if (!permissions.isEmpty()) {
requestPermissions(permissions.toArray(new String[permissions.size()]), REQUEST_CODE_SOME_FEATURES_PERMISSIONS);
}
}
外面onCreate写道:
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case REQUEST_CODE_SOME_FEATURES_PERMISSIONS: {
for (int i = 0; i < permissions.length; i++) {
if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
System.out.println("Permissions --> " + "Permission Granted: " + permissions[i]);
} else if (grantResults[i] == PackageManager.PERMISSION_DENIED) {
System.out.println("Permissions --> " + "Permission Denied: " + permissions[i]);
}
}
}
break;
default: {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
}
答案 2 :(得分:0)
我也有同样的问题, 已经在manifext.xml中提供了以下权限
.slider
由于 Krish