Bitmap.compress总是返回null

问题描述:

我正在从我的服务器上下载请求的图片,下载后这张图片成功显示,但是当我尝试在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; 
     } 

我觉得你已经很接近那里。如何相似,其下载下面有什么东西保存的图像:

 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