我想在手机图库中获取图片并在gridview上显示。 我使用Cursor:
final String[] columns = {MediaStore.Images.Media.DATA,
MediaStore.Images.Media._ID};
final String orderBy = MediaStore.Images.Media.DATE_TAKEN + " DESC";
Cursor imagecursor = getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null,
null, orderBy);
我的手机没有SD卡,出现Unfortunately app has stopped
错误。
当我用EXTERNAL_CONTENT_URI
替换INTERNAL_CONTENT_URI
时,没有任何错误,没有显示图像。
答案 0 :(得分:2)
您可以通过启动此意图来获取图片:
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);//
startActivityForResult(Intent.createChooser(intent, "Select File"), SELECT_FILE);
然后在onActivityResult()
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == SELECT_FILE)
onSelectFromGalleryResult(data);
}
}
private void onSelectFromGalleryResult(Intent data) {
if (data != null) {
try {
bm = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), data.getData());
} catch (IOException e) {
e.printStackTrace();
}
}
让我知道它是否对您有所帮助