Question

I've been trying different things and I can save a photo in the SD card taken with Camera (not intent) but cannot pick up this same picture from SD card and place it in an ImageView. I get a Null pointer Exception allways.

Don't know what's missing, hope somebody can help me:

    PictureCallback jpegCallback = new PictureCallback() {
    public void onPictureTaken(byte[] data, Camera camera) {
        // Save the image JPEG data to the SD card
        FileOutputStream fos = null;
        String fileName = "";
        try {
            fileName = "/mnt/sdcard/DCIM/MyPicture.jpg";
            fos = new FileOutputStream(fileName);
            fos.write(data);

            fos.close();
            Toast.makeText(getBaseContext(), "Image saved:" ,
                     Toast.LENGTH_LONG).show();
        } catch (FileNotFoundException e) {
            Log.e(TAG, "File Note Found", e);
            Toast.makeText(getBaseContext(), "Image couldn't be saved.",
                     Toast.LENGTH_LONG).show();
        } catch (IOException e) {
            Log.e(TAG, "IO Exception", e);
            Toast.makeText(getBaseContext(), "Image couldn't be saved.",
                     Toast.LENGTH_LONG).show();
        }
        Bitmap bitmap = BitmapFactory.decodeFile(fileName);
        Log.d(TAG, fileName);
        mImageView.setImageBitmap(bitmap);
    }
};

I have tried that too:

    try {
        Bitmap picture = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()
                + "/DCIM/MyPhoto.jpg");
        mImageView.setImageBitmap(picture);
    } catch (Exception e) {
        Log.e("Error reading file", e.toString());
    }
}

It saves the picture but goes to the catch when trying to put the image in the ImageView saying that "Error reading file"

logcat: enter image description here

DDMS: enter image description here

Sorry everybody for the headache... I forgot to put mImageView = (Imageview)findViewById(R.id.imageView1);

I'm a disaster :-)

Was it helpful?

Solution

Try this,

        try {
            Bitmap picture = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().getPath()+"/DCIM/MyPhoto.jpg");
            Log.v("Path", Environment.getExternalStorageDirectory().getPath()+"/DCIM/MyPhoto.jpg");
            mImageView.setImageBitmap(picture);
        } catch (Exception e) {
            Log.e("Error reading file", e.toString());
        }

also check your imageview mImageView initialized or not

OTHER TIPS

Use this. It would help you.

public class LoadImagesFromSDCard  extends AsyncTask<String, Void, Void> {

    private ProgressDialog Dialog = new ProgressDialog(CameraPhotoCapture.this);

    Bitmap mBitmap;

    protected void onPreExecute() {
        /****** NOTE: You can call UI Element here. *****/

        //UI Element
        Dialog.setMessage("Loading image from Sdcard..");
        Dialog.show();
    }

    // Call after onPreExecute method
    protected Void doInBackground(String... urls) {

        Bitmap bitmap = null;
        Bitmap newBitmap = null;
        Uri uri = null;       


            try {

                /**  Uri.withAppendedPath Method Description
                * Parameters
                *    baseUri  Uri to append path segment to 
                *    pathSegment  encoded path segment to append 
                * Returns
                *    a new Uri based on baseUri with the given segment appended to the path
                */

                uri = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "" + urls[0]);

                /**************  Decode an input stream into a bitmap. *********/
                bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));

                if (bitmap != null) {

                    /********* Creates a new bitmap, scaled from an existing bitmap. ***********/

                    newBitmap = Bitmap.createScaledBitmap(bitmap, 170, 170, true); 

                    bitmap.recycle();

                    if (newBitmap != null) {

                        mBitmap = newBitmap;
                    }
                }
            } catch (IOException e) {
                //Error fetching image, try to recover

                /********* Cancel execution of this task. **********/
                cancel(true);
            }

        return null;
    }

    protected void onPostExecute(Void unused) {

        // NOTE: You can call UI Element here.

        // Close progress dialog
        Dialog.dismiss();

        if(mBitmap != null)
          showImg.setImageBitmap(mBitmap);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top