And*_*per 4 android android-gallery android-camera android-external-storage
我使用下面的代码从DCIM中的Camera文件夹中获取所有图像并显示在我的应用程序中.但我想在我的应用程序中显示设备上的所有图像,无论它们存储在设备上的哪个位置.我怎样才能做到这一点?
String ExternalStorageDirectoryPath = Environment
.getExternalStorageDirectory()
.getAbsolutePath();
String targetPath = ExternalStorageDirectoryPath + "/DCIM/Camera";
images=new ArrayList<String>();
File targetDirector = new File(targetPath);
File[] files = targetDirector.listFiles();
for (File file : files) {
images.add(file.getAbsolutePath());
}
gallerylist=new CameraGalleryAdapter(getActivity().getApplicationContext(),R.layout.giphy_grid,images);
gridview.setAdapter(gallerylist);
Run Code Online (Sandbox Code Playgroud)
首先,你必须检查SD卡是否可用:
Boolean isSDPresent = android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
Run Code Online (Sandbox Code Playgroud)
如果存在SD卡,请使用:
final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID };
final String orderBy = MediaStore.Images.Media._ID;
//Stores all the images from the gallery in Cursor
Cursor cursor = getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null,
null, orderBy);
//Total number of images
int count = cursor.getCount();
//Create an array to store path to all the images
String[] arrPath = new String[count];
for (int i = 0; i < count; i++) {
cursor.moveToPosition(i);
int dataColumnIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
//Store the path of the image
arrPath[i]= cursor.getString(dataColumnIndex);
Log.i("PATH", arrPath[i]);
}
// The cursor should be freed up after use with close()
cursor.close();
Run Code Online (Sandbox Code Playgroud)
上面的代码将列出SD卡中的所有图像,用于从内存中获取图像只需更换
MediaStore.Images.Media.EXTERNAL_CONTENT_URI
Run Code Online (Sandbox Code Playgroud)
同
MediaStore.Images.Media.INTERNAL_CONTENT_URI
Run Code Online (Sandbox Code Playgroud)
使用相同的代码片段.
| 归档时间: |
|
| 查看次数: |
7230 次 |
| 最近记录: |