无法从活动文件夹中打开图像?

时间:2017-12-18 19:47:45

标签: java android android-camera2

我能够将相机中的图像保存到我的应用程序的包文件夹中,但是当我尝试从该文件夹打开图像时,我会收到消息:"可以' t开放"

我已经尝试将图像移动到图片文件夹,它打开就好了。那么,可能是什么问题。这是我到目前为止所尝试的。

    public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            mFile = new File(getActivity().getExternalFilesDir(null), "pic.jpg");
        }

private static class ImageSaver implements Runnable {

        /**
         * The JPEG image
         */
        private final Image mImage;
        /**
         * The file we save the image into.
         */
        private final File mFile;

        ImageSaver(Image image, File file) {
            mImage = image;
            mFile = file;
        }

        @Override
        public void run() {
            ByteBuffer buffer = mImage.getPlanes()[0].getBuffer();
            byte[] bytes = new byte[buffer.remaining()];
            buffer.get(bytes);
            FileOutputStream output = null;
            try {
                output = new FileOutputStream(mFile);
                output.write(bytes);
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                mImage.close();
                if (null != output) {
                    try {
                        output.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

    }

    /**

     */

Reading the image
    public class SingleMediaScanner implements MediaScannerConnection.MediaScannerConnectionClient {

        private MediaScannerConnection mMs;
        private File mFile;

        public SingleMediaScanner(Context context, File f) {
            mFile = f;
            mMs = new MediaScannerConnection(context, this);
            mMs.connect();
        }

        public void onMediaScannerConnected() {
            mMs.scanFile(mFile.getAbsolutePath(), null);
        }

        public void onScanCompleted(String path, Uri uri) {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(uri);
            startActivity(intent);
            mMs.disconnect();
        }

    }

 b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new SingleMediaScanner(getActivity(), allFiles[0]);
            }
        });

清单

  <uses-permission android:name="android.permission.CAMERA"/>
    <uses-permission android:name="android.permission.FLASHLIGHT"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-feature android:name="android.hardware.camera2.full"/>

1 个答案:

答案 0 :(得分:0)

如果要保存内部文件夹

public File getImage(Context context) {
        try {
            String nameFile = "sample.jpg";
            File folder = context.getFilesDir();
            File fileCreated = new File(folder, nameFile);
            return fileCreated;
        } catch (Exception ex) {
            throw ex;
        }
    }

然后将此文件传递给 ImageSaver