我在显示自定义手机图库时遇到了麻烦。
首先,我需要在gridview中显示图像文件夹(例如电话库),并且在选择任何文件夹时,它必须显示其中的图片并且应该允许多个选择,以便我可以选择多张图片。
可以实现吗?
如果是,该怎么做?
答案 0 :(得分:0)
要打开图库:
//open gallery to choose image
private void captureImage() {
// Create intent to Open Image applications like Gallery, Google Photos
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// Start the Intent
startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
}
从图库中选择图像时,会调用以下函数,实现它以保存所选图像。
// When Image is selected from Gallery
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
// When an Image is picked
if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
&& null != data) {
// Get the Image from data
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
// Get the cursor
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
// Move to first row
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imgPath = cursor.getString(columnIndex);
cursor.close();
// Get the Image's file name
String fileNameSegments[] = imgPath.split("/");
fileName = fileNameSegments[fileNameSegments.length - 1];
// successfully selected the image
// launching upload activity
tvCapturePicture.setText(fileName);
} else {
Toast.makeText(this, "You haven't picked any Image",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(this, "Something went wrong ,please try again , ",
Toast.LENGTH_LONG).show();
}
}
答案 1 :(得分:0)
根据您的要求,按照本教程,它将提供想法..
http://www.technotalkative.com/android-select-multiple-photos-from-gallery/