Android - Dialog not refresing ImageView when getting image from gallery/ camera - android

So i´m working on an app in which I need to show a dialog, and inside this dialog, the user can press a button to select an image from the gallery or take a photo from the camera and display that image on an ImageView inside a dialog´s ImageView.
I´m able to take the photo from the camera/gallery, and then i use it as the ImageView image, but it doesn´t show it until i press the button again, as if the layout had to be refreshed.
This is my code:
To call the dialog (get_permissions(); is used on Android M for getting the write and read storage permissions, this works correctly)
protected void createDialog() {
final Dialog dialog = new Dialog(WeightActivity.this, android.R.style.Theme_Material_Light_Dialog_Alert);
dialog.getWindow();
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog_add_weight);
Button okButton = (Button) dialog.findViewById(R.id.okButton);
final ImageButton selectImageButton = (ImageButton) dialog.findViewById(R.id.selectImageButton);
final ImageView imageView = (ImageView) dialog.findViewById(R.id.imageView);
selectImageButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
get_permissions();
selectImage();
imageView.setImageBitmap(bm);
}
});
dialog.show();
}
private void selectImage() {
final String[] items = WeightActivity.this.getResources().getStringArray(R.array.photo_options);
AlertDialog.Builder builder = new AlertDialog.Builder(WeightActivity.this);
builder.setTitle(R.string.weight_dialog_add_photo);
builder.setItems(items, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int item) {
if (items[item].equals(items[0])) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_CAMERA);
} else if (items[item].equals(items[1])) {
Intent intent = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(
Intent.createChooser(intent, "Select File"),
SELECT_FILE);
}
}
});
builder.show();
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == REQUEST_CAMERA) {
bm = (Bitmap) data.getExtras().get("data");
} else if (requestCode == SELECT_FILE) {
Uri selectedImageUri = data.getData();
String[] projection = {MediaStore.MediaColumns.DATA};
CursorLoader cursorLoader = new CursorLoader(this, selectedImageUri, projection, null, null,
null);
Cursor cursor = cursorLoader.loadInBackground();
int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
cursor.moveToFirst();
String selectedImagePath = cursor.getString(column_index);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(selectedImagePath, options);
options.inJustDecodeBounds = false;
bm = BitmapFactory.decodeFile(selectedImagePath, options);
}
}
}
Could somebody help me please??
Thanks a lot!

The problem is this,
get_permissions();
selectImage();
imageView.setImageBitmap(bm);
selectImage() starts the activity, but it doesn't block; it returns immediately. You then call setImageBitmap(), probably on a null valued bm instance field.
Move, or otherwise trigger the imageView.setImageBitmap() call in onActivityResult(), only after you have decoded the image.

Related

Getting Black Screen when displaying Gallery picture into ImageView Android

