【问题标题】:Show images in gridview from both gallery and camera在 gridview 中显示来自画廊和相机的图像
【发布时间】:2015-03-03 00:38:32
【问题描述】:

嗨,在我的应用程序中,我使用相机和图库来获取图像并在 gridview 中显示所有图像,我可以成功实现相机,但是当我对图库图像使用相同的方法时,它显示文件未找到异常

我的代码

public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == getActivity().RESULT_OK) {
            // user is returning from capturing an image using the camera
            if (requestCode == CAMERA_REQUEST) {
                Bundle extras = data.getExtras();
                Bitmap thePic = extras.getParcelable("data");
                String imgcurTime = dateFormat.format(new Date());
                File imageDirectory = new File(GridViewDemo_ImagePath);
                imageDirectory.mkdirs();
                String _path = GridViewDemo_ImagePath + imgcurTime + ".jpg";

                LogUtil.d("Camera _path" + _path);

                try {
                    FileOutputStream out = new FileOutputStream(_path);
                    thePic.compress(Bitmap.CompressFormat.JPEG, 90, out);
                    out.close();
                } catch (FileNotFoundException e) {
                    e.getMessage();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                listOfImagesPath = null;

                listOfImagesPath = RetriveCapturedImagePath();

                LogUtil.d("listOfImagesPath" + listOfImagesPath.size());

                if (listOfImagesPath != null) {
                    mCameraGrid.setAdapter(new GridAdapter(getActivity(),
                            listOfImagesPath));
                }
            } else if (requestCode == Gallery) {

                Uri selectedImageURI = data.getData();

                String imgcurTime = dateFormat.format(new Date());

                Bitmap bitmap = null;
                File imageDirectory = new File(GridViewDemo_ImagePath
                        + getRealPathFromURI(selectedImageURI));

                if (imageDirectory.exists()) {
                    bitmap = BitmapFactory.decodeFile(imageDirectory
                            .getAbsolutePath());
                }

                imageDirectory.mkdirs();

                String _path = getRealPathFromURI(selectedImageURI)
                        + imgcurTime + ".jpg";

                listOfImagesPath = null;

                listOfImagesPath = RetriveCapturedImagePath();

                LogUtil.d("listOfImagesPath" + listOfImagesPath.size());

                if (listOfImagesPath != null) {
                    listOfImagesPath.add(_path);

                    mCameraGrid.setAdapter(new GridAdapter(getActivity(),
                            listOfImagesPath));
                }
            }
        }
    }

从下面的方法获取所有保存的目录

private List<String> RetriveCapturedImagePath() {

        List<String> tFileList = new ArrayList<String>();
        File f = new File(GridViewDemo_ImagePath);
        if (f.exists()) {
            File[] files = f.listFiles();
            Arrays.sort(files);

            for (int i = 0; i < files.length; i++) {
                File file = files[i];
                if (file.isDirectory())
                    continue;
                tFileList.add(file.getPath());
            }
        }
        return tFileList;
    }

我的例外:

01-05 18:16:47.045: W/System.err(1546): java.io.FileNotFoundException: /storage/emulated/0/Download/IMG_0485.JPG2015-01-05 18:16:45.jpg: open failed: ENOENT (No such file or directory)
01-05 18:16:47.045: W/System.err(1546):     at libcore.io.IoBridge.open(IoBridge.java:409)
01-05 18:16:47.045: W/System.err(1546):     at java.io.FileInputStream.<init>(FileInputStream.java:78)

请帮我解决一下

【问题讨论】:

  • 目标路径 /storage/emulated/0/Download/ 上没有您要检索的文件,如果要显示相同的图像,存储和检索路径必须相同

标签: android gridview camera gallery


【解决方案1】:

这一行显示错误是什么

java.io.FileNotFoundException: /storage/emulated/0/Download/IMG_0485.JPG2015-01-05 18:16:45.jpg: open failed: ENOENT (No such file or directory)

在您获得图像的路径后,您将添加额外的 "imgcurTime " + ".jpg" 扩展名,所以我认为如果我是 rit 的话,这就是问题

String _path = getRealPathFromURI(selectedImageURI)
                    + imgcurTime + ".jpg";

将上面的行改为

String _path = getRealPathFromURI(selectedImageURI);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-12
    • 1970-01-01
    • 2022-01-16
    • 1970-01-01
    • 2013-04-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多