2013-01-19 208 views
0

我正在從我的服務器上下載請求的圖片,下載後這張圖片成功顯示,但是當我嘗試在SD卡上存儲相同的圖片時,它返回null。 這是我下載的圖像並保存它。我我得到空在通話過程中bitmap.compress(代碼)Bitmap.compress總是返回null

void saveImage() { 


    String root = Environment.getExternalStorageDirectory().toString(); 
    File myDir = new File(root + "/saved_images"); 
    myDir.mkdirs(); 
    String fname = "Image.png"; 
    File file = new File (myDir, fname); 

    ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
    message_bitmap.compress(Bitmap.CompressFormat.JPEG, 40, bytes); 


    //write the bytes in file 
    FileOutputStream fo; 
    try { 
     fo = new FileOutputStream(file); 

     fo.write(bytes.toByteArray()); 
     fo.close(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    /*if (file.exists()) file.delete(); 
    try { 
      FileOutputStream out = new FileOutputStream(file); 
      message_bitmap.compress(Bitmap.CompressFormat.PNG, 90, out); 
      out.flush(); 
      out.close(); 

    } catch (Exception e) { 
      e.printStackTrace(); 
    }*/ 
} 

static public Bitmap downloadBitmap(String url) { 
    final DefaultHttpClient client = new DefaultHttpClient(); 
    final HttpGet getRequest = new HttpGet(url); 

    try { 
     HttpResponse response = client.execute(getRequest); 
     final int statusCode = response.getStatusLine().getStatusCode(); 
     if (statusCode != HttpStatus.SC_OK) { 
      Log.w("ImageDownloader", "Error " + statusCode + " while retrieving bitmap from " + url); 
      return null; 
     } 

     final HttpEntity entity = response.getEntity(); 
     if (entity != null) { 
      InputStream inputStream = null; 
      try { 
       inputStream = entity.getContent(); 
       final Bitmap bitmap = BitmapFactory.decodeStream(new FlushedInputStream(inputStream)); 
       return bitmap; 
      } finally { 
       if (inputStream != null) { 
        inputStream.close(); 
       } 
       entity.consumeContent(); 
      } 
     } 
    } catch (Exception e) { 
     // Could provide a more explicit error message for IOException or IllegalStateException 
     getRequest.abort(); 
     Log.w("ImageDownloader", "Error while retrieving bitmap from " + url + e.toString()); 
    } finally { 
     if (client != null) { 

     } 
    } 
    return null; 
     } 

回答

0

我覺得你已經很接近那裏。如何相似,其下載下面有什麼東西保存的圖像:

 try { 
      img_value = new URL("your_url"); 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } 
     try { 
      mIcon1 = BitmapFactory.decodeStream(img_value.openConnection() 
        .getInputStream()); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     String filename = "your_filename.jpg"; 
     File file = new File(dir, filename); 
     if (file.exists()) { 
      try{ 
       file.delete(); 
      }catch(Exception e){ 
       //sdcard plugged in 
      } 
     } 
     try { 
      FileOutputStream out = new FileOutputStream(file); 
      mIcon1.compress(Bitmap.CompressFormat.JPEG, 90, out); 
      out.flush(); 
      out.close(); 

     } catch (Exception e) { 
      e.printStackTrace();//will occur if the phone is plugged in 
     } 
+0

Bitmap.compress返回null我總是正如我所說,我休息的代碼是完全一樣的 – user1918034

+0

你壓縮()調用使用ByteOutputStream不是一個FileOutputStream 。你確定你在saveImage()的底部註釋掉的那段代碼不工作嗎?上面的代碼對我來說是正確的。 – MarkMan