【问题标题】:Return from camera does not get displayed in ImageView从相机返回的图像不会显示在 ImageView 中
【发布时间】:2016-06-08 12:45:25
【问题描述】:

我们像这样从我们的应用程序启动相机应用程序:

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

并试图像这样捕捉相机的返回:

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

            }
    }

如果代码太多,请见谅。

我们的问题是图像被保存到磁盘但没有显示在ImageView 中。我们错过了什么?

【问题讨论】:

标签: android


【解决方案1】:

ACTION_IMAGE_CAPTURE 不返回 Uridata.getData() 没用。要么:

  • 在您的ACTION_IMAGE_CAPTURE 请求中传递EXTRA_OUTPUT,在这种情况下您知道图像应该存储在哪里,或者

  • 使用data.getExtra("data")获取缩略图Bitmap,不提供EXTRA_OUTPUT时返回

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多