Large image Extra in Intent, cause black Screen in Android - android

I have this code to pick the images from gallery or camera:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case SELECT_PICTURE_ACTIVITY_REQUEST_CODE:
if (resultCode == RESULT_OK) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA,
MediaStore.Images.Media.DISPLAY_NAME };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
if (cursor.moveToFirst()) {
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
ext = filePath
.substring(filePath.lastIndexOf(".") + 1);
photod = BitmapFactory.decodeFile(filePath);
new AsyncTaskOne().execute(new String[] {});
(fileNameIndex);
}
}
break;
case SELECT_CAMERA_ACTIVITY_REQUEST_CODE:
if (requestCode == CAMERA_REQUEST) {
photod = (Bitmap) data.getExtras().get("data");
new AsyncTaskOne().execute(new String[] {});
}
}
}
When I press the confirm button I invoke this listener
OnClickListener confirm = new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent("com.striget.eu.UpdatePhoto");
i.putExtra(PropertiesUpdatedPhoto.EXTRA_PHOTO, codedPhoto);
i.putExtra(PropertiesUpdatedPhoto.EXTRA_EXT, ext);
startActivity(i);
}
};
(where codedPhoto is the image coded in base64 by another method)
to send the image and its extension in another activity
Everything works fine with small images but if I chose a medium size image or large photo(also if isn't very large), the app freezes, the screen becomes black and If I wait some minutes return on the current activity without showing any error in the stack and the invoked intent PropertiesUpdatedPhoto doesn't start.
How I could fix this problem?

This problem is discuss here, we should keep intent extra content as small as possible.
My suggestion is instead of passing image, perhaps passing the image URI or Path would be a better solution. Then only load the image in that Activity.
Example :
OnClickListener confirm = new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent("com.striget.eu.UpdatePhoto");
i.setData(PropertiesUpdatedPhoto.EXTRA_PHOTO, # Image URI Here # );
// Or i.putExtra(PropertiesUpdatePhoto.EXTRA_PHOTO, # Image Path Here #);
i.putExtra(PropertiesUpdatedPhoto.EXTRA_EXT, ext);
startActivity(i);
}
};

Related

Get image from camera without pressing ok button after camera click in android

I did code for capturing the image from camera and it works fine,
After capturing the image it is asking for click ok in camera but i want to get image without clicking on ok button. my code for is as below and i don't have idea to get image without clicking ok button so please help me.
button_camera.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent, 0);
}
});
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 0:
if (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]);
String filePath = cursor.getString(columnIndex);
cursor.close();
Log.e("PATH", filePath+"");
Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
}
}
};
On button click listerner write the following code
cameraBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
});
To achieve this, we should notify the camera intent to enable quick capture mode, while calling it. Below the code:
private static final int REQUEST_IMAGE_CAPTURE = 1;
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePictureIntent.putExtra("android.intent.extra.quickCapture", true); // enables single click image capture
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
Note: I learned from stackoverflow and few other sites people say some devices do not support this mode. And I am not sure what devices those are. To this date, I've tested on devices from different brands with API levels 21 to 28, which all has worked for me so far.

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.

How to have the option of having a user add an image in android

I am developing an android application for the first time and I was wondering how can I have a option of having the user upload an image. Like for example, in a contact manager the user has the option of uploading an image of a contact. I was wondering how can I do that in my android application. Any help would be appreciated.
You can start from scratch..
1)Take a look at INTENT
Android Intent
Then take a look at this blogpost
Image Upload
Hope this helps you
In this one you want to click the image view to get image from sd card.. If you want to change to button then replace the listener from image to button.
int RESULT_LOAD_IMAGE = 1;
image.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
getting image from sd card:
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();
image.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}

Pick Gallery Image In Android Causing Issue

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.

how to pick images from mobile gallery into an android application

how to pick images from gallery into an android application?
I need to browse images form my gallery and choose them for my app .how it can be done ?
You'll need to use an intent to the gallery (well, "a gallery").
You can find a code example here (I haven't done it but it looks like it will work):
http://www.androidsnippets.com/get-file-path-of-gallery-image
Use this Code:
fromGalleryButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//takePhotoFromGallery = true;// edited
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);//
startActivityForResult(Intent.createChooser(intent, "Select Picture"),10);
}
});
And then add this:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 10 && resultCode == Activity.RESULT_OK) {
Uri contentUri = data.getData();
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
imagePath = cursor.getString(column_index);
tempBitmap = BitmapFactory.decodeFile(imagePath); // this is your image
}
}
In Kotlin you can do this.
In onCreate method
binding.updatephoto.setOnClickListener {
contract.launch("image/*")
}
After onCreate
private val contract = registerForActivityResult(ActivityResultContracts.GetContent()){
it?.let {
val imageUri = it
}
}

Categories

Resources