-1

whenever i try to get uri from Intent which just select an image from image picker its not working here is my code. Please answer ASAP

@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();
            }

        }
}

and this is logs: I/Error: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/media/27 from pid=5506, uid=10043 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()

permission already given are: uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"

Note:can't use proper tags for uses-permisson here because stackover flow giving error

Manifest file:

<?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>

Anshuman Kaushik
  • 91
  • 1
  • 1
  • 9

1 Answers1

0

Add this to your manifest:

<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"/>
...

if you are using an emulator check this link: https://developer.android.com/training/permissions/requesting.html and check this code:

// 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.
    }
}
Fabio Venturi Pastor
  • 2,519
  • 3
  • 19
  • 32