0

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();
        }
    }
    }
}
thousight
  • 1,134
  • 1
  • 14
  • 39

2 Answers2

2

Get the absolut path of your image

startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(file.getAbsolutePath())));
bhaskar kurzekar
  • 238
  • 2
  • 14
  • It surprisingly worked, however, the image I was opening in the gallery app becomes empty, any ideas based on how I saved it? – thousight Feb 24 '17 at 03:07
  • private void galleryAddPic() { Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); Uri contentUri = Uri.fromFile(file); mediaScanIntent.setData(contentUri); this.sendBroadcast(mediaScanIntent); } – bhaskar kurzekar Feb 24 '17 at 05:20
  • I have that. But when I want to open the photo later with my app, it opens the gallery app but the app shows nothing – thousight Feb 24 '17 at 06:29
0

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

Chief Madog
  • 1,738
  • 4
  • 28
  • 55