【问题标题】:Android: Fetch image from gallery without user interactionAndroid:在没有用户交互的情况下从图库中获取图像
【发布时间】: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


【解决方案1】:

我认为这个问题有两个部分:从手机图库中获取图片和更新 UI。

看来您的方法对于从图库中获取图像是正确的。另一种方法在这里详述:http://developer.android.com/guide/topics/data/data-storage.html#filesExternal

您看到的 RuntimeException 是因为您尝试在工作线程上更新 UI。 Android 框架要求所有 UI 更改都发生在 UI 线程上。您必须在运行查询后调用 Activity.runOnUiThread() 来更新 UI。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 2016-10-07
    • 1970-01-01
    • 2014-11-17
    • 1970-01-01
    • 1970-01-01
    • 2023-01-14
    相关资源
    最近更新 更多