在 gridview 中显示图库和相机中的图像

Show images in gridview from both gallery and camera

您好,在我的应用程序中,我使用了相机和画廊来获取图像并在 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)

请帮我解决问题

这一行显示错误是什么

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" 扩展,所以我认为这就是问题所在

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

将上面一行改为

String _path = getRealPathFromURI(selectedImageURI);