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
Related
I want to create an application that picks the image from gallery automatically according to its name. The user needs to sign in a signaturepad that he can save on the device and when he clicks on the button to save the image, the app automatically picks this image from gallery and display it on an imageview. I created one where I need to pick the image manually (with a little bit of help), this is my code so far:
private void SelectImage()
{
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*"); //Can I define here the name of the image that I want to be selected?
startActivityForResult(intent, PICK_IMAGE_REQUEST);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && null != data){
imageUri = data.getData();
imageView1.setImageURI(imageUri);
}
}
I'm currently learning how to use android studio in java and am trying to make a social media app. I am currently creating an edit profile page where the user would update their details and upload a profile picture.
I have been following tutorials online and all of the ones I have come across use the startActivityForResult method. It has been crossed out and wont call the method as it is deprecated. But I don't know what to use instead.
`ProfileImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//open gallery
Intent OpenGalleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(OpenGalleryIntent, 1000);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #androidx.annotation.Nullable Intent data){
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1000){
if(resultCode == Activity.RESULT_OK){
Uri imageUri = data.getData();
ProfileImage.setImageURI(imageUri);
UploadImageToFirebase(imageUri);
}
}
}
private void UploadImageToFirebase(Uri image){
StorageReference fileRef = storageReference.child("Profile.jpg");
fileRef.putFile(image).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Toast.makeText(Edit_Profile.this, "Image Uploaded", Toast.LENGTH_SHORT).show();
}
});![enter image description here](https://i.stack.imgur.com/padRc.jpg)`
I know there is an alternative but I don't understand how it works.
startActivityForResult is indeed deprecated in later versions of AndroidX Activity and Fragment APIs (while I believe you can still use it despite of warning). New way to get result from activity is registerForActivityResult.
In your code you would need to create a launcher, which will handle result (selected image)
private final ActivityResultLauncher<Intent> launcher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
result -> {
if (result.getResultCode() == Activity.RESULT_OK
&& result.getData() != null) {
Uri photoUri = result.getData().getData();
//use photoUri here
}
}
);
and then launch this launcher in onClickListener
profileImage.setOnClickListener(view -> {
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
launcher.launch(intent);
});
Yes startActivityForeResult is deprecated.
Now you can use ActivityResultLauncher for the callbacks
https://developer.android.com/training/basics/intents/result#java
So whenever I select a picture from the gallery in my app, it crashes. Here is the code for the button to the gallery and selected picture to the imageview.
pickImageButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
openGallery();
}
});
}
private void openGallery() { //opens the gallery
Intent gallery =
new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(gallery, PICK_IMAGE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == PICK_IMAGE) {
Uri imageUri = data.getData();
imageView2.setImageURI(imageUri);
}
}
Please comment if you need more information, I desperately need help, as this is a very major roadblock for me.
Without the Logs, I can recommend on the following:
Make sure that pickImageButton isn't null.
Make sure that imageView2 isn't null.
Make sure that in manifest your Activity is in:
android:launchMode="singleTop"
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){
}
}
}
My button's onclick listener is as follows:
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
}
});
And the result is handled as follows:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_PIC_REQUEST) {
if (resultCode == Activity.RESULT_OK) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ImageView Preview = (ImageView) findViewById(R.id.PreviewImage1);
Preview.setImageBitmap(thumbnail);
Preview.setVisibility(View.VISIBLE);
}
}
}
I got the thumbnail working but how do I access the full image so that I can do some manipulations? I want to avoid saving the file if possible.
Here is a working example: http://achorniy.wordpress.com/2010/04/26/howto-launch-android-camera-using-intents/
Getting the full-sized image is not possible without saving to a file. Also it won't be a good idea, because having so big Bitmaps in memory will soon cause Out of memory exception.