#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_read_palm:
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
break;
case R.id.btn_read_from_existing:
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, RESULT_LOAD_IMG);
break;
}
}
Here getting selectedImageUri = content://media/external/images/media/2997
and
path = /storage/emulated/0/DCIM/Camera/IMG_20170510_132342860.jpg
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
switch (requestCode) {
case CAMERA_REQUEST:
Bitmap photo = (Bitmap) data.getExtras().get("data");
saveAndShowPictureDialog(photo);
break;
case RESULT_LOAD_IMG:
Uri selectedImageUri = data.getData();
String path = getRealPathFromURI(this, selectedImageUri);
if (path != null)
showImageDialog(path);
}
}
}
getRealPathFromURI function returning correct path.
public String getRealPathFromURI(Context context, Uri contentUri) {
Cursor cursor = null;
try {
String[] proj = {MediaStore.Images.Media.DATA};
cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
assert cursor != null;
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} finally {
if (cursor != null) {
cursor.close();
}
}
}
private void showImageDialog(String pictureFile) {
// custom dialog
final Dialog dialog = new Dialog(this, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
dialog.setContentView(R.layout.image_dialog);
dialog.setTitle("Image");
// find the imageview and draw it!
ImageView image = (ImageView) dialog.findViewById(R.id.image);
Bitmap bmImg = BitmapFactory.decodeFile(pictureFile);
here i am getting a black screen/image on imageview when setting Image Bitmap from the picture file.
image.setImageBitmap(bmImg);
Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
Try the following instead of Bitmap bmImg = BitmapFactory.decodeFile(pictureFile);?
Bitmap bmImg;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
bmImg = BitmapFactory.decodeFile(selectedImagePath, options);
image.setImageBitmap(bmImg);
ImageView's have a display resolution limit based on OpenGL. Normally this limit is about 2048 x 2048. If you go above that limit, the ImageView will just show black and throw no error.
This is particularly irritating with gallery images / camera images, since those can be very high resolution.
It's also irritating because the OpenGL limit can vary heavily based on the device. Therefore, the error is very hard to reproduce.
You can query the exact maximum resolution with something like this:
int[] maxSize = new int[1];
GLES10.glGetIntegerv(GL10.GL_MAX_TEXTURE_SIZE, maxSize, 0);
This answer goes into a little more detail about OpenGL and these limitations with regard to maximum width and height of a bitmap

How to Load Images From gallery by clicking the imageview Android

I have a Image view in my activity...Where I want to place a Text view("Click To load Images") on the Image View..When user Click the Empty Image View the gallery section want to be called from there he can select the picture and it want to be load on Imageview
How can we Do that??
Try this code ....
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Pick Image from")
.setPositiveButton("Camera", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//camera intent
Intent cameraIntent = new Intent(ConversationActivity.this, CameraActivity.class);
cameraIntent.putExtra("EXTRA_CONTACT_JID", contact.getJid());
startActivity(cameraIntent);
}
})
.setNegativeButton("Gallery", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent();
// 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);
}
});
AlertDialog alert = builder.create();
alert.show();
public class ImageGalleryActivity extends Activity {
ImageView imageView;
private static int RESULT_LOAD_IMAGE = 1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageView = (ImageView) findViewById(R.id.ImageViewId);
TextView LoadImage = (TextView) findViewById(R.id.TextViewId);
LoadImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent loadIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(loadIntent, RESULT_LOAD_IMAGE);
}
});
}
#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.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
}

pick an image from gallery in android studio?

someone can tell me what the problem, it is not working, so please help fast i really need:
imagePick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Contact Image"),1);
}
});
public void onActivityResult(int reqCode, int resCode, Intent data)
{
if(resCode==RESULT_OK)
{
if(reqCode==1) {
imageURI=data.getData();
iv.setImageURI(data.getData());
}
}
}
This is working for me.
private final static int SELECT_PHOTO = 12345;
imagePick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, SELECT_PHOTO);
}
});
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Here we need to check if the activity that was triggers was the Image Gallery.
// If it is the requestCode will match the LOAD_IMAGE_RESULTS value.
// If the resultCode is RESULT_OK and there is some data we know that an image was picked.
if (requestCode == SELECT_PHOTO && resultCode == RESULT_OK && data != null) {
// Let's read picked image data - its URI
Uri pickedImage = data.getData();
// Let's read picked image path using content resolver
String[] filePath = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(pickedImage, filePath, null, null, null);
cursor.moveToFirst();
String imagePath = cursor.getString(cursor.getColumnIndex(filePath[0]));
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeFile(imagePath, options);
imageView.setImageBitmap(bitmap);
// Do something with the bitmap
// At the end remember to close the cursor or you will end with the RuntimeException!
cursor.close();
}
}

Getting multiple images from Gallery

