有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java无法从“活动”文件夹打开图像?

我可以在我的应用程序的软件包文件夹中保存相机的图像,但情况就是这样,当我尝试打开该文件夹中的图像时,我收到消息:“无法打开”

我已经尝试将图像移动到图片文件夹,它打开得很好。那么,问题出在哪里呢。这就是我到目前为止所尝试的

    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 安卓:name="安卓.permission.CAMERA"/>
    <uses-permission 安卓:name="安卓.permission.FLASHLIGHT"/>
    <uses-permission 安卓:name="安卓.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-feature 安卓:name="安卓.hardware.camera2.full"/>

共 (1) 个答案

  1. # 1 楼答案

    如果要保存在内部文件夹中

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

    然后将此文件传递给图像保护程序