2015-05-12 64 views
1

我正在動態創建Imageviews並顯示它。但我面臨重新加載問題時滾動,所以我決定使用NetworkImageView,但在這裏我無法設置位圖。它沒有顯示任何錯誤,但沒有顯示任何圖像。 下面我的代碼...將位圖設置爲NetworkImageView

final NetworkImageView imageView = new NetworkImageView(context); 
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(150, 
      200); 
    params.weight = 1; 
    //imageView.setImageBitmap(null); 
    imageView.setLayoutParams(params); 
    imageView.setMaxHeight(200); 
    imageView.setMaxWidth(150); 
    // bitmap =// 
    // BitmapFactory.decodeResource(context.getResources(),R.drawable.ic_menu_folder); 
    // Log.i("Bitmaps Counts", String.valueOf(pos)); 
    /* comment all this... 
    new Thread() { 
     public void run() { 


       bitmap = downloadImage(tempValues.getItemAbsolutePath()); 


      ((Activity) context).runOnUiThread(new Runnable() { 
       @Override 
       public void run() { 

        imageView.setImageBitmap(bitmap); 

       } 
      }); 

     } 
    }.start();*/ 
    RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext()); 
    ImageLoader mImageLoader = new ImageLoader(requestQueue,new LruBitmapCache(1000000)); 
    mImageLoader.get(tempValues.getItemAbsolutePath(), 
      ImageLoader.getImageListener(imageView, 
        R.android.defaultimg, 
        R.android.errorimg)); 

    layout.addView(imageView); 

downloadImage代碼..

/* unused method 
     private Bitmap downloadImage(String url) { 
     Bitmap bitmap = null; 
     InputStream stream = null; 
     BitmapFactory.Options bmOptions = new BitmapFactory.Options(); 
     bmOptions.inSampleSize = 1; 

     try { 
      stream = getHttpConnection(url); 
      bitmap = BitmapFactory.decodeStream(stream, null, bmOptions); 
      stream.close(); 

     } catch (IOException e1) { 
      e1.printStackTrace(); 
     } 
     return bitmap; 
    } 

    private InputStream getHttpConnection(String urlString) throws IOException { 
     InputStream stream = null; 
     URL url = new URL(urlString); 
     URLConnection connection = url.openConnection(); 

     try { 
      HttpURLConnection httpConnection = (HttpURLConnection) connection; 
      httpConnection.setRequestMethod("GET"); 
      httpConnection.connect(); 

      if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) { 
       stream = httpConnection.getInputStream(); 
      } 
     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } 
     return stream; 
    }*/ 

錯誤日誌

05-12 19:11:13.971:E/AndroidRuntime(11177):致命異常:螺紋-7325 05-12 19:11:13.971:E/AndroidRuntime(11177):進程:com.dar.app,PID:11177 05-12 19:11:13.971:E/AndroidRuntime(11177):java.lang .NegativeArraySizeException:-350 05-12 19:11:13.971:E/AndroidRun time(11177):at com.android.volley.toolbox.DiskBasedCache.streamToBytes(DiskBasedCache.java:316) 05-12 19:11:13.971:E/AndroidRuntime(11177):at com.android.volley.toolbox。 DiskBasedCache.get(DiskBasedCache.java:117) 05-12 19:11:13.971:E/AndroidRuntime(11177):在com.android.volley.CacheDispatcher.run(CacheDispatcher.java:100)

+0

我是新至Android至少請指導我 – Mister

+0

好吧,看我的答案 –

+0

請我更新的代碼和我在做什麼我是creati動態地傳遞給持有者。下面的代碼我不明白 – Mister

回答

1

可以使用凌空圖像downloder

很容易 默認IMG設置...用於圖像加載器請求.. 如果使用URL找到圖像它將設置..或者如果出錯,將設置錯誤image..this方式,您可以通知有關加載IMG錯誤

RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext()); 
ImageLoader mImageLoader = new ImageLoader(requestQueue,new LruBitmapCache(1000000)); 
mImageLoader.get(<img-url>, 
       ImageLoader.getImageListener(<imageview-object>, 
         R.drawable.defaultimg, 
         R.drawable.errorimg)); 
+0

ü可以編輯我的代碼 – Mister

+0

@Mister如果編輯的代碼work..then我會更新我的答案..to接受這個問題的答案...多數民衆贊成在未來其他發現很容易 –

+0

R.android.defaultimg爲Android我得到錯誤 – Mister

1

FINAL CODE

public class DownloadImageTask extends AsyncTask<String, Void, Bitmap> { 
    private ImageView imageView; 
    private Bitmap image; 
    private LinearLayout linear; 

    public DownloadImageTask(ImageView imageView, LinearLayout linear) { 
     this.imageView = imageView; 
     this.linear = linear; 
    } 

    protected Bitmap doInBackground(String... urls) { 
     String urldisplay = urls[0]; 
     try { 
      InputStream in = new java.net.URL(urldisplay).openStream(); 
      image = BitmapFactory.decodeStream(in); 
     } catch (Exception e) { 
      image = null; 
     } 
     return image; 
    } 

    @SuppressLint("NewApi") 
    protected void onPostExecute(Bitmap result) { 
     if (result != null) { 
      imageView.setImageBitmap(result); 
      linear.addView(imageView); 
     } 
    } 
} 

現在,在你的代碼中調用:

final ImageView imageView = new ImageView(context); 
final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(150, 200); 
params.weight = 1; 
imageView.setLayoutParams(params); 
imageView.setMaxHeight(200); 
imageView.setMaxWidth(150); 

new DownloadImageTask(imageView, layout).execute(tempValues.getItemAbsolutePath());