Bitmap.compress始终返回null

时间:2013-01-19 09:19:07

标签: android android-image android-file android-drawable

我从我的服务器下载请求的图像,下载后该图像成功显示但是当我尝试在SD卡上存储相同的图像时,它返回null。 这是我下载图像并保存它的代码。我在调用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;
        }

1 个答案:

答案 0 :(得分: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
        }