Pick Gallery Image In Android Causing Issue - android

I am using a very simple code to pick image from gallery, it work on my phone.
but testing it on three to four phones(Galaxy S3,Tablet etc..) it does not work.
Environment It Works:
if the image size i took or was in gallery below 500kb then it works
Environment It Does'nt Work:
if the image size i took or was in gallery above 500kb then it works
ImageView myimg;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Toast.makeText(this, UUID.randomUUID().toString(),
// Toast.LENGTH_LONG).show();
myimg = (ImageView) findViewById(R.id.imageView1);
mybutton = (Button) findViewById(R.id.myButton);
mybutton.setOnClickListener(this);
}
public void onClick(View v) {
if (v == mybutton) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, 0);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode,
Intent 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
myimg.setImageBitmap(yourSelectedImage);
}
}
}
Any one put some light on it how to handle this situation?
Any help would be appreciated.

Related

Choose an image from gallery and show it using an ImageView

private static int RESULT_LOAD = 1;
String img_Decodable_Str;
ImageView imageView = (ImageView) findViewById(R.id.Gallery);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// Start the Intent
startActivityForResult(galleryIntent, RESULT_LOAD);
}
});}
#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 && 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]);
img_Decodable_Str = cursor.getString(columnIndex);
cursor.close();
ImageView imageView = (ImageView) findViewById(R.id.Gallery);
// Set the Image in ImageView after decoding the String
imageView.setImageBitmap(BitmapFactory
.decodeFile(img_Decodable_Str));
} else {
Toast.makeText(this, "Hey pick your image first",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(this, "Something went embrassing", Toast.LENGTH_LONG)
.show();
}
}}
I have an ImageView. If the user clicks on the ImageView he will be able to add an image of his choice to it. When i click on the ImageView I'm redirected to gallery but when i choose an image, that image isn't showing in the ImageView. where have i gone wrong?
In the short term, replace:
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]);
img_Decodable_Str = cursor.getString(columnIndex);
cursor.close();
ImageView imageView = (ImageView) findViewById(R.id.Gallery);
// Set the Image in ImageView after decoding the String
imageView.setImageBitmap(BitmapFactory
.decodeFile(img_Decodable_Str));
with:
Uri selectedImage = data.getData();
imageView.setImageURI(selectedImage);
Later, use an image-loading library, such as Picasso or Glide, as setImageURI() loads the image on the main application thread, which will freeze your UI for as long as that work takes.

how to set image in imageview from galary and camera? [duplicate]

This question already has answers here:
Allow user to select camera or gallery for image
(16 answers)
Closed 7 years ago.
i am new in android i dont know how to set imageview in image from galary and camera. please help me.. when i capure image from camera that image not set at imageview see below code
private static final int CAMERA_REQUEST = 1888;
ImageView imageView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera_capture);
imageView = (ImageView) this.findViewById(R.id.imageView1);
Button photoButton = (Button) this.findViewById(R.id.btncapture);
photoButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
}
You will get the Uri of the file in onActivityResult() you need to find find the path f from the ContentResolver and then create Bitmap.
For example-
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]);
picturePath = cursor.getString(columnIndex);
profileImage.setImageBitmap(BitmapFactory.decodeFile(picturePath));
cursor.close();

how to put my own image instead of mediastore.images.media.data?

How Can I put image from directory instead of load from media. I load from gallery like this
String[] filePathColumn = { MediaStore.Images.Media.DATA };. How can load from /assets or /drawable-mdpi ? This is my code. Kindly tell me how can i used this code to add image from my directory?
public class ImageGalleryDemoActivity extends Activity {
private static int RESULT_LOAD_IMAGE = 1;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture);
buttonLoadImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
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));
}
}
You can put the respective drawables in the android drawables folders and Android would automatically choose the required drawable based upon the device display density. You can set the drawables as
imageView = (ImageView)findViewById(R.id.imageView);
imageView.setImageDrawable(getResources().getDrawable(R.drawable.image));
You can also take help from here.

i need to access images from gallery and then access the pixels of the image for further processing in android

this is the code i'm using currently,the code only displays the image from gallery. but i want to access the pixels of the image.i am confused exactly where the image data is stored in the code as in which variable.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture);
buttonLoadImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
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));
}
}
}
Please refer this Answer on SO, here is described to get Pixels, you should use:
Bitmap bm = BitmapFactory.decodeFile(picturePath);
and then :
bm.getPixel(x,y);// will return an int that corresponds to an int in the Color class, such as Color.BLACK or Color.WHITE.
Hope this may help you

Image resolution or size issue gives 'Force Close' error

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.

Categories

Resources