I am trying to CROP a photo in android, but my temp-photo file always empty(0kb) although I had picked a photo from my gallery.
This is my code :
File dir = new File(Environment.getExternalStorageDirectory(), "pictures/softtime");//save path
if (!dir.exists()) {
dir.mkdirs();
}
currentImageFile = new File(dir, System.currentTimeMillis()+".jpg");//path+filename
//Create if not exists
if (!currentImageFile.exists()) {
try {
currentImageFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
Intent intentGet = new Intent("android.intent.action.GET_CONTENT");
intentGet.setType("image/*");
intentGet.putExtra("crop",true);
intentGet.putExtra("scale",true);
intentGet.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(currentImageFile));
startActivityForResult(intentGet,CROP);
I have run this code at my phone , I found that There is a new file created after I pick a photo ,but the new file is empty (0kb).
what can i do ?
Thank you everyone for help.
There is no requirement for ACTION_GET_CONTENT activities to support some undocumented crop extra. If you want to allow the user to crop an image, use an image cropping library.
Related
I'm creating an image filter app in Android studio. first, the user selects an image from gallery and it will be displayed in imageview. Then the user clicks edit button and that image is displayed in imageview of next activity where we can add filters... It works fine with low resolution images but when I select any high resolution image it is shown in first imageview but when I click edit button either the app crashes or the last image I had selected is displayed.I searched for the solution but couldn't find it. If anyone knows how to solve this problem please help me
There is a limit to the size of data that can be passed through an intent. The limit is roughly 500Kb - your high resolution photographs will be larger than this.
Consider saving your image to a file location on the device, passing the URI to the receiving activity and loading it within there.
first paste crash logs.
then instead of passing image itself just pass image path.
or simply add the edit tools and mainView in one activity and make edit tools invisible! however you can use fragment too.
use with putExtra to send the Uri Path:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent .setClass(ThisActivity.this, NewActivity.class);
intent .putExtra("KEY", Uri);
startActivity(intent );
You just need to add path of image.
It's better to save the image in storage and pass the Uri of location instead of passing the image.
Save image in storage:-
public static Uri saveImageOnExternalStorage(Bitmap capturedBitmap, String imageId) {
if (null != capturedBitmap ) {
OutputStream fOutputStream;
String path = Environment.getExternalStorageDirectory().toString();
File file = new File(path + "temp", mediaId + ".png");
file.delete();
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
try {
if (file.createNewFile()) {
fOutputStream = new FileOutputStream(file);
capturedBitmap.compress(COMPRESS_FORMAT, 100, fOutputStream);
fOutputStream.flush();
fOutputStream.close();
return Uri.fromFile(file); // return saved image path on external storage
}
} catch (FileNotFoundException e) {
Log.e(TAG,e.getMessage());
return null;
} catch (IOException e) {
e.printStackTrace();
Log.e(TAG,e.getMessage());
}
}
return null;
}
Now the same Uri you can pass in the intent of next activity:-
Intent intent = new Intent(CurrentActivity.this, LaunchActivity.class);
intent .putExtra("image_key", Uri);
startActivity(intent );
I'm currently trying to save images taken from a phone to its gallery, but the code below only works if I choose the stock camera app when the chooser dialog pops up. Whenever I choose another camera app(e.g., the Google camera), the taken picture doesn't get saved any where.
To make things even stranger, sometimes the picture does show up in its designated directory in the gallery, but after 15 mins or so, the same goes for when I use the stock camera app: the picture will get saved in the default camera shots directory, but takes quite a bit to show up in its designated directory, if it shows up there at all.
// Capturing Camera Image will launch camera app request image capture
void captureImage() {
//file uri to store image.
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
// Request camera app to capture image
Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
// start the image capture Intent
getActivity().startActivityForResult(captureIntent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
}
well ,
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
does not work anymore .
you should do something like this :
call Camera Activity :
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
and onActivityResult :
if (data.getData() == null) {
Bitmap bm = (Bitmap)
data.getExtras().get("data");
String timeStamp = new SimpleDateFormat(
"yyyyMMdd_HHmmss").format(new Date());
File pictureFile = new File(Environment
.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES)
.getAbsolutePath()
+ File.separator + "IMG_" + timeStamp);
try {
FileOutputStream fos = new FileOutputStream(
pictureFile);
bm.compress(Bitmap.CompressFormat.JPEG, 90, fos);
fos.close();
String filePath = pictureFile.getAbsolutePath();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} } else {
Uri imgUri =data.getData());
}
It turns out my code was working after all. The pictures were being saved in the new directory, but the problem was that the gallery wasn't being updated, which explains why the photos would randomly appear in the directory later on. Being new to this, it never occurred to me that I would have to update the gallery. I only came to this realization after using ES File Explorer to look through my files. To fix my problem, I just made a new method in my CameraFragment that would call on the media scanner. I called this method from onActivityResult().
Here's the new method, though there's nothing really "new" about it since I ran into the same code on other SO questions:
protected void mediaScan() {
getActivity().sendBroadcast(
new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
Uri.parse(fileUri.toString())));
}
I also don't need to call the package manager and iterate through the apps that could handle the camera intent if I'm not giving the option to use choose a picture from a gallery, so I'm going to remove all that from my question.
I'm a problem when I using the MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA in the Intent. The camera starts correctly but it doesn't save the files in my specific folder "/photo". But when I use the MediaStore.ACTION_IMAGE_CAPTURE it works fine, but I can't use this because it take only one photo each time.
I need the camera starts and the user takes many photos. After he closes the camera and all photos are saved in my specific folder.
Thanks for your help.
Regards,
Marcelo
Source code:
public void startCamera() {
Intent takePictureIntent = new Intent(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA);
File file = null;
try {
file = createImageFile();
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
} catch (IOException e) {
file = null;
Log.e(this.getClass().getName(), e.getMessage(), e);
}
activity.startActivity(takePictureIntent);
}
private File createImageFile() throws IOException {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = JPEG_FILE_PREFIX + timeStamp + JPEG_FILE_SUFFIX;
return new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/photo/", imageFileName);
}
MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA This intent does not support activity results or specific intent file outputs. This intent is designed to simply open the camera. The functionality you seek does not exist natively in Android.
I am trying to invoke the default device camera from my application to take picture using intent android.provider.MediaStore.ACTION_IMAGE_CAPTURE. Everything seems to be working fine except for when i click and save a particular image it gets stored at two location
1. At the default camera location.
2. At the location which i am passing with intent.
I just want the 2nd option to happen and not the 1st one. I thought creating a .nomedia file in used defined location would make sure that the picture would not be listed as part of gallery but later i found that the pic is getting stored at both the location.
My relevant portion of code is:
In the Activity:
Intent intentCamera = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
try {
tempDir = Environment.getExternalStorageDirectory();
// place where to store camera taken picture
photo = createTemporaryFile("picture", ".jpg", tempDir);
photo.delete();
} catch (Exception e) {
Toast.makeText(this, "Please check SD card! Error in fetching image",
Toast.LENGTH_SHORT).show();
}
if (photo != null) {
Uri mImageUri = Uri.fromFile(photo);
Log.i(TAG, "" + mImageUri.toString());
intentCamera.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri);
}
startActivityForResult(intentCamera, CAMERA_PIC_REQUEST);
The corresponding method.
public File createTemporaryFile(String part, String ext,File tempDir) throws Exception {
tempDir = new File(tempDir.getAbsolutePath() +"/temp/");
if (!tempDir.exists()) {
tempDir.mkdir();
}
createNoMediaFile(tempDir.getAbsolutePath());
return File.createTempFile(part, ext, tempDir);
}
Is there a way i can avoid 1 i.e. saving the image to the default gallery location. Thanks.
onActivityResult returns you the Uri of Image you can retrieve the path of Image copy the image from that location and save at your desired location and after that delete that from Gallery.
I'm creating an app that downloads a lot of pictures from a website and stores them all in the cache folder so that it won't take up too much space on the phone.
Now my problem is that I want the user to be able to click on an image and it will load up the image in the default android picture viewer. I've researched and figured out how to do that no problem, but I'm not 100% sure if this method will work. I can call the Intent no problem and the Pictureviewer opens but it doesn't display the images?
Can anyone let me know if this is possible to do this way? Thanks
Here is the code calling the intent and getting the directory and files...
URL url = null;
try
{
url = new URL(assetsToFullScreen[arg2]);
} catch (MalformedURLException e)
{
e.printStackTrace();
}
File cacheDir = SingleArticle.this.getCacheDir();
String fileName = url.getFile();
fileName = fileName.substring(fileName.lastIndexOf('/') + 1);
File file = new File(cacheDir, fileName);
Intent i = new Intent(android.content.Intent.ACTION_VIEW);
i.setDataAndType(Uri.fromFile(file),"image/png");
startActivity(i);*/