【发布时间】:2011-10-03 05:06:53
【问题描述】:
我想从图库中获取图片(如果存在),而无需用户从图库中选择图片。我想以编程方式执行此操作。我尝试了以下方法:
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = new Activity().managedQuery( MediaStore.Images.Media.INTERNAL_CONTENT_URI, projection, null, null, null);
if (cursor != null) {
// HERE YOU WILL GET A NULLPOINTER IF CURSOR IS NULL
// THIS CAN BE, IF YOU USED OI FILE MANAGER FOR PICKING THE MEDIA
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
BitmapFactory.Options options = new BitmapFactory.Options();;
options.inSampleSize = 1;
Bitmap bm = BitmapFactory.decodeFile(
cursor.getString(column_index),options);
remoteView.setImageViewBitmap(R.id.image,bm);
我从工作线程而不是主 UI 线程调用这段代码。这种方法正确吗?如果不是,那么在没有用户交互的情况下从图库中获取图像的更好方法是什么?
谢谢。
【问题讨论】:
-
您为什么关注这种方法?它不工作吗?
-
是的,不是!错误是:java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare().
标签: android android-widget android-appwidget photo-gallery