I'm trying to capture an image with the android.provider.MediaStore.ACTION_IMAGE_CAPTURE and things seem to work fine, the only problem is that the process com.android.camera isn't being destroyed after returning and it is interfering with the running of the rest of the application.
So I'm wondering why it is not being destroyed and how I can destroy it once the capture is done? Thanks any help would be appreciated.
Here is my code:
private OnClickListener buttonCaptureListener = new OnClickListener(){
public void onClick(View v){
String path = Environment.getExternalStorageDirectory() + "/"+Long.toHexString(System.currentTimeMillis())+".jpg";
File file = new File(path);
Uri outputFileUri = Uri.fromFile(file);
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra( MediaStore.EXTRA_OUTPUT, outputFileUri );
startActivityForResult(intent,SelectImagesActivity.REQUEST_CAPTURE);
}
};
Result handling:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == SelectImagesActivity.REQUEST_CAPTURE){
if(resultCode == RESULT_OK){
// display image and etc.
}
}
}
I also think it is normal action that process is not destroyed immediately.
And I suggest below code for explicit meaning.
it is different that appending a extra data -('return data', true)- into the intent.
private OnClickListener buttonCaptureListener = new OnClickListener(){
public void onClick(View v){
String path = Environment.getExternalStorageDirectory();
String file_name = Long.toHexString(System.currentTimeMillis())+".jpg";
File file = new File(path , file_name);
Uri outputFileUri = Uri.fromFile(file);
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra( MediaStore.EXTRA_OUTPUT, outputFileUri );
intent.putExtra("return-data", true);
startActivityForResult(intent,SelectImagesActivity.REQUEST_CAPTURE);
}
Related
This code is redirecting to drive with open navigation but not opening actual given path
OLD Code
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath() + "/foldername/");
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setDataAndType(uri, "text/csv");
startActivity(intent);
The path value I am getting through uri is:
/storage/emulated/0/myfolder
NEW Code
Intent resultIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
resultIntent.addCategory(Intent.CATEGORY_OPENABLE);
Uri uri =
Uri.parse(Environment.getExternalStorageDirectory().getPath() +
"/myfolder/");
resultIntent.setType("*/*");
resultIntent.putExtra("android.provider.extra.INITIAL_URI", uri);
// show the entire internal storage tree
//resultIntent.putExtra("android.content.extra.SHOW_ADVANCED", true);
startActivity(resultIntent);
Let me know the issue where is the problem in this code.
private static final int READ_REQUEST_CODE = 10;
...
/**
* Fires an intent to spin up the "file chooser" UI and select an image.
*/
public void performFileSearch() {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType(uri, "text/*");
startActivityForResult(intent, READ_REQUEST_CODE);
}
After the user selects a document in the picker, onActivityResult()
gets called. The resultData parameter contains the URI that points to
the selected document. Extract the URI using getData(). When you have
it, you can use it to retrieve the document the user wants.
#Override
public void onActivityResult(int requestCode, int resultCode,
Intent resultData) {
if (requestCode == READ_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
Uri uri = null;
if (resultData != null) {
uri = resultData.getData();
Log.i(TAG, "Uri: " + uri.toString());
showImage(uri);
}
}
}
Simple question. Is it possible to allow a button (preferably a FAB) to open a camera app, sort of as a short cut? This shortcut can either lead to the default camera, or lead to any downloaded camera apps.
Yes, it's possible.
You can create an implicit intent with action ACTION_IMAGE_CAPTURE.
eg:
File file = new File(Environment.getExternalStorageDirectory() + "/DCIM/", "image" + new Date().getTime() + ".png");
Uri imgUri = Uri.fromFile(file);
String imgPath = file.getAbsolutePath();
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, setImageUri());
startActivityForResult(intent, CAPTURE_IMAGE);
To handle the result:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != Activity.RESULT_CANCELED) {
if (requestCode == CAPTURE_IMAGE) {
Bitmap resultAsBitmap = BitmapFactory.decodeFile(imgPath);
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
}
PS: Don't forget to add your permissions in manifest file.
In my app i'm using the camera app on the phone in order to take a picture. After taking the picture it returns back to my app. I noticed that it acts differently in different phones. In Galaxy s3 I found that after taking a picture it shows the picture and gives an option to save it (and then go back to my app) or discard (and go back to the camera app). The problem is that when the screen is rotated the app crushes when the save/discard screen appears. The only way to stop it from crushing is to take the picture without rotating the screen and keep it that way.
Is there a solution to this problem? Maybe there is a way that I can define that it won't allow rotation at this screen?
Here is the code:
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
//Specify target file
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
newPicName = android.os.Environment.getExternalStorageDirectory() + "/"+app.APP_FOLDER + "/" + app.getSession()+ "_"+app.getDateTime()+ "_" +sdf.format(cal.getTime()) +".jpg";
File imageFile = new File(newPicName);
Uri imageFileUri = Uri.fromFile(imageFile);
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageFileUri);
startActivityForResult(intent, 0);
If you just want to limit the orientation of the screen you need to specify it in your manifest… For example:
<activity
android:name="com.xxx"
android:label="#string/xxx"
android:screenOrientation="portrait" > //this limits the screen to only portrait.
</activity>
Additional Info
This is how I do it and it works for me perfectly.
//instance variables
private static final int TAKE_PICTURE = 0;
private Uri mUri;
private Bitmap mPhoto;
//First I create a method to capture the image.
private void captureImage() {
Intent i = new Intent("android.media.action.IMAGE_CAPTURE");
File f = new File(Environment.getExternalStorageDirectory(), "profile.jpg");
i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
mUri = Uri.fromFile(f);
startActivityForResult(i, TAKE_PICTURE);
}
//Then I handle the result.
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case TAKE_PICTURE://this is a constant defined in my instance variables.
if (resultCode == Activity.RESULT_OK) {
getContentResolver().notifyChange(mUri, null);
ContentResolver cr = getContentResolver();
try {
mPhoto = android.provider.MediaStore.Images.Media.getBitmap(cr, mUri);
//set your photo in a screen…
} catch (Exception e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
}
}
Try following code :
private File tempFile;
private static final int CAMERA_IMAGE = 1;
public void takePicFromCamera() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (tempFile != null) {
Log.i(""image Path : "", tempFile.getPath());
Uri picUri = Uri.fromFile(tempFile); // convert path to Uri
intent.putExtra(MediaStore.EXTRA_OUTPUT, picUri);
Log.i("Picture Uri", " : " + picUri);
}
startActivityForResult(intent, MedicationConstants.CAMERA_IMAGE);
}
public void onActivityResult(int requestCode, int resultCode, final Intent intent) {
if (requestCode == MedicationConstants.CAMERA_IMAGE) {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
if (tempFile != null) {
//do anything with image file. Store in database. Or even should add functionality of dropping
}
}
}, 2000);
}
}
In on onActivityResult you should add functionality of image cropping. Please have a look at simple-crop-image-lib. It is great library & works for almost all device.
Thanks
I need to take photos in my application, this is the core codes:
tigger:
mPhotoToBeUpload = new File(mImageBasePath, System.currentTimeMillis() + ".jpg");
try {
mPhotoToBeUpload.mkdirs();
mPhotoToBeUpload.createNewFile();
} catch (IOException e) {
Log.e("xx",e.getMessage());
}
Uri outputFileUri = Uri.fromFile(mPhotoToBeUpload);
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(cameraIntent, Result_Code_Camera);
Result handler:
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
if (resultCode == RESULT_OK) {
Bitmap photoTaken = BitmapFactory.decodeFile(mPhotoToBeUpload.getAbsolutePath());
mImageView.setImageBitmap(photoTaken);
} else
}
However I found that these codes work in some devices but not in some others.
For example, they does not work in Nexus 4 and Nexus5.
While I saythey does not work,I mean that after I take a picture, and hit the OK button, I cannot get back to the previous activity:
It have no effect when I hit the OK(the icon inside the red circle).
What is the problem?
Upadte:
private final String mImageBasePath = Environment.getExternalStorageDirectory() + File.separator + IConstant.Application_Folder + File.separator + "photos";
mPhotoToBeUpload = new File(mImageBasePath, System.currentTimeMillis() + ".jpg");
Result: mPhotoToBeupLoad=/storage/emulated/0/myapp/photos/1395287647386.jpg
Show what is your path for mImageBasePath, because this problem mostly happen when the file path is invalid.
Solved by referring this question:Android ACTION_IMAGE_CAPTURE Intent
Instead of
Uri outputFileUri = Uri.fromFile(mPhotoToBeUpload);
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(cameraIntent, Result_Code_Camera);
Use:
URI mPhotoToBeUploadURI = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new ContentValues());
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mPhotoToBeUploadURI);
I have a folowing code:
public void take_picture(View view)
{
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
ImageView slikaa = (ImageView)this.findViewById(R.id.slikaa);
if ((requestCode == CAMERA_REQUEST)&& (resultCode == Activity.RESULT_OK)) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
slikaa.setImageBitmap(photo);
}
Now my question is how to get that image path(for saving it to my database), and then again, use it to show in a picture(I don't know how to get String paths, and then re-use it)
For getting Image Path in onActivityResult you will need to Start camera by send Image Path with Intent as:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//ContentValues values = new ContentValues();
ContentValues values = new ContentValues(3);
values.put(MediaStore.Images.Media.DISPLAY_NAME, "testing");
values.put(MediaStore.Images.Media.DESCRIPTION, "this is description");
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
imageFilePath = MainActivity.this.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageFilePath);
startActivityForResult(intent, CAMERA_REQUEST);
and on onActivityResult
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
ImageView slikaa = (ImageView)this.findViewById(R.id.slikaa);
if ((requestCode == CAMERA_REQUEST)&& (resultCode == Activity.RESULT_OK)) {
//get image from path
Bitmap photo = (Bitmap) data.getExtras().get("data");
photo = BitmapFactory.decodeStream(this.getContentResolver()
.openInputStream(imageFilePath), null, op);
slikaa.setImageBitmap(pic);
//slikaa.setImageBitmap(photo);
}
String path;
Public void take_picture(){
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
File output = new File(dir,"gtumca.png");
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(output));
path = output.getAbsolutePath(); <-------------
startActivityForResult(cameraIntent, TAKE_PHOTO);
}