2014-02-21 73 views
1

我遇到了一個問題,即當我向磁盤寫入位圖時,它會寫入磁盤,但它會寫爲極小的圖像(文件大小爲3kb或更小)。防止位圖被寫入正在縮放的​​磁盤

我已檢查源圖像的確是正確的尺寸,但輸出圖像似乎收縮,儘管配置位圖選項不縮放。

@Override 
protected Void doInBackground(PPImage... params) { 
    String filename = "pp_" + position + ".jpg"; 
    File externalStorageDirectory = Environment.getExternalStorageDirectory(); 
    final File destination = new File(externalStorageDirectory, filename); 

    BitmapFactory.Options opts = new BitmapFactory.Options(); 
    opts.inSampleSize = 16; 
    opts.inPurgeable = true; 
    opts.inScaled = false; 

    decode(opts, Uri.parse(params[0].getUri()), getActivity(), new OnBitmapDecodedListener() { 
     @Override 
     public void onDecoded(Bitmap bitmap) { 
      try { 
       FileOutputStream out = new FileOutputStream(destination, false); 
       writeImageToFileTask.this.holder.pathToImage = destination.getAbsolutePath(); 
       bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out); 
       out.flush(); 
       out.close(); 

       MediaStore.Images.Media.insertImage(getActivity().getContentResolver(), destination.getAbsolutePath(), destination.getName(), destination.getName()); 
      } catch (FileNotFoundException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 

    return null; 
} 

private void decode(BitmapFactory.Options options, Uri mUri, Context mContext, OnBitmapDecodedListener listener) { 
    try { 
     InputStream inputStream; 
     if (mUri.getScheme().startsWith("http") || mUri.getScheme().startsWith("https")) { 
      inputStream = new URL(mUri.toString()).openStream(); 
     } else { 
      inputStream = mContext.getContentResolver().openInputStream(mUri); 
     } 

     Bitmap bitmap = BitmapFactory.decodeStream(inputStream, null, options); 

     listener.onDecoded(bitmap); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

如何確保寫入文件的圖像尺寸與原始源圖像相同?

回答

2

你已經在你的代碼中指定的樣本大小,這將導致調整:

opts.inSampleSize = 16; 

就刪除此行,和輸出圖像的尺寸應該是相同的。

關於inSampleSize的使用,根據official doc

例如,inSampleSize == 4點返回的形象是1/4原來的 寬度/高度,和1/16的數量的像素。任何值 < = 1的處理方式與1相同。