Cropping camera intent in Android 4.4 - java

How can I crop camera intent in Android 4.4?
I can crop camera intent below 4.4 with
Intent pictureActionIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion < 19) {
pictureActionIntent.putExtra("crop", "true");
pictureActionIntent.putExtra("outputX", 100);
pictureActionIntent.putExtra("outputY", 120);
pictureActionIntent.putExtra("aspectX", 1);
pictureActionIntent.putExtra("aspectY", 1);
pictureActionIntent.putExtra("scale", true);
pictureActionIntent.putExtra("return-data", true);
} else {
}
startActivityForResult(pictureActionIntent, 1);
but in 4.4 it's not working on else portion.

There are several open source libraries for cropping images in Android.
Intent action from an app that may not exist on any given user’s device.
Here are some libraries to consider:
https://github.com/biokys/cropimage
https://github.com/MMP-forTour/cropimage (forked from the above one)

Related

Android - Cordova plugin changing functionality to force torch on when recording

I'm adjusting the Java of a Cordova plugin which records video fine, but the client has requested to force the torch on, but what I have attempted isn't working, if someone can steer me in the right direction:
private void captureVideo(int duration, boolean highquality, boolean frontcamera) {
Intent intent = new Intent(android.provider.MediaStore.ACTION_VIDEO_CAPTURE);
String videoUri = getVideoContentUriFromFilePath(this.cordova.getActivity(), getTempDirectoryPath());
intent.putExtra(MediaStore.EXTRA_OUTPUT, videoUri);
intent.putExtra("android.intent.extra.videoQuality", 0);
// Added this line
intent.putExtra("android.intent.extra.FLASH_MODE_ON", 1);
if (Build.VERSION.SDK_INT > 7) {
intent.putExtra("android.intent.extra.durationLimit", duration);
}
this.cordova.startActivityForResult(this, intent, CAPTURE_VIDEO);
}

Camera intent is not working properly in android version 11

