0

I have camera app that clicks picture and saves it on sd card and when I decode that it gives null bitmap here is the code

Bitmap bitmap = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()+"FacePhoto.jpg");

I have checked in ddms the Facephoto file exists on sd card

what can be problem here that I get "null" bitmap

5 Answers 5

1

You are missing a slash, change the code as follows.

Bitmap bitmap = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()+"/FacePhoto.jpg"); 
Sign up to request clarification or add additional context in comments.

Comments

1

There may be a problem with your image path just check like this

File rootDir = Environment.getExternalStorageDirectory();
File file = new File(rootDir + "/FacePhoto.jpg");

if (file.exists())
{
    Bitmap bitmap = BitmapFactory.decodeFile(file);

}
else
{
   System.out.println("File Not Exists. Check the path!!");
}

Comments

0

Try using

Environment.getExternalStorageDirectory()+"/FacePhoto.jpg"

use '/filename' instead of just 'filename'.

If this doesn't work try using

Environment.getExternalStorageDirectory().getAbsolutePath()+"/FacePhoto.jpg"

The point to note is to use absolute path instead of relative.

Also see if filename is correctly spelled in your code.

Comments

0

Now on activity result you have to use like this.

if (requestCode == CAMERA) {
                final File file = getTempFile();

                new Thread(new Runnable() {

                    @Override
                    public void run() {
                        try {
                            selPath = file.getAbsolutePath();
                            final String selectedImagePath = file
                                    .getAbsolutePath();
                            bitmap = BitmapFactory
                                    .decodeFile(selectedImagePath);
                            selPath = selectedImagePath;

                        } catch (Exception e) {
                            Log.v(TAG, "Exception: " + e.toString());
                            handler.sendEmptyMessage(IMAGENOTLOADED);
                        }
                    }
                }).start();
            }

getTempFile method is like this

private File getTempFile() {
    final File path = new File(Environment.getExternalStorageDirectory(),
            getPackageName());
    if (!path.exists()) {
        path.mkdir();
    }
    return new File(path, fileName);
}

Comments

0

try the following :

     FileInputStream instream = new FileInputStream("/sdcard/Pics/Image.png"); 
            BufferedInputStream bif = new BufferedInputStream(instream); 
            byteImage1 = new byte[bif.available()]; 
            bif.read(byteImage1); 
             BitmapFactory.decodeByteArray(byteImage1, 0, byteImage1.length);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.