Using Imagemview to upload gallery photo

Asked

Viewed 2,284 times

3

I want to make a program that I upload a photo to a ImagemView from the Android gallery.

I need a code to open the gallery and save the image as if it were a profile.

  • Did you find any solution? poste as a reply to help other users!

  • http://tjkannan.blogspot.in/2012/01/load-image-from-camera-or-gallery.html http://www.coderzheaven.com/2012/04/20/select-an-image-from-gallery-in-android-and-show-it-in-an-imageview/ Check the examples of these links and see if it helps you as well as other developers who want to do the same.

  • Did you solve your problem with the answer? Or do you need some more information?

1 answer

2

First step

Give read permission using READ_EXTERNAL_STORAGE for gallery photos on manifest.xml:

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

Second step

Create a simple intention

Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);

Third step

Display photo on in a ImageView exploring the application life cycle using the onActivityResult().

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };
        Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
        cursor.moveToFirst();
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();
        ImageView imageView = (ImageView) findViewById(R.id.iv);
        imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
    }
}

Good luck!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.