我正在开发一个简单的图库应用程序,其中我搜索包含图像的文件夹,然后显示该特定文件夹的图像。首先,我使用Environment.getExternalStorageDirectory()
,然后递归搜索具有图像的文件夹。它工作的很好的模拟器和设备。当我的客户端在Nexus 4上安装应用程序时,无法加载任何内容。我看到一些说Nexus系列没有任何外部SD插槽的帖子。我没有用于调试的Nexus 4,客户端也是非技术型的。他可以自己排查问题的原因。任何人都可以在这方面提供帮助吗?我认为问题出在Environment.getExternalStorageDirectory()
调用中,这不适用于Nexus。任何想法如何解决这个问题? 这里是代码剪断我使用:获取Nexus 4上的所有图库图像
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" />
谢谢!
请参阅:http://stackoverflow.com/questions/7616974/how-to-check-internal-and-external-storage-if-exist – Shadesblade