我需要创建一个应用程序,从我的 Android 应用程序打开 Android 图库,我应该只能查看图库中的 .jpeg 图像,其他图像(如 .png 或任何其他格式)应该被删除。
有人可以建议如何做到这一点吗?
look this example for txt you can do the same with jpg files, just list the files and select only that you want to use. Android: File list in ListView. NOTE: The folder for gallery is Enviroment.DIRECTORY_DCIM
这是代码:
Intent intent = new Intent();
intent.setType("image/jpeg");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Main.this.startActivity(intent);
但不确定是否所有 android 画廊应用程序都会额外支持该类型
干得好...
// Directory path here
String path = "PATH/TO/GALLERY";
File folder = new File(path);
File[] directoryListing = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
files = listOfFiles[i].getName();
if (files.endsWith(".JPG") || files.endsWith(".JPEG")) {
// Handle your file here
}
}
}
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/jpg");
startActivityForResult(photoPickerIntent, 1);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch(requestCode) {
case 1:
if(resultCode == RESULT_OK){
Uri selectedImage = imageReturnedIntent.getData();
InputStream imageStream = getContentResolver().openInputStream(selectedImage);
Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream);
}
}
}