2012-12-29 154 views
1

我正在使用活動來顯示完整圖像,並且用戶有一個按鈕來保存它。將位圖保存到SD卡後圖像質量下降

圖像正從我在互聯網上託管圖像的URL的imageview中加載。

我將圖像另存爲PNG文件。

保存圖像後,質量下降。我知道PNG不是無損的,但有沒有什麼辦法可以保存圖像,因爲它不會影響質量或分辨率?

下面是代碼:

buttonSave.setOnClickListener(new Button.OnClickListener() 
{ 
    public void onClick(View arg0) 
    { 
     fullSizeImage.buildDrawingCache(); 
     Bitmap bm=fullSizeImage.getDrawingCache(); 
     String root = Environment.getExternalStorageDirectory().toString(); 
     File myDir = new File(root + File.separator + "MyWallpapers"); 
     if(!myDir.exists()) 
     { 
      myDir.mkdir(); 
     } 
     String fname = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date())+".png"; 
     File file = new File (myDir, fname); 
     /*if (file.exists()) file.delete(); */ 
     try 
     { 
      //FileOutputStream out = new FileOutputStream(file); 
      /*FileOutputStream out = new FileOutputStream(file); 

      bm.compress(Bitmap.CompressFormat.PNG, 100, out);*/ 

      ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
      bm.compress(Bitmap.CompressFormat.PNG, 100, bytes); 

      file.createNewFile(); 
      //write the bytes in file 
      FileOutputStream fo = new FileOutputStream(file); 
      fo.write(bytes.toByteArray()); 
      bytes.flush(); 
      bytes.close(); 
      /*out.flush(); 
      out.close();*/ 
      Toast.makeText(FullSizeImageDisplay.this, "Wallpaper Saved in SDcard/MyWallpapers folder",Toast.LENGTH_SHORT).show(); 
      String[] paths = {root + File.separator + "MyWallpapers"}; 

      String[] mediaType = {"image/png"}; 
      MediaScannerConnection.scanFile(FullSizeImageDisplay.this, paths, mediaType, null); 
      ContentValues values = new ContentValues(); 
      values.put(MediaStore.Images.Media.DATA, file.getAbsolutePath()); 
      values.put(MediaStore.Images.Media.MIME_TYPE, "image/png"); 

      getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); 
     } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 

     OutputStream fOut = null; 
     Uri outputFileUri; 

     } 
    }); 
} 

回答

1
fullSizeImage.buildDrawingCache(); 
Bitmap bm=fullSizeImage.getDrawingCache(); 

哪裏fullSizeImage是可能的形象圖。

我看到加載圖像時出現問題。您必須使用類似BitmapFactory.Options

製作以下更改

BitmapFactory.decodeStream(<param1>,param2,null); 
+0

BitmapFactory.decodeStream(,參數2,NULL);是替代什麼? –

+0

正在做任何懶加載類的東西?或解碼url只爲一次圖像的bmp? –

+0

將url解碼爲一個圖像的bmp。 –