Using ImageView to upload photo gallery

2

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

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

    
asked by anonymous 06.11.2014 / 01:17

1 answer

1

First step

Give read permission using READ_EXTERNAL_STORAGE for gallery photos in 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

Show photo in a ImageView by exploring the application lifecycle, using the onActivityResult() method.

@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!

    
05.10.2016 / 18:45