0

I am beginner in Android Programming and I just want to display the captured image through camera and here is my code

private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
    );

    // Save a file: path for use with ACTION_VIEW intents
    mCurrentPhotoPath = "file:" + image.getAbsolutePath();
    return image;
}

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) {
            fileUri = FileProvider.getUriForFile(this,
                    "com.example.android.fileprovider",
                    photoFile);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
            startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
        }
    }
}
private void previewSavedImage() {
    try {
        // hide video preview
        videoPreview.setVisibility(View.GONE);

        imgPreview.setVisibility(View.VISIBLE);

        // bimatp factory
        BitmapFactory.Options options = new BitmapFactory.Options();

        // downsizing image as it throws OutOfMemory Exception for larger
        // images
        options.inSampleSize = 2;

        String path = fileUri.getPath();

        //File img_file = new  File(getFilesDir(), path);



        final Bitmap bitmap = BitmapFactory.decodeFile(path, options);

        imgPreview.setImageBitmap(bitmap);
    } catch (NullPointerException e) {
        e.printStackTrace();
    }
}

In above code bitmap always returning none and because of that I can not display image. Am I mission something?

Yogesh
  • 865
  • 6
  • 13

1 Answers1

1

Finally I solved this. the problem is with "file:" string in mCurrentPhotoPath, I just need to remove it before using it in decodeFile method.

Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath.replace("file:", ""));
Yogesh
  • 865
  • 6
  • 13
  • Then why did you put it there? And if you needed such then you should have used "file://". – greenapps Jul 24 '16 at 08:47
  • Good catch! That's an attention to detail. – Jossie Calderon Jul 24 '16 at 15:36
  • I will remove that. I was just following documentation and thought it might be useful later. – Yogesh Jul 24 '16 at 16:30
  • great answer, I copied from docs as well really bad from from the google docs as on their sample app and website they have the file: prefix [link](https://developer.android.com/training/camera/photobasics.html) – ngatirauks Sep 29 '16 at 02:07