I have try some following solution to captured image on android version 11. But this solution are not working. when I use bitmap that time I get blur image this Is not visible properly.I have added the below code in the manifest.
android:requestLegacyExternalStorage="true" ` Add this top stored image in external storage`
<queries>
<intent>`
<action android:name="android.media.action.IMAGE_CAPTURE" />
</intent>
</queries>
// add code in class call image Intent
public static Intent getPickImageIntent(Context context) {
mContext = context;
Intent chooserIntent = null;
List<Intent> intentList = new ArrayList<>();
Intent pickIntent = new Intent(Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// takePhotoIntent.putExtra("return-data", true);
// takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(getTempFile(context)));
intentList = addIntentsToList(context, intentList, pickIntent);
intentList = addIntentsToList(context, intentList, takePhotoIntent);
if (intentList.size() > 0) {
chooserIntent = Intent.createChooser(intentList.remove(intentList.size() - 1),
context.getString(R.string.pick_image_intent_text));
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentList.toArray(new Parcelable[]{}));
}
return chooserIntent;
}
when I add temp file path then this is not working in above API level 30
// pass image uri to activity set image in imageview
public static Uri getImageFromResultUri(Context context, int resultCode,
Intent imageReturnedIntent) {
File imageFile = getTempFile(context);
Uri selectedImage = null;
int sdkVersion = Build.VERSION.SDK_INT;
if (resultCode == Activity.RESULT_OK) {
boolean isCamera = (imageReturnedIntent == null ||
imageReturnedIntent.getData() == null ||
imageReturnedIntent.getData().toString().contains(imageFile.toString()));
if (isCamera) { /** CAMERA **/
// selectedImage = Uri.fromFile(imageFile);
Bitmap photo = (Bitmap) imageReturnedIntent.getExtras().get("data");
selectedImage = getImageUri(context,photo);
} else { /** ALBUM **/
selectedImage = imageReturnedIntent.getData();
}
}
return selectedImage;
}
when I convert Bitmap image to URI
public static Uri getImageUri(Context mContext, Bitmap inImage){
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG,100,bytes);
String path = MediaStore.Images.Media.insertImage(mContext.getContentResolver(),inImage,"Title",null);
return Uri.parse(path);
}
when I convert the image bitmap to URI I get a thumbnail so this is a blur so how can I get an image in android version 11 without using a bitmap. And I don't what to store this image in the gallery. Image is getting blur in every device.
When I use takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(getTempFile(context))); this code then this is working properly in below version 11. but how can I use same code for android version 11
Uri.fromFile(getTempFile(context)));
You should use FileProvider and FileProvider.getUriForFile() instead to provide a valid uri to camera intent.
Also for versions below Android 11;
which is your target API version? requestLegacyExternalStorage will work only when targetting at most Android 10, when targetting Android 11 you have to use Scoped Storage, thus implement support for this feature. without this implementation you won't be able to access files outside your private app folder on Android 11+ devices

Android app lags while a big file is downloaded

I'm using Fast-Android-Networking library in my android app to download file through it.
For files less than 15 megabytes, my code works fine. But, when I try downloading files with more than 20 megabytes of length, the application and also the target device lags.
When I tested with my old Lenovo A319 which has 512mb of RAM, I've found the problem and thought it must be an hardware problem.
But, after testing the application in Lenovo A6000, Samsung J1 4G, Motorola Moto G Turbo and LYF Water 11, I came to this decision that the application is lagging on all devices and only while the file is being downloaded.
I can't understand why this problem is happening. I've also checked the logcat but found nothing that can help me to understand the root of the problem.
Any idea?
Some of my code :
AndroidNetworking.download(link, Environment.getExternalStorageDirectory().getPath() + "/ABCD", title + "."+fileExtension)
.setTag("Download: " + title)
.setPriority(Priority.HIGH)
.build()
.setDownloadProgressListener(new DownloadProgressListener() {
#Override
public void onProgress(long bytesDownloaded, long totalBytes) {
int percentage = (int) Math.floor(bytesDownloaded * 100.0 / totalBytes);
System.out.println(percentage);
//I'm using Notification to report progress to user.
mBuilder.setProgress(100, percentage, false);
mNotifyManager.notify(id, mBuilder.build());
}
})
.startDownload(new DownloadListener() {
#Override
public void onDownloadComplete() {
if (file.length() < 500000) {
Snackbar.make(mView, "Download Error", Snackbar.LENGTH_LONG).show();
mBuilder.setContentText("Download Error!").setProgress(0, 0, false);
mNotifyManager.notify(id, mBuilder.build());
} else {
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setDataAndType(path, getMimeType(path.toString()));
PendingIntent notifyPIntent = PendingIntent.getActivity(mContext.getApplicationContext(), 0, intent, 0);
mBuilder.setContentIntent(notifyPIntent);
mBuilder.setContentTitle("Download Completed")
.setContentText("Download complete! - Click To Play")
.setProgress(0, 0, false);
mNotifyManager.notify(id, mBuilder.build());
Snackbar.make(mView, "Download Completed : " + title + "."+fileExtension, Snackbar.LENGTH_LONG).setAction("Play Now", new View.OnClickListener() {
#Override
public void onClick(View v) {
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setDataAndType(path, getMimeType(path.toString()));
mContext.startActivity(intent);
}
}).show();
}
}
#Override
public void onError(ANError error) {
Snackbar.make(mView, "Download Error : " + error.toString(), Snackbar.LENGTH_LONG).show();
error.printStackTrace();
mBuilder.setContentText("Download Error!");
mBuilder.setProgress(0, 0, false);
mNotifyManager.notify(id, mBuilder.build());
}
});
Based on this issue, library has critical problems in memory usage area.

How I could call the native viewer on android? Or implements Zoom and rotate the Image?

I have the image's path and I'd like to caLL the native viewer from android, because the user want's the option of zoom and rotate the image. I have to make it by hard or I can call an native Intent?
To call the native viwer :
Intent i = new Intent(Intent.ACTION_VIEW);
i.setDataAndType(Uri.fromFile(new File(path)), "image/jpg");
PackageManager packageManager = getPackageManager();
List<ResolveInfo> activities = packageManager.queryIntentActivities(i, 0);
boolean isIntentSafe = activities.size() > 0;
// Start an activity if it's safe
if (isIntentSafe) {
startActivity(i);
}

Android image cropping is reducing the size of the image

I need to crop a photo from the android phone gallery and then edit it.
The original size of the photo is 3264 *2448 which is displayed in my screen of size 1280 * 720 as a scaled down image.
When I crop it, i am getting a image of size 185 * 139.
The code I am using for cropping is
Intent cropIntent = new Intent("com.android.camera.action.CROP");
cropIntent.setDataAndType(picUri, "image/*");
cropIntent.putExtra("crop", "true");
cropIntent.putExtra("return-data", true);
startActivityForResult(cropIntent, PIC_CROP);
When I display the cropped image in an imageView it is displayed as a much smaller image.
I should be cropping the original sized image and not the scaled down version. Could any of you please guide me as to how to do this?
Please try this, i know its too late, but may help someone.
private void performCrop() {
try {
//call the standard crop action intent (the user device may not support it)
Intent cropIntent = new Intent("com.android.camera.action.CROP");
//indicate image type and Uri
cropIntent.setDataAndType(mImageCaptureUri, "image/*");
//set crop properties
cropIntent.putExtra("crop", "true");
//indicate aspect of desired crop
cropIntent.putExtra("aspectX", 4);
cropIntent.putExtra("aspectY", 3);
//indicate output X and Y
cropIntent.putExtra("outputX", 800);
cropIntent.putExtra("outputY", 800);
File f = new File(Environment.getExternalStorageDirectory(),
"/temporary_holder.jpg");
try {
f.createNewFile();
} catch (IOException ex) {
Log.e("io", ex.getMessage());
}
uri = Uri.fromFile(f);
cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(cropIntent, PIC_CROP);
} //respond to users whose devices do not support the crop action
catch (ActivityNotFoundException anfe) {
//display an error message
String errorMessage = "Your device doesn't support the crop action!";
Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
}
}
Make your onActivityResult like this :-
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PIC_CROP) {
String filePath = Environment.getExternalStorageDirectory()
+ "/temporary_holder.jpg";
thumbnail = BitmapFactory.decodeFile(filePath);
//thumbnail = BitmapFactory.decodeFile(filePath);
// Log.i("",String.valueOf(thumbnail.getHeight()));
ImageView image = (ImageView) findViewById(R.id.pestImage);
image.setImageBitmap(thumbnail);
}
}
Add:
intent.putExtra(MediaStore.EXTRA_OUTPUT, tempUri);
Then you can get an image file which is not reduced. And use this saved file instead of:
Intent.getExtras().getParcelable("data")
Just ignore returned bitmap.
First off you should be aware that the crop intent cannot be relied upon for cross-device compatibility. (Any OEM can replace the stock camera/gallery app with their own version). This aside, I think you are missing a few extras that should go in the intent and give you your desired effect.
// width & height are your desired width/height for the cropped result
cropIntent.putExtra("outputX", width);
cropIntent.putExtra("outputY", height);
cropIntent.putExtra("aspectX", width);
cropIntent.putExtra("aspectY", height);
cropIntent.putExtra("scaleUpIfNeeded", true);
Try adding those to your intent and see how it turns out.

Categories

Resources