Application terminate when cropping Image - java

I am having an Application which captures an image and send the ImageUri to Crop Intent for cropping. Some of the photos are getting cropped properly. When there is a change in resolution Application(i.e. Photo taken using front camera with different resolution or Uploading a downloaded images) is getting terminated automatically without any error.
Code to open Camera
fabcamera.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Open Camera and upload the photo
Intent cameraIntent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent,CAMERA_CAPTURE);
}
});
private void performCrop(Uri picUri){
Intent cropIntent = new Intent("com.android.camera.action.CROP");
cropIntent.setDataAndType(picUri, "image/*");
cropIntent.putExtra("crop", "true");
cropIntent.putExtra("aspectX", 0);
cropIntent.putExtra("aspectY", 0);
cropIntent.putExtra("outputX", 0);
cropIntent.putExtra("outputY", 0);
cropIntent.putExtra("return-data", true);
startActivityForResult(cropIntent, PIC_CROP);
}
fabgallery.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Open Gallery and upload the photo
Intent intent=new Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(intent.createChooser(intent,"Select File"),SELECT_FILE);
}
});
Can anyone give me a suggestion?

Related

how to replace the startActivityForResult because its now deprecated this is the last piece i need to finish up my project

how to replace it with ActivityResultLauncher.
sorry guys i am just new to coding and programming.
SelectImageGallery.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Image From Gallery"), 1);
}
});
You need to register for activity result outside of onCreate using below Kotlin code
val getContent = registerForActivityResult(ActivityResultContracts.GetContent()) { uri: Uri? ->
// Callback after selecting image
// Do whatever you want to do with uri
}
}
}
Now instead of startActivityForResult(..) call below method when you want to open gallery to pick image
getContent.launch("image/*")
SelectImageGallery.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE)
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
result.launch(Intent.createChooser(intent, "Select Image From Gallery"));
}
});
INSIDE SAME METHOD AS LISTENER:
ActivityResultLauncher<String> result = registerForActivityResult(
new ActivityResultContracts.GetContent(),
new ActivityResultCallback<Uri>() {
#Override
public void onActivityResult(Uri result) {
//DO whatever with received image content scheme Uri
}
});

Why does the Camera Intent not returning the image to the ImageView?

What I am trying to achieve is on the map long press it brings up and custom dialog view with 3 buttons, one for photo, one for save and one for cancel.
So at the moment when tapping on photo the camera intent opens and I can take a photo. Upon clicking "ok" the intent returns to the custom dialog window but no image is displayed?
This is my code I am using at the moment:
public void onMapLongClick(LatLng point) {
LayoutInflater factory = LayoutInflater.from(MainActivity.this);
final View deleteDialogView = factory.inflate(R.layout.custom_dialog, null);
final AlertDialog deleteDialog = new AlertDialog.Builder(MainActivity.this).create();
deleteDialog.setView(deleteDialogView);
deleteDialogView.findViewById(R.id.btn_photo).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
String f = System.currentTimeMillis()+".jpg"; // Designated name
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), f);
fileUri = FileProvider.getUriForFile(MainActivity.this, getPackageName() + ".fileprovider", file);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(cameraIntent, TAKE_PICTURE);
}
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
switch (requestCode) {
case TAKE_PICTURE:
ImageView imgView = findViewById(R.id.a);
imgView.setImageURI(fileUri);
break;
}
}
});
Notice that you pass 'fileUri' to the intent, so you also need to get it from the returned intent.
Try replacing imgView.setImageURI(fileUri);
with imgView.setImageURI(data.getData());
or
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
which will give you the URI with the image.

Android. Crop images from Gallery

