I'm trying to open my photo with intent to view it in my phone's default gallery app, but when I do so, my app flashes and reloads without providing any errors. I'm trying to open my photo like this:
Uri uri = FileProvider.getUriForFile(context, "www.markwen.space.fileprovider", file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "image/jpg");
startActivity(intent);
I cannot find anything wrong in here. I'm thinking if it would be when I save my photo, which is taken using the camera, I didn't encode it correctly. When I save my photo, I simply follow the instructions here: https://developer.android.com/training/camera/photobasics.html
So how do you guys save your photo after you take the photo with your phone's camera app? What can I do to open up my photo? Thanks
Update:
here is how I take the photo:
private void startPhotoIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Create photo file
File photo = new File(getFileDirectory(".jpg"));
// Save intent result to the file
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, FileProvider.getUriForFile(getContext(), "www.markwen.space.fileprovider", photo));
if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
// Start intent to take the picture
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
here is the way I am saving the photos I took:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// Photo
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == -1) {
Uri imageUrl = data.getData();
if (imageUrl != null) {
// Announce picture to let other photo galleries to update
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
mediaScanIntent.setData(imageUrl);
getActivity().sendBroadcast(mediaScanIntent);
try {
MediaStore.Images.Media.insertImage(getContext().getContentResolver(), directory + filename, "Multimedia-Image", "Multimedia-Image");
Toast.makeText(getContext(), "Image saved to gallery", Toast.LENGTH_LONG).show();
filename = "";
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
}
Get the absolut path of your image
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(file.getAbsolutePath())));
i'm not sure what your trying to do,
saving the picture to the phone is easy,
deepending on what intent you are using.
i would look for a way to change the intent so to specify the saving location for the image itself
Google for EXTRA_STREAM EXTRA_OUTPUT
Related
How Can I Get the Uri of Image if I Open the Gallery and Select Image and share with my App, but Image is not showing in ImageView
Open gallery for getting Image Uri
private static final int REQUEST_SELECT_IMAGE = 100;
private void handleOpenGallery() {
Intent intentSelect = new Intent(Intent.ACTION_GET_CONTENT);
intentSelect.setType("image/*");
startActivityForResult(Intent.createChooser(intentSelect, getString(R.string.select_picture)),
REQUEST_SELECT_IMAGE);
}
Uri of selected Image
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_SELECT_IMAGE) {
if (resultCode == Activity.RESULT_OK) {
Uri uri = data.getData();
shareImage(uri);
}
}
}
Share Image
private void shareImage(Uri imageUri) {
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/jpeg");
startActivity(Intent.createChooser(shareIntent, "Share to"));
}
You can follow below code by Raghu to open gallery,
if you only want to open gallery then you can use,
Intent intentSelect = new Intent(Intent.ACTION_PICK);
Add check for read/write permissions if OS version is above Marshmallow(6.0)
If you still don't get the uri then in onActivityResult when you will fetch the uri try the getPath method from here
Add check if the Android OS version is above Nougat(7.0) or not, as above nougat while fetching file uri you may get "FileUriExposedException" refer this link for more info
I am using an implicit Intent to take a picture. I have been following the work outlined in this tutorial. The issue that I am having is that the extra added to the Intent is not being delivered. Here is the code I'm using:
private void dispatchTakePictureIntent(Context context) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(context.getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile(this.getActivity());
} catch (IOException ex) {
Log.e(TAG, "Error creating file: " + ex.toString());
//TODO: 2017/1/24 - Handle file not created
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle("Error")
.setMessage(ex.toString());
final AlertDialog dialog = builder.create();
dialog.show();
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(context,
"com.example.myapp",
photoFile);
//THIS EXTRA IS NOT BEING ADDED TO THE INTENT
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
galleryAddPic(context, photoFile.getAbsolutePath());
}
}
}
When the onActivityResult method is fired, the Intent is empty. Here is that code:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
//These extras are empty. I have used the debug tool, and there is nothing in here.
Bundle extras = data.getExtras();
//extras is null
imageBitmap = (Bitmap) extras.get("data");
previewImage.setImageBitmap(imageBitmap);
}
}
Why is the intent empty? What do I need to do to fix this issue?
Why is the intent empty?
Because you asked for it to be empty, by including EXTRA_OUTPUT in your ACTION_IMAGE_CAPTURE request. Quoting the documentation:
The caller may pass an extra EXTRA_OUTPUT to control where this image will be written. If the EXTRA_OUTPUT is not present, then a small sized image is returned as a Bitmap object in the extra field. This is useful for applications that only need a small image. If the EXTRA_OUTPUT is present, then the full-sized image will be written to the Uri value of EXTRA_OUTPUT.
What do I need to do to fix this issue?
Either:
Get rid of EXTRA_OUTPUT (if you want a thumbnail-sized image), or
Stop looking for the "data" extra, and look in the location that you specified in EXTRA_OUTPUT
I am developing android app and my app have button to take camera. Previously i had fallen into a state where return data in onActivityResult after taking picture being null. This is camera expected behaviour whereby if we put EXTRA_OUTPUT in intent , it would return null. For that reason , I did null checking code and it went fine .
Now again after a few days and i tested . I still fallen into same issue again. But this time data is not null. data has empty intent such as intent and data.getData() become null.I fixed this by checking data.getData() == null and it works again. I don't why it is like that. Just curious about what was going on. For that reason i have to re-upload to production again. :-(
//camera intent
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePictureIntent.putExtra("requestCode", Constants.REQUEST_IMAGE_CAPTURE);
Intent chooseImageIntent = new Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
chooseImageIntent.setType("image/* video/*");
chooseImageIntent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
chooseImageIntent.putExtra("requestCode", Constants.REQUEST_CHOOSE_FROM);
//app can use camera
if (takePictureIntent.resolveActivity(mContext.getPackageManager()) != null) {
//add output file path which camera will save image to
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Helpers.getOutputMediaFileUri());
//create choose
Intent chooser = Intent.createChooser(chooseImageIntent, "Select From");
//add take camera intent as first intent
chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS,new Intent[]{takePictureIntent});
//open up dialog
((Activity) mContext).startActivityForResult(chooser, Constants.REQUEST_CHOOSE_FROM);
} else {
((Activity) mContext).startActivityForResult(chooseImageIntent, Constants.REQUEST_IMAGE_GALLERY);
}
EDITED
I know I how to fix the problem. What i don't understand is return data must be null if i put in EXTRA_OUTPUT. Mostly importantly the code I implemented few weeks back , i am quite sure that data return null and suddenly it is non null value again.
Actually the camera intent doesnot return the data in intent because after getting image it kill the activity.
so try this
void opencameraForPicture(int requestCode, Uri fileUri) {
checkPermissionForMarshMello(Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE);
Intent intent = new Intent(Constants.CAMERA_INTERNAL_CLASS);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
/* start activity for result pass intent as argument and request code */
startActivityForResult(intent, requestCode);
}
/**
* This method set the path for the captured image from camera for adding
* the new picture in the list
*/
private Uri getOutputMediaFile() {
File mediaStorageDir = new File(
Environment.getExternalStorageDirectory(), "."
+ Constants.CONTAINER);
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists()) {
mediaStorageDir.mkdirs();
}
File mediaFile = new File(mediaStorageDir.getPath() + File.separator
+ "IMG_" + System.currentTimeMillis() + ".png");
Uri uri = null;
if (mediaFile != null) {
uri = Uri.fromFile(mediaFile);
}
return uri;
}
in #Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
String imagePath = fileUri.getPath();
//you can decode this path as bitmap
}
I am trying to test the sample code related to capturing an image via camera. The documentation says that a URI can be passed as an extra in the intent were the camera will save the image.
I tried the following :
// image1 doesn't exist
File file = new File(getFilesDir() + "/image1");
Uri uri = Uri.fromFile(file);
Intent i = new Intent();
i.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(MediaStore.EXTRA_OUTPUT, uri);
if (i.resolveActivity(getPackageManager()) != null) {
startActivityForResult(i, 1337);
}
I am trying to place the image as a file named image1 in my files directory. I am testing this on the genymotion VM. I have tested getting the image as a bitmap in the return Intent, but when I use the above approach, the camera app gets stuck when I click done after taking the picture.
I'm guessing it has something to do with URI permissions. Do I need to add some permissions in the intent like in data sharing ?
Edit:
I tried to follow these instructions, except I want to save the photo in my app's directory, so I tried the following but it doesn't work (the app has camera permission) :
String imagePath = null;
try {
File image = File.createTempFile("testImage", ".jpg", getFilesDir());
imagePath = "file://" + image.getAbsolutePath();
}
catch(Exception e){
Toast.makeText(getApplicationContext(), "" + e.getMessage(), Toast.LENGTH_SHORT).show();
}
File file = new File(imagePath);
Uri uri = Uri.fromFile(file);
Intent i = new Intent();
i.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(MediaStore.EXTRA_OUTPUT, uri);
if (i.resolveActivity(getPackageManager()) != null) {
startActivityForResult(i, 1337);
}
I also have onActivityResult(), but its no use, as the camera app gets stuck as explained above.
Also, an additional question : When I don't have camera permission in my test app, I can still invoke the camera and the get the bitmap in intent extra, how so ? Is this something specific to genymotion VM ?
Make sure you have these permissions for your manifest
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
Everything looks good to me. Do you have onActivityResult()
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == RESULT_OK){
String result=data.getStringExtra("result");
}
if (resultCode == RESULT_CANCELED) {
//Write your code if there's no result
}
}
}//onActivityResult
im build some apps that call camera activity..
im just to take picture from my apps and send it to web server..
but i can't get my image path..
im always getting NullException Error when try to get image path..
here's my code when calling camera activity :
Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
this.startActivityForResult(camera, PICTURE_RESULT);
and this is code for activity result :
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICTURE_RESULT){
if (resultCode == Activity.RESULT_OK) {
takePicture(data);
} else if (resultCode == Activity.RESULT_CANCELED) {
}
}
}
protected void takePicture(Intent data) {
Bundle b = data.getExtras();
pic = (Bitmap) b.get("data");
if (pic != null) {
imagePicture.setImageBitmap(pic);
}
}
is there something wrong with my code?
Thanks
Ok, I see your problem. You're not setting the path to begin with. Please look at this doc.
http://developer.android.com/reference/android/provider/MediaStore.html#ACTION_IMAGE_CAPTURE
So you see, when you call ACTION_IMAGE_CAPTURE you are not passing it the extra EXTRA_OUTPUT that tells the application where the picture is going to be stored. This EXTRA_OUTPUT is the path to the file.
So right under where you make the intent do this:
Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
URI pictureUri = Uri.fromFile(new File(<path to your file>));
camera.putExtra(MediaStore.EXTRA_OUTPUT, pictureUri);