0

I'm a starter of Android and I'm doing a simple app that is able to upload the image the the server and I couldn't load out the image from the gallery onto the ivImage image view. Here is my code and below of it is the errors.

public class Home extends AppCompatActivity implements View.OnClickListener{
private static final int RESULT_LOAD_IMAGE = 1;

ImageView ivUpload, ivGallery, ivImage;
EditText etItemdesc,etItemname;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);

    ivImage = (ImageView) findViewById(R.id.ivImage);
    ivUpload = (ImageView) findViewById(R.id.ivUpload);
    ivGallery = (ImageView) findViewById(R.id.ivGallery);
    etItemdesc = (EditText) findViewById(R.id.etItemdesc);
    etItemname = (EditText) findViewById(R.id.etItemname);

    ivGallery.setOnClickListener(this);
    ivUpload.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    switch(v.getId()){
        case R.id.ivGallery:
            Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(galleryIntent,RESULT_LOAD_IMAGE);
            break;
        case R.id.ivUpload:

            break;
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == RESULT_LOAD_IMAGE && resultCode ==RESULT_OK && data!= null){
        Uri selectedImage = data.getData();
        ivImage.setImageURI(selectedImage);   //error is on this row
    }
}
}

here are the errors

Unable to open content: content://media/external/images/media/42
         java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/media/42 from pid=3098, uid=10058 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
             at android.os.Parcel.readException(Parcel.java:1599)
             at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
             at android.database.DatabaseUtils.readExceptionWithFileNotFoundExceptionFromParcel(DatabaseUtils.java:146)
             at android.content.ContentProviderProxy.openTypedAssetFile(ContentProviderNative.java:692)
             at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1104)
             at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:942)
             at android.content.ContentResolver.openInputStream(ContentResolver.java:662)
             at android.widget.ImageView.resolveUri(ImageView.java:832)
             at android.widget.ImageView.setImageURI(ImageView.java:449)
             at chengweifeng1132701116.fyp.Home.onActivityResult(Home.java:52)
             at android.app.Activity.dispatchActivityResult(Activity.java:6428)
             at android.app.ActivityThread.deliverResults(ActivityThread.java:3695)
             at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742)
             at android.app.ActivityThread.-wrap16(ActivityThread.java)
             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)
             at android.os.Handler.dispatchMessage(Handler.java:102)
             at android.os.Looper.loop(Looper.java:148)
             at android.app.ActivityThread.main(ActivityThread.java:5417)
             at java.lang.reflect.Method.invoke(Native Method)
             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
  • 1
    You need to add `READ_EXTERNAL_STORAGE` to your manifest, using a `` element. And, if your `targetSdkVersion` is 23 or higher, you need to [implement runtime permissions](https://developer.android.com/training/permissions/requesting.html) for this permission. – CommonsWare Dec 06 '16 at 17:37
  • Possible duplicate of [Storage permission error in Marshmallow](http://stackoverflow.com/questions/33162152/storage-permission-error-in-marshmallow) – Gokhan Arik Dec 06 '16 at 17:39
  • 1
    You should really just use `ACTION_GET_CONTENT`. That does not require the storage permission (unlike `ACTION_PICK` and using `MediaStore`) – ianhanniballake Dec 06 '16 at 18:06
  • thanks dude that worked – Cheng Wei Feng Dec 07 '16 at 10:49

1 Answers1

0

you forgot to add this permission :

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

and if you are testing on api20+ make sure to add run-time permitting. see the documentation here : https://developer.android.com/training/permissions/requesting.html

msdev16
  • 201
  • 3
  • 11
  • where do i implement the run-time permitting? on the cases or on the onActivityResult() function? – Cheng Wei Feng Dec 06 '16 at 17:52
  • Follow the link for more details ... you have to request permissions if it's not granted ... and run the Intent.ACTION_PICK code in onRequestPermissionsResult() if permissions was granted. – msdev16 Dec 06 '16 at 17:57
  • so if i've already got the READ_EXTERNAL_STORAGE permissions, i'll just add in onRequestPermissionsResult()? or i have to request for permissions eitherway – Cheng Wei Feng Dec 06 '16 at 18:00