2017-06-18 73 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 
     } 
    });