从图库视图和相机中显示图像

时间:2015-01-05 13:02:58

标签: android gridview camera gallery

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

请帮我解决问题

1 个答案:

答案 0 :(得分: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);