An imageview is not being updated with a photo taken in a camera activity on my Samsung 4S phone app. I save the Uri of the photo in a saveinstancestate and the photo is there on the camera but it does not update the imageview. Below is the code.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
Bitmap image1;
if(requestCode == RESULT_CROP_IMAGE)
{
Bundle extras = data.getExtras();
image1 = extras.getParcelable("data");
}
else {
if (requestCode == RESULT_LOAD_IMAGE) {
selectedImage = imageUri; // this gets the photo just taken
} else
selectedImage = data.getData(); // this gets an existing photo and it works
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
picturePath = cursor.getString(columnIndex);
cursor.close();
image1 = BitmapFactory.decodeFile(picturePath);
}
displayphoto = Bitmap.createScaledBitmap(image1, newWidth, newHeight, true);
ImageView imageView = (ImageView) findViewById(R.id.myImage);
imageView.setEnabled(true);
imageView.setImageBitmap(displayphoto);
Button cropPhoto = (Button)findViewById(R.id.cropPhoto);
cropPhoto.setVisibility(View.VISIBLE);
Not only is the imageView not showing the image, the cropPhoto button does not appear either. In debug mode I can see that the instructions are being executed and I examined the dimensions of the returned photo which are correct but nothing shows up. However, if I get an existing photo from my camera, everything works so I know it has something to do with losing global variables. I know that the instancestate has to be retrieved for certain objects when taking a photo and I do that in the code below so I know my imageUri is returned properly.
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putParcelable("myURI", imageUri);
savedInstanceState.putString("myPath", mCurrentPhotoPath);
super.onSaveInstanceState(savedInstanceState);
}
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
imageUri = savedInstanceState.getParcelable("myURI");
mCurrentPhotoPath = savedInstanceState.getString("myPath");
}
So what is missing here? Why does not my image or my button show up? Does it have something to do with losing global variables when taking a photo? Also, I should mention that every once in awhile it works and the image appears. I don't understand why it normally doesn't appear.
Related
I have a app where i allow the user to pick a picture from galery and it use as PROFILE PICTURE, pick a picture and set in a "ImageView" in my app.
The problem is that when the app is closed, ou the activity is changed the picture desapear, or back de default picture again, i want save this picture state for when back to activity or when close and re-open the app the picture continue there and dont need set all over again.
I am new in developing, please if you can help me looking my code below and give make the needed changes and give me the ready code i will be really deeply thankful cause i spent several days for do it and still i cant. I need a ready code cause i am new in developing and if you try explain something i will not understand.
Here is my code where i pick the picture:
public class MainActivity extends Activity {
private static int RESULT_LOAD_IMG = 1;
String imgDecodableString;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void loadImagefromGallery(View view) {
// Create intent to Open Image applications like Gallery, Google Photos
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// Start the Intent
startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
// When an Image is picked
if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
&& null != data) {
// Get the Image from data
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
// Get the cursor
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
// Move to first row
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imgDecodableString = cursor.getString(columnIndex);
cursor.close();
ImageView imgView = (ImageView) findViewById(R.id.imgView);
// Set the Image in ImageView after decoding the String
imgView.setImageBitmap(BitmapFactory
.decodeFile(imgDecodableString));
} else {
Toast.makeText(this, "You haven't picked Image",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
.show();
}
}
}
Try overriding the onSaveInstanceState Activity lifecycle methods. Here's a blog post regarding its best practices.
http://inthecheesefactory.com/blog/fragment-state-saving-best-practices/en
I have just started in developing in Android. So far I am learning stuff. I am stuck with this problem. In my onCreate method, i have a button to select an image in the device and the onActivityResult to get the image and display the image in an ImageView
After researching and looking for answers in the internet, someone advice that I am missing the onSaveInstanceState. so I created it. Here is my code.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button buttonLoadImage = (Button)findViewById(R.id.buttonLoadPicture);
targetImage = (ImageView) findViewById(R.id.imgView);
buttonLoadImage.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, RESULT_LOAD_IMG);
}
});
}
#Override
public void onSaveInstanceState(Bundle savedInstanceState){
savedInstanceState.putParcelable("BitmapImage", bitmap);
super.onSaveInstanceState(savedInstanceState);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState){
image = savedInstanceState.getParcelable("BitmapImage");
targetImage.setImageBitmap(image);
}
#Override
protected void onActivityResult( int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK){
Uri targetUri = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(targetUri, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imgDecodableString = cursor.getString(columnIndex);
cursor.close();
this.bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(targetUri));
targetImage.setImageBitmap(bitmap);
}
}
catch (FileNotFoundException e){
e.printStackTrace();
}
}
After building and running the code, selecting image and displaying it in ImageView works fine. When i change the orientation, image selected is still displayed. However when i rotate back again, the image displayed is gone.
Where am i doing wrong. Any advice and help is appreciated.
Are you storing 'bitmap' correctly? Here you are storing it to a local variable:
Bitmap bitmap;
bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(targetUri));
targetImage.setImageBitmap(bitmap);
And here you are using a field:
savedInstanceState.putParcelable("BitmapImage", bitmap);
Looks like the field isn't being set so the onSaveInstanceState is storing a null value. Try removing Bitmap bitmap; or change bitmap = BitmapFactory... to this.bitmap = BitmapFactory.
EDIT:
Also try storing bitmap here:
image = savedInstanceState.getParcelable("BitmapImage");
targetImage.setImageBitmap(image);
By changing it to:
image = savedInstanceState.getParcelable("BitmapImage");
this.bitmap = image;
targetImage.setImageBitmap(this.bitmap);
I'm not sure what image is though as you haven't include the delcaration in the post.
I want to show the image that taken by user gallery to a canvas to edit it after.but till now i can get image from user.but i dont know how to show the image by canvas
this is my code :
public class second extends ActionBarActivity {
private static int RESULT_LOAD_IMAGE = 1;
Bitmap base;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, 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 imageView = (ImageView) findViewById(R.id.imgView);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
}
i know to creat a canvas with Canvas canvas= new Canvas(image) and draw the bitmap by ondraw method.
but still i dont figure out how to convert the photo that given from users gallery to canvas and show it
shouldn't something like this work:
imageView.setImage(yourImage);
imageView.notify();
sry that i had to post thisn as answers, but i dont have enough repfor a simple comment :/
I have 2 imageView to import images from gallery and set on these imageViews. I basically do this by:
String mPicPath1, mPicPath2;
protected void onCreate(Bundle icicle) {
mPicPath1 = null;
mPicPath2 = null;
}
#Override
protected void onActivityResult(int requestCode, int resultcode, Intent data){
super.onActivityResult(requestCode, resultcode, data);
switch(requestCode){
case 1:
if (data != null && resultcode == RESULT_OK)
{
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]);
mPicPath1 = cursor.getString(columnIndex);
cursor.close();
logoview.setBackgroundResource(0);
logoview.setImageBitmap(BitmapFactory.decodeFile(mPicPath1));
}
break;
case 2:
if (data != null && resultcode == RESULT_OK)
{
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
cursor.moveToFirst();
mPicPath2 = cursor.getString(columnIndex);
cursor.close();
qrcodeview.setBackgroundResource(0);
qrcodeview.setImageBitmap(BitmapFactory.decodeFile(mPicPath2));
}
break;
}
and i use a button onClickListener to start intent and go to SecondActivity:
save=(Button)findViewById(R.id.save);
save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(NewCard.this, Template.class);
if (!TextUtils.isEmpty(mPicPath1)) {
intent.putExtra("picture_path1", PicPath1);
}
if (!TextUtils.isEmpty(mPicPath2)) {
intent.putExtra("picture_path2", PicPath2);
}
startActivity(intent);
}
});
And my SecondActivity to set images on 2 different imageViews:
String pre_img_path1= getIntent().getStringExtra("picture_path1");
ImageView crdlogoframe = (ImageView) findViewById(R.id.crdlogoframe);
crdlogoframe.setImageBitmap(BitmapFactory.decodeFile(pre_img_path1));
String pre_img_path2= getIntent().getStringExtra("picture_path2");
ImageView crdqrframe = (ImageView) findViewById(R.id.crdqrframe);
crdqrframe.setImageBitmap(BitmapFactory.decodeFile(pre_img_path2));
So my problem is about the file size or resolution of the images. If i take 2 high resolution images from gallery (taken by standard camera: 1992kb, 3264x2448) and i click my save (save.onClickListener) button, i receive Force Close error. If i take small size images there is no problem(74kb, 800x600) i can proceed SecondActivity and see images are set. How i can solve this issue. Should i use a syntax to resize the images when i pick or set. The formats are both .Jpeg. Thank you very much.
i do not have the rep to comment yet so .... but as Simon said your images are to large 31MB need to try and keep things under about 16MB so you will have to re-size them before you can display them
Instead of
BitmapFactory.decodeFile(mPicPath2)
You need to use something like
BitmapFactory.decodeFile(mPicPath2, options)
Where options is an instance of BitmapFactory.Options with an inSampleSize set to something like 4 that will scale down the image as it is loaded.
I am picking image from gallery and showing image on ImageView. I am able to do it successfully. My problem is when i try to set an image that are captured from camera(.jpg images) they are not shown in imageview just a blank screen comes up. Even images of type .jpg are successfully showed that are not captured from camera. Can't understand the reason, may be due to larger size. Help please
Following is my code:
public class ImagePick extends Activity {
private Bitmap bitmap;
private ImageView imv;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_upload);
imv = (ImageView) findViewById(R.id.targetimage);
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, 0);
}
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch(requestCode) {
case 0:
if(resultCode == RESULT_OK){
Uri selectedImage = imageReturnedIntent.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 filePath = cursor.getString(columnIndex); // file path of selected image
cursor.close();
// Convert file path into bitmap image using below line.
Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
// put bitmapimage in your imageview
imv.setImageBitmap(yourSelectedImage);
}
}
}
}
Thanks in Advance
you need to use FileInputStream and BufferedInputStream to read the image then you convert the image into bitmap.
private FileInputStream is = null;
private BufferedInputStream bis = null;
is = new FileInputStream(new File(imagePath));
bis = new BufferedInputStream(is);
also check the link
Thanks
Instead of:
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex); // file path of selected image
Try:
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
And close the cursor After the BitmapDecoding.
Remember that the access to the SD card should be done in AsynckTasks. And you can resize the image to fit the screen, that way you won't use a lot of memory, take a look at my other answer Bitmap Out Of Memory Issues. on how to do this.