我想知道如何从图库中获取预先保存的图像,然后将其显示到屏幕上。任何教程/有用的链接和信息将不胜感激。如果您希望我进一步解释任何内容,请询问。
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, 1);
此 Intent 用于从 SD 卡中选取图像并使用onActivityResult()
用于获取图像并在中显示图像ImageView.
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 1:
{
if (resultCode == RESULT_OK)
{
Uri photoUri = data.getData();
if (photoUri != null)
{
try {
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(photoUri, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
Bitmap bMap = BitmapFactory.decodeFile(filePath);
image.setImageBitmap(bMap);
}catch(Exception e)
{}
}
}
}
}
现在我们从图库中获取选定的图像,然后将图像设置为 ImageVIew.Hereimage.setImageBitmap(bMap);
将图像设置为 ImageView。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)