Sourabh Saldi
Sourabh Saldi

Reputation: 3587

Can not decode bitmap Image returns null

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

Upvotes: 0

Views: 1874

Answers (5)

Ramesh Maldakal
Ramesh Maldakal

Reputation: 3412

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

Upvotes: 0

Ram kiran Pachigolla
Ram kiran Pachigolla

Reputation: 21191

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!!");
}

Upvotes: 1

Anup Cowkur
Anup Cowkur

Reputation: 20563

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.

Upvotes: 0

Royston Pinto
Royston Pinto

Reputation: 6721

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

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

Upvotes: 1

Arpit Patel
Arpit Patel

Reputation: 1571

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

Upvotes: 0

Related Questions