Pass Bitmap data from one activity to another [duplicate] - java

This question already has answers here:
How can I pass a Bitmap object from one activity to another
(10 answers)
Closed 4 years ago.
My main activity contains the imageview and editactivity has a button for changing the image in imageview .
I used the Intent to startActivityForResult and onActivityResult code is shown below
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
Uri uri = data.getData();
try {
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
View mainactivity;
ImageView imageprofile;
mainactivity= LayoutInflater.from(this).inflate(R.layout.activity_main,null);
imageprofile = (ImageView)mainactivity.findViewById(R.id.ProfileImage);
imageprofile.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
The image choosen is not set , please help

You should not pass Bitmap to another activity.
Passing the entire bitmap requires a lot of memory
Pass the URI to another activity and load if needed. Hope it helps!

Related

Custom Gallery Image Picker

I would like to allow the user to pock only one image that can be referenced via a uri, I've successfully done this through the following code:
private static final int PICK_IMAGE_REQUEST = 1;
private Uri imageUri;
// Choose file extended from BottomTabView, opens all images on device
public static void openFileChooser(Context context) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
((Activity) context).startActivityForResult(intent, PICK_IMAGE_REQUEST);
// Slide Animation
((Activity) context).overridePendingTransition(R.anim.slide_in_up, R.anim.nothing);
}
// TODO: Is it better to use bitmap or URI
// Check if user has selected file and describe next step
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
// Retrieve image as a URI
imageUri = data.getData();
// Pass image URI to an intent and start activity
Intent intent = new Intent(this, UploadImageActivity.class);
intent.putExtra("imageUri", imageUri.toString());
startActivity(intent);
// Slide Animation
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
this.finish();
}
}
The above code opens the following:
However, I would like to have something like the following:
Question: How can I achieve something more like the "Custom Gallery"?
if you want to custom layout pick image, you need:
create a screen and using recyclerView to make the layout as you want
get all picture gallery (https://stackoverflow.com/a/25957752/10153377)

How to pick an image from gallery and save within the app after cropping? [duplicate]

This question already has answers here:
android:select image from gallery then crop that and show in an imageview
(3 answers)
android pick images from gallery
(19 answers)
Closed 4 years ago.
I am new to Android development and I tried my best to pick an image from gallery and saving within the app after cropping but I failed. Please help me to solve this issue. I tried to mix up different codes but nothing is working for me.
if (resultCode == RESULT_OK) {
//Uri photoUri = data.getData();
//if (photoUri != null) {
// photoPickerIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, uri);
CropImage.activity(android.net.Uri.parse(data.getDataString()))
.setAspectRatio(1,1)
.setFixAspectRatio(true)
.start(activity);
CropImage.ActivityResult result1 = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK) {
iv.setImageURI(result1.getUri());
} else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
Exception error = result1.getError();
Log.d(TAG, "onActivityResult: " + error.getMessage());
}
//currentImage = MediaStore.Images.Media.getBitmap(this.getContentResolver(), photoUri);
//selectedImage.setImageBitmap(currentImage);
// }
First of All Add a dependency inside your project Gradle.built(app:odle) file
Like
dependencies {
implementaion 'com.theartofdev.edmodo:android-image-cropper:2.8.+'
}
After that add following two permission inside manifiest file
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Create a Constant inside your class
private static final int REQUEST_FOR_GALLARY = 1;
you will need this .
After that Put the following Code inised your button on which u click to open gallery or whatever your are using .
Intent gallaryIntent = new Intent();
gallaryIntent.setAction(Intent.ACTION_GET_CONTENT);
gallaryIntent.setType("image/*");
startActivityForResult(gallaryIntent, REQUEST_FOR_GALLARY);
After that Override the medthod onActivityResult of your Activity class
Like
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent
data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_FOR_GALLARY && resultCode == RESULT_OK && data !=
null) {
Uri imageUri = data.getData();
CropImage.activity()
.setGuidelines(CropImageView.Guidelines.ON)
.start(this);
}
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK) {
Uri resultUri = result.getUri();
//Save image wherever you want to save it
}
}
}
Now Change your Code According to your Recuirement and Save image whereever you want to store .

Android: Multiple image view

I want two ImageView's on one activity view, One image for the profile picture and the other for the profile cover page.
My code
setupImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
bringImagePicker();
}
});
private void bringImagePicker() {
// start picker to get image for cropping and then use the image in cropping activity
CropImage.activity()
.setGuidelines(CropImageView.Guidelines.ON)
.setAspectRatio(1,1)
.start(SetupActivity.this);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK) {
mainImageURI = result.getUri();
setupImage.setImageURI(mainImageURI);
isChanged = true;
} else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
Exception error = result.getError();
}
}
}
Now how can I add for profile cover image?
You can use ID for both of them, and change the image with corresponding ID from the intent that holds data, add another condition after receiving the results.
I don't know if this library can handle this or not.
If not this simplest solution to hold state variable that tells you which image to update
feel free to ask for clarification

Image is not updating

Everything works fine except image updation! When I register a new user and when I click in the image, it opens my image gallery, I choose any image, and it back to register activity but it won't update. Also I'm unable to register because that image is not uploading successfully and we sat if imagepath == null, show that's why toast error is coming that fill all the details.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == PICK_IMAGE && requestCode == RESULT_OK && data.getData() !=null){
imagePath = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), imagePath);
userProfilePic.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
super.onActivityResult(requestCode, resultCode, data);
}
NOTE:
I tried multiple images, same issue.
There is no error in logcat.
Default image is showing perfectly when we first time try to register that time we sat android logo which is a default.
I can't register without selecting any image because image path == null.
No problem of image size.
Guys, any solution?

nullpointerexception when not taking photo

I have an app where at an activity I am taking a photo (among other things) .
Now, when I press the button to take the photo it opens the camera.If i will press the back button or the cancel button (not taking photo) ,it crashes and gives
nullpointer
and
Failure delivering result ResultInfo
in this line:
Bitmap photo = (Bitmap) data.getExtras().get("data");
I use:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == CAMERA_REQUEST){
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.PNG, 100, stream);
blobvalue = stream.toByteArray();
Bundle extras = new Bundle();
Intent k=new Intent(this,MainActivity.class);
extras.putParcelable("Bitmap", photo);
k.putExtras(extras);
}
if (requestCode == RESULT_CANCELED) {
}
}
and in my adapter:
ImageView myImage=(ImageView) convertView.findViewById(R.id.myimage);
final Bitmap image;
if(theItems.getImagemyItems() != null)
{
byte []temp = theItems.getImagemyItems();
image = BitmapFactory.decodeByteArray(temp, 0, temp.length);
myImage.setImageBitmap(image);
}
else
{
image = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher);
myImage.setImageBitmap(image);
}
As far as I remember , the above used to workd for this purpose.
I don't know what else to do.
You have just tested requestCode but haven't resultCode so I would suggest you to check resultCode whether user has captured image or cancel capturing.
Try:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == CAMERA_REQUEST){
if (resultCode == Activity.RESULT_OK) {
// Image captured and saved to fileUri specified in the Intent
}
else if (resultCode == Activity.RESULT_CANCELED) {
// User cancelled the image capture
} else {
// Image capture failed, advise user
}
}
You just have to place a check in your onActivityResult , the case RESULT_OK is when the user takes the picture successfully and the case RESULT_CANCELLED is when you press the hardware back button and want to return to your activity.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == CAMERA_REQUEST){
if(resultCode == RESULT_OK){
// your code comes here
}
if(resultCode == RESULT_CANCELED){
}
}
}

Categories