Search code examples
androidbitmapbitmapfactory

Why Bitmap.decodeFile() method is returning null?


I am trying to decode a File into Bitmap like this -

 BitmapFactory.Options options = new BitmapFactory.Options();
    try {
        Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, options);
        cropImageView.setImageBitmap(bitmap);
        Log.d("Musik","bitmap - "+bitmap+" "+mCurrentPhotoPath);
    }
    catch (Exception e){
        Log.d("Musik",e.toString());
    }

and my mCurrentPhotoPath is

file:/storage/sdcard0/Android/data/com.example.iaugmentor.iaugmentortestvers/files/Pictures/JPEG_20160805_161105_478260200.jpg

I am unable to find why it is returning null and also no exception is caught .

Please help . Thanks in advance.

To make things clear , let me state how am i saving the image in the first place I am creating a File object first and then capturing a image and saving it in a given Uri -

// Create the File where the photo should go
                    File photoFile = null;
                    try {
                        photoFile = createImageFile();
                    } catch (IOException ex) {
                        // Error occurred while creating the File

                    }

                    if (items[which].equals("Take Photo")) {

                        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

                        if(intent.resolveActivity(getPackageManager()) != null) {


                            // Continue only if the File was successfully created
                            if (photoFile != null) {
                                profilePicUri = FileProvider.getUriForFile(ApplicationContext.getAppContext(),
                                        "com.example.iaugmentor.iaugmentortestvers",
                                        photoFile);


                            }
                            intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, profilePicUri);

                            try {
                                intent.putExtra("return-data", true);

                                startActivityForResult(intent, RESULT_LOAD_IMG);
                            } catch (ActivityNotFoundException e) {
                                e.printStackTrace();
                            }
                        }

The method createImageFile() goes like this`

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;
}

then i am trying to read the image from another activity with the help of variable mCurrentPhotoPath


Solution

  • Create/Save filePath : mCurrentPhotoPath like this in your createImageFile() Method.

    File imageFile = new File(image.getAbsolutePath()); 
    
    mCurrentPhotoPath = imageFile.toString();