I want to be able to track image file names when a picture has been taken with the default Camera Glassware. This is so I can delete them when finished. I have the following code:
...
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, TAKE_PICTURE_REQUEST);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == TAKE_PICTURE_REQUEST && resultCode == RESULT_OK) {
String imgPath = data.getStringExtra(Intents.EXTRA_PICTURE_FILE_PATH);
mService.addToImageQueue(imgPath);
}
else if (requestCode == TAKE_PICTURE_REQUEST && resultCode == RESULT_CANCELED) {
...
}
}
If I tap, the resultCode returns RESULT_OK. When I dismiss (swipe down), I get the resultCode RESULT_CANCELED. This is how I intended it to work, except it still generates the image file even if the resultCode is RESULT_CANCELED... I honestly feel like this might be a bug since I tried to use data.getStringExtra(Intents.EXTRA_PICTURE_FILE_PATH); and got a NullPointerException. Am I doing something wrong? Is there a way to get this file name even on RESULT_CANCELED?
You could create a temporary file first (look at the createImageFile() method in this tutorial). If successfully created, do two things:
Save the path of this file to a String.
Include this file's URI in the intent extra (putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile))).
If resultCode is RESULT_CANCELED, you can now trace back to the path of the temporary file and call delete() on it.
Here is some sample code:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
Log.v("MainActivity", "Result successful.");
} else if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_CANCELED) {
Log.v(TAG, "Result canceled. Uri of file is " + mCurrentPhotoPath);
File file = new File(mCurrentPhotoPath);
if (file.exists()) {
Log.v(TAG, "File exists.");
if(file.delete()) {
Log.v(TAG, "File was successfully deleted!");
} else {
Log.v(TAG, "File not successfully deleted.");
}
} else {
Log.v(TAG, "File does not exist!");
}
}
}
Note: For new File(mCurrentPhotoPath) to work, remove "file:" from the beginning of mCurrentPhotoPath.
Related
I want to croping image from other activity to another activity with Canhub Android Image Cropper library. This is my code :
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable 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) {
mCropImageUri = result.getOriginalUri();
createImage();
} else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
AppLogger.e(result.getError().getMessage());
}
}
if (requestCode == PermissionCheckUtils.LOCATION_PERMISSION_REQUEST_CODE && resultCode == RESULT_OK) {
getLocation();
}
}
and this when i access the camera :
private void openCropImage() {
Intent intent = CropImage.activity().setGuidelines(CropImageView.Guidelines.ON).getIntent(this);
startActivityForResult(intent, CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE);
}
with these code, i open camera but source include galery. My question is how to open the croper with source just from camera. I already read the documentation but I'm confused : https://github.com/CanHub/Android-Image-Cropper
It is very simple !!
In Your openCropImage() Function set following code :
Intent intent = CropImage
.activity()
.setImageSource(includeGallery = false, includeCamera = true)
.setGuidelines(CropImageView.Guidelines.ON)
.getIntent(this);
startActivityForResult(intent, CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE);
That's it !! Happy Coding :-)
I have an app that allows the user to select a file from the file chooser. The problem lies when I try to turn that Uri to a File, it creates something that I can't use (/document/raw:/storage/emulated/0/Download/CBTJourney-Backup/EntriesBackup1570487830108) I would like to get rid of everything before raw: but the right way. Where ever I try to copy from that file using InputStream, it doesn't copy anything. It's like the file doesn't exist. Any ideas?
public void chooseDatabaseFile() {
Intent intent = new Intent();
intent.addCategory(Intent.CATEGORY_OPENABLE);
// Set your required file type
intent.setType("*/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Choose Database to Import"),GET_FILE_PATH);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GET_FILE_PATH && data != null) {
if(resultCode == RESULT_OK){
Uri currFileURI = data.getData();
if(currFileURI == null) {
return;
}
else{
String databasePath = currFileURI.getPath();
// TODO: Determine if actual database
importDatabase(new File(databasePath));
// File produces "/document/raw:/storage/emulated/0/Download/CBTJourney-Backup/EntriesBackup1570487830108"
}
}
}
}
Have you tried changing your mimetype?
Otherwise take a look at:
Convert file: Uri to File in Android
I'm trying to access the image gallery and I have these code. Content Resolver turned red in the fragment. I've been trying tweaks, but still it isn't solved.
public void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1 && resultCode == RESULT_OK && data!= null && data.getData() != null){
Uri filepath = data.getData();
bitmap = (Bitmap) MediaStore.Images.Media.getBitmap(getContentResolver, filepath);
}
}
Try these variations:
getActivity().getApplicationContext().getContentResolver()
or simply
getActivity().getContentResolver()
I am using the library https://github.com/Yalantis/uCrop provided here.
I want to open gallery intent directly when a button is clicked with crop option facility but how do I do it??
Currently, am doing this
public void onclickbutton (View view) {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
if (checkPermissionREAD_EXTERNAL_STORAGE(this)) {
startActivityForResult(intent, GALLERY_INTENT);
}
}
in Onactivity result
if (requestcode == GALLERY_INTENT && resultcode == RESULT_OK)
{
uri = data.getData();
UCrop.of(uri, uri)
.withAspectRatio(16, 9)
.withMaxResultSize(500, 500)
.start(this);
}
Again in onactivity result
if (resultcode == RESULT_OK && requestcode == UCrop.REQUEST_CROP) {
final Uri resultUri = UCrop.getOutput(data);
Toast.makeText(this, resultUri.toString(), Toast.LENGTH_SHORT).show();
} else if (resultcode == UCrop.RESULT_ERROR) {
final Throwable cropError = UCrop.getError(data);
}
Please help me where I am going wrong..
You need to change your Onactivity result
you are passing same uri you must pass source uri and destination uri. like shown below
UCrop.of(sourceUri, destinationUri)
.withAspectRatio(16, 9)
.withMaxResultSize(maxWidth, maxHeight)
.start(context);
Ok so this here is the intent I am sending
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
startActivityForResult(intent, REQUEST_CODE);
And then in the onActivityResult I am doing this:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.i("Intent name:",data.toString());
if (requestCode == REQUEST_CODE){
if (resultCode == Activity.RESULT_OK){
Toast.makeText(this, "Image saved to \n" + fileUri.toString() , Toast.LENGTH_LONG).show();
Toast.makeText(this, "Result Code: " + resultCode , Toast.LENGTH_LONG).show();
//Bitmap mBitMap = BitmapFactory.decodeFile(data.getData().toString());
//imageView.setImageBitmap(mBitMap);
}
else if (resultCode == RESULT_CANCELED){
Toast.makeText(this, "Capture Cancelled", Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(this, "Capture failed", Toast.LENGTH_LONG).show();
}
}
super.onActivityResult(requestCode, resultCode, data);
}
The LogCat is showing a NullPointerException at the line that says Image Saved....
And also this:
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=null}
This happens whether i try to use the data object or the fileUri field of my class.
Why is data being returned null?
Why is it that even though I am using a field of the class i still get the same error?
Whenever you save an image by passing EXTRAOUTPUT with camera intent ie
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
in a file, the data parameter inside the onActivityResult always return null. So, instead of using data to retrieve the image , use the filepath to retrieve the Bitmap.
So onActivityResult would be something like this:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE) {
if (resultCode == Activity.RESULT_OK) {
String[] fileColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(imageUri,
fileColumn, null, null, null);
String contentPath = null;
if (cursor.moveToFirst()) {
contentPath = cursor.getString(cursor
.getColumnIndex(fileColumn[0]));
Bitmap bmp = BitmapFactory.decodeFile(contentPath);
ImageView img = (ImageView) findViewById(R.id.imageView1);
img.setImageBitmap(bmp);
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "Capture Cancelled", Toast.LENGTH_LONG)
.show();
} else {
Toast.makeText(this, "Capture failed", Toast.LENGTH_LONG)
.show();
}
}
super.onActivityResult(requestCode, resultCode, data);
}
Make sure that you have taken imageUri or fileUri as a global variable so that you can access it inside onActivityResult as well. Best of luck
The correct/preferred way to handle data in these cases would be as:
In called Activity set data to the Intent , then setResult code as RESULT_OK and then finish that activity.
In this recieving activity , check the result code.. and retrieve data from Intent variable as :intent.getExtra("... "); //The variables which you have set in the child activity that has been closed now..