我有一個imageView和一個位圖,我以前使用AsyncTask從互聯網下載。這些圖片是Google Play應用的縮略圖,例如:https://lh6.ggpht.com/y8KPuTX0t3-dXVejh0NKvpPqtseEBiFp65Epyu4r5PxDs4GKLf_5sQaPrh7e5fGPLHxs=w300-rw。正如您所看到的,圖標的尺寸爲300x300,但您可以更改它修改網址末尾的數字。在一些設備(例如Samsung Galaxy S5或Motorola G)中,位圖被正確加載到imageView中,但在其他設備中卻沒有(除非我將尺寸從300x300更改爲50x50,例如它的工作原理)。我試過不同的方式來下載位圖,但它似乎不是問題所在。我試圖捕捉任何拋出的異常,但似乎沒有人拋出(我認爲這可能是內存問題,但沒有關於它)。相關代碼:位圖只出現在某些設備
class BitmapDownloaderTask extends AsyncTask<String, Void, Bitmap> {
private final WeakReference<ImageView> imageViewReference;
HashMap<String, Bitmap> images;
String url;
Context context;
String titleImage;
TextView titleTextView;
/**
* Constructor. Create a new BitMapDownloader object.
* @param imageView view where introduce the selected image
*/
public BitmapDownloaderTask(ImageView imageView, String url, HashMap<String, Bitmap> images, Context context, String titleImage, TextView titleTextView) {
this.images = images;
imageViewReference = new WeakReference<ImageView>(imageView);
this.url = url;
this.context = context;
this.titleImage = titleImage;
this.titleTextView = titleTextView;
}
@Override
/**
* Download method. Run in the task thread
* @param params comes from the execute() call. params[0] is the url
* @return a Bitmap with the image
*/
protected Bitmap doInBackground(String... params) {
return downloadBitmap(params[0]);
}
@Override
/**
* Method to associates the image to the imageView once is downloaded
*/
protected void onPostExecute(Bitmap bitmap) {
try{
if (isCancelled()) {
bitmap = null;
}
if (imageViewReference != null) {
ImageView imageView = imageViewReference.get();
if (imageView != null) {
if(bitmap!=null){
imageView.setImageBitmap(bitmap);
images.put(url, bitmap);
titleTextView.setText(titleImage);
ImageDownloader.doAnimation(imageView, context);
}
}
}
}catch(Exception e){
Log.d("Error", " Crazy Error:" + e);
}
}
public static Bitmap downloadBitmap(String src) {
try {
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (Exception e) {
Log.v("Error", "Error:" + e);
e.printStackTrace();
}
return null;
}
我通過執行此異步任務:
public void downloadAndSetImage(Context context, String url, ImageView imageView, String titleImage, TextView titleTextView) {
BitmapDownloaderTask task = new BitmapDownloaderTask(imageView, url, images, context, titleImage, titleTextView);
if(!images.containsKey(url)){
task.execute(url);
}
else{
imageView.setImageBitmap(images.get(url));
titleTextView.setText(titleImage);
doAnimation(imageView, context);
}
}
的ImageView的是如此簡單:
<RelativeLayout
android:id="@+id/suggestedAppRelativeLayout1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:gravity="center_vertical" >
<ImageView
android:id="@+id/suggestedAppIconImageView1"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_centerHorizontal="true" />
我想這個問題來調整位圖的ImageView的。這就是爲什麼我試圖像下面不同方法的位圖設置爲ImageView的面前,但它沒有工作:
bmp=Bitmap.createScaledBitmap(bmp, imageView.getWidth(), imageView.getHeight(), true);
任何幫助或想法將是真正偉大!乾杯!
試圖找到問題的根源。首先檢查下載圖像任務的一切是否按預期工作。您可以保存位圖來檢查尺寸是否正常。如果一切順利,位圖調試縮放位圖的代碼。 – 2014-11-04 10:57:33