Blair Osbay
Blair Osbay

Reputation: 227

Return from camera does not get displayed in ImageView

We started the camera app from our app like this:

    Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    String timeStamp = "001";

    File imagesFolder = new File(Environment.getExternalStorageDirectory(), "MymhiImages");
    imagesFolder.mkdirs();

    File image = new File(imagesFolder, filename + ".png");
    Uri uriSavedImage = Uri.fromFile(image);

And attempted to catch the return from the camera like this:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        InputStream stream = null;
        if (requestCode == REQUEST_CODE && resultCode == AppCompatActivity.RESULT_OK)
            try {
                // recyle unused bitmaps
                if (bitmap != null) {
                    bitmap.recycle();
                }
                stream = getContentResolver().openInputStream(data.getData());
                bitmap = BitmapFactory.decodeStream(stream);

                imageView.setImageBitmap(bitmap);

                File dir = new File(Environment.getExternalStorageDirectory().toString() + "/mhi/");
                if (!dir.exists())
                    dir.mkdir();

                String path = Environment.getExternalStorageDirectory().toString() + "/mhi/" + filename + ".png";
                OutputStream out = null;
                File imageFile = new File(path);

                try {
                    out = new FileOutputStream(imageFile);
                    // choose JPEG format
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
                    out.flush();
                } catch (FileNotFoundException e) {
                    // manage exception
                } catch (IOException e) {
                    // manage exception
                } finally {

                    try {
                        if (out != null) {
                            out.close();
                            //jsi.showMsg("Saved Successfully");
                            Toast.makeText(getApplicationContext(), "Saved Successfully", Toast.LENGTH_LONG).show();
                        }

                    } catch (Exception exc) {
                    }

                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {

                if (stream != null)
                    try {
                        stream.close();
                    } catch (IOException ie) {
                        ie.printStackTrace();
                    }

            }
    }

Forgive us, if there is too much code.

Our problem is that the image gets saved to disk but is not displayed in the ImageView. What have we missed?

Upvotes: 0

Views: 32

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007099

ACTION_IMAGE_CAPTURE does not return a Uri. data.getData() is useless. Either:

  • Pass EXTRA_OUTPUT in your ACTION_IMAGE_CAPTURE request, in which case you know where the image should be stored, or

  • Use data.getExtra("data") to get a thumbnail Bitmap, which is returned in cases where you do not provide EXTRA_OUTPUT

Upvotes: 1

Related Questions