Placing An Picture In Imageview Selected From Gallery
I want to select an image from gallery and place that selected image in the ImageView. I have also an Intent that will take picture through camera and place it in ImageView and it
Solution 1:
Below is the working solution of choosing a picture from library or capturing one from camera and using it in a imageview.
Source : SO
//functions to select image from the deviceprivatevoidselectImage() {
final CharSequence[] items = {"Take Photo","Choose from Library", "Cancel" };
AlertDialog.Builderbuilder=newAlertDialog.Builder(signature_new.this);
builder.setTitle("Add Photo!");
builder.setItems(items, newDialogInterface.OnClickListener() {
@OverridepublicvoidonClick(DialogInterface dialog, int item) {
if (items[item].equals("Take Photo")) {
Intentintent=newIntent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_CAMERA);
} elseif (items[item].equals("Choose from Library")) {
Intentintent=newIntent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(
Intent.createChooser(intent, "Select File"),
SELECT_FILE);
} elseif (items[item].equals("Cancel")) {
dialog.dismiss();
}
}
});
builder.show();
}
@OverrideprotectedvoidonActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == REQUEST_CAMERA) {
Bitmapthumbnail= (Bitmap) data.getExtras().get("data");
ByteArrayOutputStreambytes=newByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
Filedestination=newFile(Environment.getExternalStorageDirectory(),
System.currentTimeMillis() + ".jpg");
FileOutputStream fo;
try {
destination.createNewFile();
fo = newFileOutputStream(destination);
fo.write(bytes.toByteArray());
fo.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
imageF.setImage(ImageSource.bitmap(thumbnail));
} elseif (requestCode == SELECT_FILE) {
UriselectedImageUri= data.getData();
try {
Bitmap bm=decodeUri(selectedImageUri);
imageViewF.setImage(ImageSource.bitmap(bm));
//uploadbm=bm;//dialog_dimension();
}
catch(FileNotFoundException e) {
e.printStackTrace();
}
}
}
}
Solution 2:
this is the intent to open gallery
IntentGallery_intent=newIntent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(Gallery_intent, constants.RESULT_LOAD_IMAGE);
and this is on activity result
@OverrideprotectedvoidonActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == constants.RESULT_LOAD_IMAGE && resultCode == RESULT_OK && data != null) {
Filedir= Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
BitmapFactory.Optionsoptions=newBitmapFactory.Options();
options.inJustDecodeBounds = false;
options.inPreferredConfig = Bitmap.Config.RGB_565;
b = BitmapFactory.decodeFile(getRealPathFromURI(data.getData()), options);
intbmpHeight= b.getHeight();
intbmpWidth= b.getWidth();
if (bmpHeight > 1500) {
bmpHeight = bmpHeight / 4;
bmpWidth = bmpWidth / 4;
}
Bitmapout= Bitmap.createScaledBitmap(b, bmpWidth, bmpHeight, false);
Filefile=newFile(dir, "resize.png");
FileOutputStream fOut;
try {
fOut = newFileOutputStream(file);
out.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
b.recycle();
out.recycle();
} catch (Exception e) {
}
ImageView.setImageURI(Uri.fromFile(file));
}
Solution 3:
Correcting your code... your conditions are quite wrong in onActivityResult
privatevoidopenGallery() {
tvGallery.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View view) {
Toast.makeText(getApplicationContext(), "Opening Gallery, Please wait..", Toast.LENGTH_SHORT).show();
Intent intent = newIntent();
// Show only images, no videos or anything else
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
// Always show the chooser (if there are multiple options available)startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}
});
}
Your OnActivityResult
@OverrideprotectedvoidonActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
Uriuri= data.getData();
try {
Bitmapbitmap= MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
// Log.d(TAG, String.valueOf(bitmap));
previewView = (ImageView) findViewById(R.id.imgPostIssue);
previewView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
ImageViewpreviewView= (ImageView) findViewById(R.id.imgPostIssue);
previewView.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Solution 4:
How to place an image into
ImageView
from Gallery
private int PICK_IMAGE_REQUEST = 1;
privatevoidopenGallery() {
tvGallery.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View view) {
Intent intent = newIntent();
// Show only images, no videos or anything else
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
// Always show the chooser (if there are multiple options available)startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}
});
}
OnActivityResult
@OverrideprotectedvoidonActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stubsuper.onActivityResult(requestCode, resultCode, data);
/*camera preview*/if (resultCode != RESULT_CANCELED) {
if (requestCode == 0) {
Bitmapbp= (Bitmap) data.getExtras().get("data");
previewView.setImageBitmap(bp);
}
/*gallery preview*/else {
Uriuri= data.getData();
try {
Bitmapbitmap= MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
Log.d(TAG, String.valueOf(bitmap));
ImageViewpreviewView= (ImageView) findViewById(R.id.imgPostIssue);
previewView.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Post a Comment for "Placing An Picture In Imageview Selected From Gallery"