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.
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.
Give read permission using READ_EXTERNAL_STORAGE
for gallery photos in manifest.xml
:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Create a simple intention
Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
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!