Android: Cannot access an image file from the internal storage on Android device

845 views Asked by At

I am trying to save an image, taken by the camera to the INTERNAL storage of the device and then to use it as ImageView, but I cannot access the file. It is all for educational purpose. I have a button to call the camera app as sub activity.

public void addListener() {
    Button b = (Button)findViewById(R.id.snap);
    b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            File picturesDirectory;
            picturesDirectory = getFilesDir();
            imageFile = new File(picturesDirectory, "passspoints_image");

            Log.d(DEBUGTAG, imageFile.getAbsolutePath());

            Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imageFile));
            startActivityForResult(i, PHOTO_TAKEN);
        }
    });
}

And then I am trying to access the file.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch(requestCode) {
        case PHOTO_TAKEN:
            Log.d(DEBUGTAG, imageFile.getAbsolutePath());
            Bitmap photo = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
            try {
                photo = rotateImageIfRequired(photo, Uri.fromFile(imageFile));
            } catch (IOException e) {
                e.printStackTrace();
            }
            if(photo != null) {
                ImageView imageView = (ImageView)findViewById(R.id.imageView);
                imageView.setImageBitmap(photo);
            } else {
                Toast.makeText(this, "Unable to read photo file", Toast.LENGTH_LONG).
                        show();
            }
            break;
    }
}

This is the path from the debug tag:

/data/data/tk.crackedbunny.www.takingphotostest/files/passspoints_image

And this is the error message:

E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /data/data/tk.crackedbunny.www.takingphotostest/files/passspoints_image: open failed: ENOENT (No such file or directory)

Before that I have managed to do this using the external storage, but I would like to use the internal storage in case the external storage is not available.

Thanks.

1

There are 1 answers

2
Waylan Punch On

Is passspoints_image a picture name ? How about to add an extension to it, such as passspoints_image.jpg ?

Or do you have read/write permission in your AndroidManifest.xml file ?