2017-06-18 69 views
0

我试图通过从互联网下载图像来设置壁纸,但它显示没有找到“get()方法”。 我的代码: 在这段代码wall_set是一个按钮的名称显示get()方法未找到

wall_set.setOnClickListener(
      new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        Bitmap result=Glide.with(getApplicationContext()) 
          .load("http://www.sport-stickers.com/images/2013/CARTOON%20IRON%20ONS/Doraemon/Doraemon%20Iron%20ons%20(Wall%20Stickers)%20N3715.jpg").get(); 


        WallpaperManager wallpaperManager = WallpaperManager.getInstance(getApplicationContext()); 
        try { 
         wallpaperManager.setBitmap(result); 
        } catch (IOException ex) { 
         ex.printStackTrace(); 
        } 
       } 
      } 
    ); 
+1

查看[Glide文档](https://github.com/bumptech/glide/wiki)。看起来你应该使用'into()'而不是'get()'。 – rundavidrun

+0

它看起来像你缺少添加asBitmap()看到这个答案https://stackoverflow.com/a/27394484/7134908 –

回答

1

改变你的这部分代码:

Bitmap result=Glide.with(getApplicationContext()) 
         .load("http://www.sport-stickers.com/images/2013/CARTOON%20IRON%20ONS/Doraemon/Doraemon%20Iron%20ons%20(Wall%20Stickers)%20N3715.jpg").asBitmap().get(); 

添加 “asBitmap”

you might need to add 
asBitmap().into(20, 20). // Width and height 
在以下

+0

感谢您的响应先生,但即使添加“.asBitmap()”它显示相同的错误 – user8027365

0

如果你关注Glide示例的用法,它会得到那个得到()方法属于java.util.concurrent.Future对象。未来的班级定义由官方文档给出如下。

public interface Future<V> Future表示异步计算的结果 。提供的方法用于检查计算是否完成,是否等待完成,以及检索计算结果 。只有在计算完成时才能使用 方法获取结果,如果需要,可以将其阻塞 ,直到准备就绪。取消是通过取消方法执行的。 提供了其他方法来确定任务是否正常完成或取消了 。一旦计算完成,计算不能被取消。如果您想为可取消性使用Future而不提供可用结果,则您可以通过 声明Future形式的类型,并作为 基础任务的结果返回null。

用法示例(注意,下列各类都是编造的。)

interface ArchiveSearcher { String search(String target); } 
class App { 
    ExecutorService executor = ... 
    ArchiveSearcher searcher = ... 
    void showSearch(final String target) 
     throws InterruptedException { 
    Future<String> future 
     = executor.submit(new Callable<String>() { 
     public String call() { 
      return searcher.search(target); 
     }}); 
    displayOtherThings(); // do other things while searching 
    try { 
     displayText(future.get()); // use future 
    } catch (ExecutionException ex) { cleanup(); return; } 
    } 
} 

让我们看看会一步发生了一步:

Bitmap theBitmap = Glide. 
     with(this). //in Glide class and returns RequestManager 
     load(image_url). // in RequestManager and returns RequestBuilder<Drawable> 
     asBitmap(). //in RequestBuilder and returns RequestBuilder<Bitmap> 
     submit(). // in RequestBuilder and returns FutureTarget<TranscodeType> which extends Future<> 
     get(); // this belongs to Future object which is the result of async computation 

public static RequestManager with(Context context) { 
    return getRetriever(context).get(context); 
    } 

public RequestBuilder<Drawable> load(@Nullable Object model) { 
    return asDrawable().load(model); 
    } 


    public RequestBuilder<Bitmap> asBitmap() { 
    return as(Bitmap.class).transition(new GenericTransitionOptions<Bitmap>()) 
      .apply(DECODE_TYPE_BITMAP); 
    } 

public FutureTarget<TranscodeType> submit() { 
    return submit(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL); 
    } 


public interface FutureTarget<R> extends Future<R>, Target<R> { 
} 

但更恰当和安全解决方法是使用回调

Glide 
    .with(this) 
    .load(image_url) 
    .asBitmap() 
    .into(new SimpleTarget<Bitmap>(100,100) { 
     @Override 
     public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) { 
      //resource is the resulting bitmap 
     } 
    });