1

Basically what I'm trying to do is create a simple app that uses the default camera app to take a picture, then I want to display that image in a new activity within an ImageView, but everything I've tried has resulted in

java.io.FileNotFoundException: /storage/emulated/0/imageToProcess.jpg: open failed: ENOENT (No such file or directory)

I've included the correct permissions (maybe even too many) in the manifest:

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

and I'm also manually requesting permissions if need be in the main activity:

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    if(checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(new String[]{Manifest.permission.CAMERA}, 1);
    }
    if(checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
        requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
    }
    if(checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
        requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
    }
}

so I don't think the issue is with read/write permissions.

In my main activity's xml file, I've created a button with an onClick listener that calls the following method:

public void openCamera(View view) {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    File f = new File(Environment.getExternalStorageDirectory() + File.separator + "imageToProcess.jpg");
    imgUri = Uri.fromFile(f);
    intent.putExtra("imgUri", imgUri.toString());
    startActivityForResult(intent, TAKE_PICTURE);
}

Here's the onActivityResult() method I defined to create a new intent and pass the uri to the new activity:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);
    if(requestCode == TAKE_PICTURE && resultCode == Activity.RESULT_OK) {
        Intent camResult = new Intent(this, ShowCameraResult.class);
        camResult.putExtra("imgUri", imgUri.toString());
        startActivity(camResult);
    }
}

NOTE: imgUri and TAKE_PICTURE are defined at the top of the class as protected static final int TAKE_PICTURE = 1; and private Uri imgUri;

Lastly, here is my ShowCameraResult activity

public class ShowCameraResult extends AppCompatActivity {
    private Bitmap mImageBitmap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_camera_result);
        ImageView imageView = (ImageView)findViewById(R.id.imageView);
        String filePath = getIntent().getStringExtra("imgUri");
        Log.d("ShowCameraResult", "directory: " + filePath);
        try {
            mImageBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse(filePath));
            imageView.setImageBitmap(mImageBitmap);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

The Log.d method call above returns

ShowCameraResult: directory: file:///storage/emulated/0/imageToProcess.jpg

This leads me to believe that the image result from the camera isn't actually being saved into my imageToProcess.jpg file, but I could be very wrong as this is the first real android project I've worked on.

So my question is: Where did I go wrong?

Any help is greatly appreciated, thank you!

Austin
  • 21
  • 3

1 Answers1

0

ITs returning a file, not a content resolver uri. Just decode the file with BitmapFactory.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • It's returning the URI of a file. Use BitmapFactory.decodeFile to get the Bitmap for the file. Then display that. No content resolvers needed, it isn't returning a UR. Its storing the file right where you asked it to in your intent. – Gabe Sechan Feb 10 '17 at 06:26
  • Oh and side note- you don't need camera permission to launch the camera intent. Only if you plan on taking the picture in your app. – Gabe Sechan Feb 10 '17 at 06:27