In my app i'm trying to crop images. I find some code that can crop images when a'm taking photo from camera, but i can't crop photos from gallery, cause:
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=0, data=null} to activity {....MainActivity}: java.lang.NullPointerException "
in this line: Bundle extras = data.getExtras();
This is myActivity's code:
public class MainActivity extends Activity {
ImageView imVCature_pic;
Button btnCapture;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeControls();
}
private void initializeControls() {
imVCature_pic=(ImageView)findViewById(R.id.imVCature_pic);
btnCapture=(Button)findViewById(R.id.btnCapture);
Button buttonGallery = (Button) findViewById(R.id.btn_select_gallery);
btnCapture.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
/*create instance of File with name img.jpg*/
File file = new File(Environment.getExternalStorageDirectory() + File.separator + "img.jpg");
/*put uri as extra in intent object*/
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
/*start activity for result pass intent as argument and request code */
startActivityForResult(intent, 1);
}
});
buttonGallery.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent2 = new Intent();
intent2.setType("image/*");
intent2.setAction(Intent.ACTION_GET_CONTENT);
File file2 = new File(Environment.getExternalStorageDirectory() + File.separator + "img2.jpg");
/*put uri as extra in intent object*/
intent2.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file2));
/*start activity for result pass intent as argument and request code */
startActivityForResult(intent2, 3);
}} );
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//if request code is same we pass as argument in startActivityForResult
if(requestCode==1){
//create instance of File with same name we created before to get image from storage
File file = new File(Environment.getExternalStorageDirectory()+File.separator + "img.jpg");
//Crop the captured image using an other intent
try {
/*the user's device may not support cropping*/
cropCapturedImage(Uri.fromFile(file));
}
catch(ActivityNotFoundException aNFE){
//display an error message if user device doesn't support
String errorMessage = "Sorry - your device doesn't support the crop action!";
Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
}
}
if(requestCode==2){
//Create an instance of bundle and get the returned data
Bundle extras = data.getExtras();
//get the cropped bitmap from extras
Bitmap thePic = extras.getParcelable("data");
//set image bitmap to image view
imVCature_pic.setImageBitmap(thePic);
}
if(requestCode==3){
//create instance of File with same name we created before to get image from storage
File file = new File(Environment.getExternalStorageDirectory()+File.separator + "img.jpg");
//Crop the captured image using an other intent
try {
/*the user's device may not support cropping*/
cropCapturedImage(Uri.fromFile(file));
}
catch(ActivityNotFoundException aNFE){
//display an error message if user device doesn't support
String errorMessage = "Sorry - your device doesn't support the crop action!";
Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
}
}
}
//create helping method cropCapturedImage(Uri picUri)
public void cropCapturedImage(Uri picUri){
//call the standard crop action intent
Intent cropIntent = new Intent("com.android.camera.action.CROP");
//indicate image type and Uri of image
cropIntent.setDataAndType(picUri, "image/*");
//set crop properties
cropIntent.putExtra("crop", "true");
//indicate aspect of desired crop
cropIntent.putExtra("aspectX", 256);
cropIntent.putExtra("aspectY", 256);
//indicate output X and Y
cropIntent.putExtra("outputX", 256);
cropIntent.putExtra("outputY", 256);
//retrieve data on return
cropIntent.putExtra("return-data", true);
//start the activity - we handle returning in onActivityResult
startActivityForResult(cropIntent, 2);
}
Where's a mistake?

Unable To Open Images and Video Using onActivityResult - Edited / Updated

I'm trying to accomplish a very simple task:
I need to be able to click the imageButton for video, select from gallery and play, and click the imageButton for images, select an image and view it - ALL USING THE GALLERY
How might this be accomplished?
I believe I'll need to modify my onActivityResult since it does not currently perform as expected.
Current Source:
ImageButton pb = (ImageButton) findViewById(R.id.photos);
pb.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, SELECT_PHOTO);
}
});
ImageButton vb = (ImageButton) findViewById(R.id.video);
vb.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
.show();
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("video/*");
startActivityForResult(intent, SELECT_VIDEO);
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if (resultCode == Activity.RESULT_OK) {
Uri selectedVideo = data.getData();
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(selectedVideo, "video/*, image/*");
startActivity(Intent.createChooser(intent,
"Complete action using"));
}
}
;
}
}
Edit:
I have updated my source to reflect the most recent answer - however I am still unable to open images - I continue to get the "unable to play video" when attmempting to view an image.
checkout this for video:
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("video/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Video"), SELECT_VIDEO);
and this for images:
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Images"), SELECT_PHOTO);
and in onActivityResult try this :
if (requestCode==SELECT_VIDEO) {
//take whatever video action
}else if(requestCode==SELECT_PHOTO){
//take whatever image action
}

Building a Camera App - Receive

I am new to Android programming and I'm writing an application in Java that opens the camera take a photo and save it. I made it via Intents but I can't see onActivityResult running.
I have tested it into my phone (Samsung Galaxy S) and when I take the photo I receive a preview of that photo having two buttons one Save and the other Cancel. I haven't added something to my code to do this so I think it's something that camera does. I want after capturing the image to run onActivityResult (after I press the Save button on the preview).
But how I'm going to return a result to start onActivityResult after pressing the Button Save on the preview?
I FORGOT to tell that after i press save my entire app is terminated.
Here is my Code
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TakePicButton = (Button) findViewById(R.id.TakePicture);
TakePicButton.setOnClickListener((android.view.View.OnClickListener) this);
}
#Override
public void onDestroy(){
super.onDestroy();
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// Image captured and saved to fileUri specified in the Intent
Toast.makeText(this, "Image saved to:\n" + data.getData(), Toast.LENGTH_LONG).show();
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT);
} else {
Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT);
}
}
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId() == R.id.TakePicture){
// create Intent to take a picture and return control to the calling application
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
// start the image capture Intent
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
}
try the below code, you will have to modify it a bit, it will help you get From Library and From Camera both, the SELECT_PICTURE is used for getting image from library
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case SELECT_PICTURE:
Uri selectedImageUri = data.getData();
filemanagerstring = selectedImageUri.getPath();
selectedImagePath = getPath(selectedImageUri);
if (selectedImagePath != null)
myFile = new File(selectedImagePath);
else if (filemanagerstring != null)
myFile = new File(filemanagerstring);
if (myFile != null) {
Bitmap bmp_fromGallery = decodeImageFile(selectedImagePath);
break;
case CAMERA_REQUEST:
Bitmap bmp_Camera = (Bitmap) data.getExtras().get("data");
break;
default:
break;
}
}

Categories

Resources