Right now I have an imageview. Clicking on the imageview I can get the option to select the gallery/camera intent.selecting on the required intent and the required picture I get the image in the imageview.This works fine for one single image.
How to get more than one picture.I mean the imageview[].Is there any code on this available?
Start Intent with EXTRA_ALLOW_MULTIPLE
Intent intent = new Intent( );
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
startActivityForResult(Intent.createChooser(intent,
"select multiple images"), PICK_IMAGE_MULTIPLE);
On receiving side
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == Activity.RESULT_OK){
if(requestCode == PICK_IMAGE_MULTIPLE){
String[] imagesPath = data.getStringExtra("data").split("\\|");
}
}
}
thanks for the response but here is my code.Now please let me know how to go ahead to have multiple images shown up in multiple imageviews.Also let me know if I need a placeholder like gridview for that?
ImageView image_view = new ImageView(this);
image_view.setId(field_id);
Uri selectedImage = Uri.parse(field_val);
String[] filePath = { MediaStore.Images.Media.DATA };
String picturePath;
Cursor c = getContentResolver().query(selectedImage,
filePath, null, null, null);
c.moveToFirst();
if(c.moveToFirst() && c.getCount() >= 1)
{
int columnIndex = c.getColumnIndex(filePath[0]);
picturePath = c.getString(columnIndex);
c.close();
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmapOptions.inSampleSize = 20;
Bitmap thumbnail = (BitmapFactory.decodeFile(
picturePath, bitmapOptions));
image_view.setImageBitmap(thumbnail);
image_view.setTag(selectedImage);
}
else {
image_view.setImageDrawable(getResources().getDrawable(
R.drawable.camera_launcher));
}
image_view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
getImage();
}
});
private void getImage() {
final CharSequence[] options = { "Take Photo", "Choose from Gallery",
"Cancel" };
AlertDialog.Builder builder = new AlertDialog.Builder(
this);
builder.setTitle("Add Photo!");
builder.setItems(options, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int item) {
if (options[item].equals("Take Photo")) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 1);
} else if (options[item].equals("Choose from Gallery")) {
Intent intent = new Intent(Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 2);
} else if (options[item].equals("Cancel")) {
dialog.dismiss();
}
}
});
builder.show();
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == 1) {
Bundle extras = data.getExtras();
try {
((ImageView) findViewById(R.id.imageid)).setImageBitmap((Bitmap) extras
.get("data"));
((ImageView) findViewById(R.id.imageid))).setTag(R.string.imgtag,
data.getDataString());
} catch (Exception e) {
e.printStackTrace();
}
} else if (requestCode == 2) {
Uri selectedImage = data.getData();
String[] filePath = { MediaStore.Images.Media.DATA };
String picturePath;
Cursor c = getContentResolver().query(selectedImage, filePath,
null, null, null);
c.moveToFirst();
int columnIndex = c.getColumnIndex(filePath[0]);
picturePath = c.getString(columnIndex);
c.close();
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmapOptions.inSampleSize = 20;
Bitmap thumbnail = (BitmapFactory.decodeFile(picturePath,
bitmapOptions));
((ImageView) findViewById(R.id.imageid))).setImageBitmap(thumbnail);
((ImageView) findViewById(R.id.imageid))).setTag(R.string.imgtag,
selectedImage.toString());
}
}
}

Saving Image path of pictures taken to Database

I'm new to android development. I am trying to create an app where when a user take a picture, it's path will be saved to the database. I am able to save and retrieve the image when it's from the gallery, But how do I save it if it's taken by camera?
Here's My Code
private void selectImage() {
final CharSequence[] items = { "Take Photo", "Choose from Library",
"Cancel" };
AlertDialog.Builder builder = new AlertDialog.Builder(Student_Profile.this);
builder.setTitle("Add Photo");
builder.setItems(items, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int item) {
if (items[item].equals("Take Photo")) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File f = new File(android.os.Environment
.getExternalStorageDirectory(), "temp.jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
startActivityForResult(intent, 1);
} else if (items[item].equals("Choose from Library")) {
Intent intent = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(
Intent.createChooser(intent, "Select File"),
0);
} else if (items[item].equals("Cancel")) {
dialog.dismiss();
}
}
});
builder.show();
}
#Override
public void onActivityResult( int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK){
if (requestCode == 0) {
studentsDbAdapter.delete_Pic_byID(studID);
Uri targetUri = data.getData();
picture_location=getPath(targetUri);
// picture_location = targetUri.toString();
studentsDbAdapter.insertImagePath(studID ,picture_location);
showpic();
} }
else if (requestCode == 1) {
// How do I get The URI?
// I'm thinking that maybe if I can get the Uri
//from the new image I can save it like how I saved the image to my DB gallery style.
String pic_location=getPath(Uri);
studentsDbAdapter.insertImagePath(studID ,pic_location);
showpic();
}
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
#SuppressWarnings("deprecation")
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
public void showpic() {
//LoginDataBaseAdapter db = studentsDbAdapter.open();
boolean emptytab = false;
boolean empty = studentsDbAdapter.checkPic(null, emptytab);
//Cursor cursor = loginDataBaseAdapter.fetchProfileImageFromDatabase();
if(empty==false)
{
String pathName = studentsDbAdapter.getImapath(studID);
File image = new File(pathName);
if(image.exists()){
ImageView imageView= (ImageView) findViewById(R.id.studpic);
imageView.setImageBitmap(BitmapFactory.decodeFile(image.getAbsolutePath()));
}
}
}
Try this. It may help you.
if (fromGallery) {
data.getData()); //get image data like this
} else {
bm = (Bitmap) data.getParcelableExtra("data");
}

Categories

Resources