【问题标题】:Load Images from Assets in Widget remoteView从 Widget remoteView 中的 Assets 加载图像
【发布时间】:2016-10-01 01:55:57
【问题描述】:

我正在使用以下代码将我的图像加载到 Android 小部件ImageView

remoteViews.setImageViewResource(R.id.widget_image, R.drawable.image);

我必须将图像保存在drawable 文件夹中。是否可以将图像保存在资产中并将其加载到remoteView

【问题讨论】:

    标签: android remoteview


    【解决方案1】:

    使用BitmapFactory,您可以从AssetManager 提供的InputStream 中将assets/ 中的图像加载为Bitmap,然后使用RemoteViews#setImageViewBitmap() 方法将其设置在您的小部件上。

    例如:

    RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
    ...
    
    try {
        InputStream is = context.getAssets().open("image.jpg");
        Bitmap bmp = BitmapFactory.decodeStream(is);
    
        remoteViews.setImageViewBitmap(R.id.widget_image, bmp);
    }
    catch (IOException e) {
        e.printStackTrace();
    }
    

    【讨论】:

      【解决方案2】:

      解决办法如下:

       imageView = (ImageView)findViewById(R.id.iv);
              try {
                  // get input stream
                  InputStream ims = getAssets().open("download.jpg");
                  // load image as Drawable
                  Drawable d = Drawable.createFromStream(ims, null);
                  // set image to ImageView
                  imageView.setImageDrawable(d);
              }
              catch(IOException ex) {
                  return;
              }
      

      【讨论】:

        猜你喜欢
        • 2012-07-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-18
        • 1970-01-01
        • 1970-01-01
        • 2019-11-28
        相关资源
        最近更新 更多