I would like to ask a question that relates to the ordering of codes.
I am working on a drawing app of which letting user to select image, and then crop the image so as to fit to a drawing plate. Everything works fine using the below code if do not delete the tempFile shown in the code:
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PHOTO_PICKED && resultCode == RESULT_OK)
{
if (data == null)
{
Log.w(TAG, "Null data, but RESULT_OK, from image picker!");
Toast t = Toast.makeText(this, R.string.no_photo_picked, Toast.LENGTH_SHORT);
t.show();
return;
}
if (data != null)
{
File tempFile = getTempFile();
String filePath= Environment.getExternalStorageDirectory() +"/"+TEMP_PHOTO_FILE;
Toast.makeText(this, "path "+filePath, Toast.LENGTH_LONG).show();
doodleView.load_pic(filePath);
// if (tempFile.exists()) tempFile.delete();
}
}
The drawing plate is an extended view named doodleView, and would load the cropped chosen image using doodleView.load_pic(filePath); I would also like to remove the tempFile after it is used as these are redundant. Yet if i add if (tempFile.exists()) tempFile.delete(); the program will crash as the File is deleted before the image file is loaded to the extended view.
If then how could it be rearranged? Or is it possible to execute such delete tempFile on extended View? All the main operations of cropping and placing the tempfile is written in MainActivity.
Thanks in advance for your advice!
Related
I am getting customers documents using FilePicker . After opening FilePicker it shows options to take images from gallery as well as camera .
After taking images , images are visible inside gallery but But i don't want images to be visible inside gallery . and i know there are already questions about hiding images from gallery but not from FilePicker .
What should i do ? I think saving images inside a folder Android/App/data inside internal/external storage will do it . But how to get the folder location and save images there . ? or any other solution that can help?
here is code inside onClick() method .
if (view == pickPhotoBtn) {
FilePickerBuilder.getInstance().setMaxCount(10)
.setActivityTheme(R.style.AppTheme3)
.pickPhoto(getActivity());
}
and this is onActivityResult() method .
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case FilePickerConst.REQUEST_CODE_PHOTO:
if (resultCode == Activity.RESULT_OK && data != null) {
Log.e(TAG, "onActivityResult: ");
for (String path : data.getStringArrayListExtra(FilePickerConst.KEY_SELECTED_PHOTOS)) {
String fileName = CommonMethod.getFileNameFromPath(path);
if (!documentNameList.contains(fileName) && !selectPhotosPath.contains(path) && selectPhotosPath.size() < 10) {
selectPhotosPath.add(path);
File file = new File(path);
Log.e(TAG, "Image Name: " + fileName);
documentNameList.add(fileName);
documentIdList.add(0);
selectPhotosBitmap.add(CommonMethod.compressImage(file, getContext()));
}
}
adapter.addItems(selectPhotosBitmap);
adapter.notifyDataSetChanged();
Log.e(TAG, "Number Of Files: " + selectPhotosPath.size());
}
}
}
You can save your image in app internal storage and by default no one can access you app internal storage as it is private for other. You can check below link how to storage and retrive image from internal storage
Saving and Reading Bitmaps/Images from Internal memory in Android
I've written code in Android to capture an image and then use that image as follows:
private static Intent i;
final static int cons = 0;
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
public void takePicture()
{
i= new Intent(MediaStore.ACTION_IMAGE_CAPTURE); //Open camera
startActivityForResult(i, cons);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
Log.d("Testing if condition", "Test"); //This code does not execute
Bundle ext = data.getExtras();
Log.d("Upload image", ext.toString());
sendForUpload(data); // function to upload file to server
} else if (resultCode == RESULT_CANCELED) {
// User cancelled the image capture
} else {
// Image capture failed, advise user
}
}
}
This code lets me take a photo and it saves to the SD card. However, I'm not sure if the file is sent to the sendForUpload function where I've handled getting the path and uploading the file. In fact nothing inside the if (resultCode == RESULT_OK) block works. How do I use the image captured from this activity?
Well, you have a few problems.
First, you are calling startActivityForResult(i, cons);. cons is 0. You are trying to compare 0 with CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE. Since CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE is 100, that will not work.
Second, you are calling data.getExtras().toString() (split over two Java statements). I would expect this to return a value like android.os.Bundle#34334143. If that is what you want to upload, fine. I am guessing that you want to upload the image. Since you did not include EXTRA_OUTPUT in your ACTION_IMAGE_CAPTURE Intent, you get a Bitmap of the thumbnail image taken by the camera via data.getParcelableExtra("data").
Third, you are making inappropriate assumptions:
This code lets me take a photo and it saves to the SD card.
There is no requirement for the camera app to save the image anywhere, let alone to removable storage. In fact, I would argue that this is a bug in your camera app. That is not surprising, as ACTION_IMAGE_CAPTURE implementations have bugs.
where I've handled getting the path and uploading the file
There is no path. You did not include EXTRA_OUTPUT in your ACTION_IMAGE_CAPTURE Intent, and so all you get back is the thumbnail Bitmap.
I'm using this to put the image in a ImageView:
Uri uri = data.getData();
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
You can use bitmap or uri objects for upload or show your picture..
Hope it helps you.
I successfully save the pictures from android camera
to using the following root paths
Environment.getExternalStorageDirectory()
and
context.getExternalFilesDir(null)
but I cannot save using the following root path
context.getFilesDir()
Do you know if it is possible to save the pictures taken from android camera to the android internal storage which have context.getFilesDir() as root folder?
I use the following code to start the camera:
Intent takePictureIntent = new
Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(picFile));
startActivityForResult(takePictureIntent, REQUEST_TAKE_PICTURE);
//and this code I used to get the camera output
#Override
public void onActivityResult(int requestCode, int resultCode,
Intent data) {
if ((requestCode == REQUEST_TAKE_PICTURE) && (resultCode ==
Activity.RESULT_OK)
) {
Toast.makeText(this, "Can get the image.",
Toast.LENGTH_LONG).show();
//Bitmap photo = (Bitmap) data.getExtras().get("data");
// System.out.println("photo "+photo.getWidth());
// final ImageView imageView = (ImageView) findViewById(R.id.imageView1);
// imageView.setImageBitmap(photo);
} else if ((requestCode == REQUEST_TAKE_PICTURE) && (resultCode == Activity.RESULT_CANCELED)) {
Toast.makeText(this, "Cannot get the image.", Toast.LENGTH_LONG).show();
}
}
You cannot use getFilesDir(), because you are launching a third-party camera app to take the picture, and it cannot write to your app's internal storage.
I was following the android tutorial as follows from this official link
I wish to save the full Image. The problem is when the user enters the camera app takes a photo but decides to cancel it, android still saves the image with size zero. Any way to avoid this?
The code is as follows
static final int REQUEST_TAKE_PHOTO = 1;
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
...
}
// Continue only if the File was successfully created
if (photoFile != null) {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photoFile));
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}
Ok solved it using the name of the newly created file. When we create name of file we simply save the path of the file in a String variable say "Current Path"
And add the following code-
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == Activity.RESULT_CANCELED && requestCode == 0)
{
File file = new File(currentPath);
boolean delete = file.delete();
Log.i("delete",String.valueOf(delete));
}
}
I think you are doing something wrong in your code. You should post your code to get the proper assistance.
as far as I understand . You should override the onbackpressed function. you should close/release camera here. In this case I think the camera would not take and save picture.
Hi I am using this piece of code for camera in my activity.
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == TAKE_PICTURE_WITH_CAMERA) {
if (resultCode == Activity.RESULT_OK) {
// To Write the Images in SDCard
File f = new File(imgName);
try {
Uri capturedImage = Uri
.parse(android.provider.MediaStore.Images.Media
.insertImage(getContentResolver(),
f.getAbsolutePath(), null, null));
Log.i("camera",
"Selected image: " + capturedImage.toString());
pic.setImageURI(capturedImage);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
} else {
Log.i("Camera", "Result code was " + resultCode);
}
}
}
I am trying to apply the image captured to my ImageView. The first time, the camera takes a picture and sets it to the ImageView. But when I run it for the second time, my app is not able to set the newly captured image to the imageView. It crashes after returning from the camera intent. How can I solve this?
Any help is appreciated.
In order to avoid this kind of error, the bitmap has to be scaled first. I have tried the below piece of code and it works fine.
bm1=Bitmap.createScaledBitmap(bm, 300, 300,true);
pic.setImageBitmap(bm1);
bm.recycle();
bm=null;
When reading about Camera, I think it is Hardware. Hardware always is using its own Thread. Perhaps you need to create a memory copyied image and serve it by dispatcher. I do not know your code but I am interested in receiving it to help you.
http://blog.lieberlieber.com/2009/07/09/controlinvoke-wpf-style-and-special-bitmapsource-handling/
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/f0c5e590-8bfe-4452-a784-4987af9fce89/