2014-01-16 37 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(); 
...