获取Nexus 4上的所有图库图像

时间:2014-07-17 18:03:43

标签: android nexus-4 android-external-storage

我正在开发一个简单的图库应用,在其中我搜索包含图像的文件夹,然后显示该特定文件夹的图像。首先我使用Environment.getExternalStorageDirectory(),然后递归搜索具有图像的文件夹。它运行良好的模拟器和设备。当我的客户端在Nexus 4上安装应用程序时,没有任何内容正在加载。我看过一些帖子说Nexus系列没有任何外置SD插槽。我没有Nexus 4进行调试,客户端也是非技术性的。他可以自己排除故障,找出问题的原因。在这方面有人可以提供帮助吗?我认为问题出在Environment.getExternalStorageDirectory()电话中,这在Nexus中不适用。知道如何解决这个问题吗? 这是我正在使用的代码:

    private File createImageFile() throws IOException {
        // Create an image file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";
        File cacheDir = null;
        if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {
            cacheDir=new File(android.os.Environment.getExternalStorageDirectory().getAbsolutePath(), "MyImages");
        }
        else {
            cacheDir = getCacheDir();
        }
        if(!cacheDir.exists()) {
            cacheDir.mkdir();
        }
//      File storageDir = Environment.getExternalStoragePublicDirectory(
//              Environment.DIRECTORY_PICTURES);
        File image = File.createTempFile(
                imageFileName,  /* prefix */
                ".jpg",         /* suffix */
                cacheDir      /* directory */
                );

        // Save a file: path for use with ACTION_VIEW intents
        mCurrentPhotoPath = "" + image.getAbsolutePath();
        return image;
    }

    public void dispatchTakePictureIntent() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        // Ensure that there's a camera activity to handle the intent
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            // Create the File where the photo should go
            File photoFile = null;
            try {
                photoFile = createImageFile();
            } catch (IOException ex) {
                // Error occurred while creating the File
                ex.printStackTrace();
            }
            // Continue only if the File was successfully created
            if (photoFile != null) {
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                        Uri.fromFile(photoFile));
                startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
            }
        }
    }

这里我添加的权限显示:

<uses-permission
    android:name="android.permission.WRITE_EXTERNAL_STORAGE"
    android:maxSdkVersion="18" />

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

<uses-feature
    android:name="android.hardware.camera"
    android:required="true" />

谢谢!

1 个答案:

答案 0 :(得分:0)

我认为您应该尝试使用MediaStore.Images组件。使用光标加载图像。请参阅文档:http://developer.android.com/reference/android/provider/MediaStore.html