2014-01-16 58 views
0

我從Facebook獲取圖像並將其寫入SD卡,但圖像質量很低。以下是我的代碼來讀取和寫入:從Facebook獲取並寫入SD的圖像質量非常低。

try 
     { 
      URL url = new URL(murl); 
      HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
      connection.setDoInput(true); 
      connection.connect(); 
      InputStream input = connection.getInputStream(); 
      Bitmap myBitmap = BitmapFactory.decodeStream(input); 

      data1 = String.valueOf(String.format(getActivity().getApplicationContext().getFilesDir()+"/Rem/%d.jpg",System.currentTimeMillis())); 

      FileOutputStream stream = new FileOutputStream(data1); 

      ByteArrayOutputStream outstream = new ByteArrayOutputStream(); 
      myBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outstream); 
      byte[] byteArray = outstream.toByteArray(); 

      stream.write(byteArray); 
      stream.close(); 


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

下面的代碼我用來顯示相同​​的圖像:即使使用選項後

    File IMG_FILE = new File(IMAGE_CONTENT); 
        B2.setVisibility(View.INVISIBLE); 
        Options options = new BitmapFactory.Options(); 
        options.inScaled = false; 
        options.inDither = false; 
        options.inPreferredConfig = Bitmap.Config.ARGB_8888; 
        Bitmap bitmap = BitmapFactory.decodeFile(IMG_FILE.getAbsolutePath(),options); 
        iM.setImageBitmap(bitmap); 

質量仍然較低。可以做些什麼來改善這一點?

回答

2

從URL保存圖像到SD卡中使用此代碼

try 
{ 
    URL url = new URL("Enter the URL to be downloaded"); 
    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 
    urlConnection.setRequestMethod("GET"); 
    urlConnection.setDoOutput(true);     
    urlConnection.connect();     
    File SDCardRoot = Environment.getExternalStorageDirectory().getAbsoluteFile(); 
    String filename="downloadedFile.png"; 
    Log.i("Local filename:",""+filename); 
    File file = new File(SDCardRoot,filename); 
    if(file.createNewFile()) 
    { 
    file.createNewFile(); 
    }     
    FileOutputStream fileOutput = new FileOutputStream(file); 
    InputStream inputStream = urlConnection.getInputStream(); 
    int totalSize = urlConnection.getContentLength(); 
    int downloadedSize = 0; 
    byte[] buffer = new byte[1024]; 
    int bufferLength = 0; 
    while ((bufferLength = inputStream.read(buffer)) > 0) 
    {     
    fileOutput.write(buffer, 0, bufferLength);     
    downloadedSize += bufferLength;     
    Log.i("Progress:","downloadedSize:"+downloadedSize+"totalSize:"+ totalSize) ; 
    }    
    fileOutput.close(); 
    if(downloadedSize==totalSize) filepath=file.getPath();  
} 
catch (MalformedURLException e) 
{ 
    e.printStackTrace(); 
} 
catch (IOException e) 
{ 
    filepath=null; 
    e.printStackTrace(); 
} 
Log.i("filepath:"," "+filepath) ; 
return filepath; 

使用此代碼來設置SD卡的圖像以imageview的BG

File f = new File("/mnt/sdcard/photo.jpg"); 
ImageView imgView = (ImageView)findViewById(R.id.imageView); 
Bitmap bmp = BitmapFactory.decodeFile(f.getAbsolutePath()); 
imgView.setImageBitmap(bmp); 

別人使用這個

File file = .... 
Uri uri = Uri.fromFile(file); 
imgView.setImageURI(uri); 

您可以直接在網頁上顯示圖像而無需下載。請檢查下面的功能。它會將來自網絡的圖像顯示在圖像視圖中。

public static Drawable LoadImageFromWebOperations(String url) { 
    try { 
     InputStream is = (InputStream) new URL(url).getContent(); 
     Drawable d = Drawable.createFromStream(is, "src name"); 
     return d; 
    } catch (Exception e) { 
     return null; 
    } 
} 

然後在活動使用代碼設置圖像的ImageView。

+0

我的要求是保存它,因爲我的列表中的一些其他圖像是從內存中顯示的,並且在網絡和內存之間交換會使我的列表無效。 – Skynet

+0

使用上面的代碼將圖像保存到SD卡從web url.in上面的代碼中,您將圖像保存爲原始byte.so,您的圖像質量將不會降低。 – Ruban

+0

我在上面,謝謝隊友:) – Skynet

1

問題是您正在處理有損格式(JPG)並且正在重新壓縮圖像。即使質量在100,你仍然會受到損失 - 你只能得到最少的金額。

而不是解壓縮到Bitmap,然後在將其寫入文件時重新壓縮,您希望將原始字節直接下載到文件。

... 
InputStream is = connection.getInputStream(); 
OutputStream os = new FileOutputStream(data1); 

byte[] b = new byte[2048]; 
int length; 

while ((length = is.read(b)) != -1) { 
    os.write(b, 0, length); 
} 

is.close(); 
os.close(); 
...