I added the permission on the manifest.xml
But I still having the same error:
Permission Denial: reading com.android.providers.media.MediaProvider
Help me please.
My java class
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);
}
}
}
My activity
<?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>
My code is not working Thank you
Add external read/write permission in Manifest file
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Try this, Put this code in onCreate right after setContentView and put the remaining code of onCreate in else part:
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);
}
}
out side onCreate write :
@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);
}
}
}
I also had same issue , Have provided permissions as below in manifext.xml
**<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>**
Thanks Krish
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.