2017-08-13 38 views
-4

我不能字符串轉換成屬性集中。我一直收到一個錯誤,我的論點不匹配。我怎麼會一個字符串數據轉換爲圖像

我該如何解決這個問題?

+0

你的意思是你想一個base64字符串轉換爲圖像? –

+0

沒有我的意思是如何將URL字符串轉換爲圖像。 – ZachGiovanelli

+0

最終LoaderImageView圖像=新LoaderImageView(此,getAnImageUrl());它說的AttributeSet不能應用於java.string「getAnImageUrl」我不明白這個問題,因爲在所有它應該工作。它說參數不匹配。謝謝。 – ZachGiovanelli

回答

1

下面的代碼將幫助您將base64字符串轉換成圖像。

public void saveAsImage(String attachThumbnail) { 

    byte[] previewImage = Base64.decode(attachThumbnail, Base64.NO_WRAP); 
    final File file = new File("[dir]", "image_name" + ".jpg"); 
    save(file, previewImage); 
} 

/** 
* To save the content in a file. 
* 
* @param file file to be store. 
* @param content actual data. 
*/ 
public static void save(File file, byte[] content) { 
    FileOutputStream fos = null; 
    try { 
     fos = new FileOutputStream(file); 

     fos.write(content); 

    } catch (FileNotFoundException e) { 
     // e.printStackTrace(); 
    } catch (IOException e) { 
     // e.printStackTrace(); 
    } finally { 
     try { 
      if (fos != null) { 
       fos.close(); 
      } 
     } catch (IOException ioe) { 

     } 
    } 
} 
0

好的,如果你想從URL加載圖片,你可以做的最好的事情就是使用一個好的庫。我會建議使用Glide庫,因爲它快速且穩定。爲此,該行添加到您的build.gradle文件dependencies部分:

compile 'com.github.bumptech.glide:glide:3.7.0' 

然後使用此方法加載所需imageUrl到所需ImageView

public void loadImageWithGlide(Context context, String imageUrl, ImageView holder) { 
    Glide.with(context).load(imageUrl) 
      .thumbnail(0.5f) 
      .crossFade() 
      .dontAnimate() 
      .diskCacheStrategy(DiskCacheStrategy.ALL) 
      .into(holder); 
} 
相關問題