简体   繁体   English

从Facebook提取图像并将其写入SD时,图像质量非常低。

[英]Image quality very low when images fetched from Facebook and written to SD.

I am fetching images from Facebook and writing them to SD card, but the image quality is very low. 我正在从Facebook获取图像并将其写入SD卡,但是图像质量很低。 Following is my code to fetch and write: 以下是我要提取和编写的代码:

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();
        }

The following code I use to display the same image: 我用来显示相同​​图像的以下代码:

                    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);

The quality is still low even after using Options. 即使使用选项后,画质仍然很差。 What can be done to improve this? 有什么可以改善的呢?

to Save image from URL onto SD card use this code 要将图片从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;

use this code to set sdcard image as your imageview bg 使用此代码将sdcard图像设置为您的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);

else use this 否则用这个

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

You can directly show image from web without downloading it. 您可以直接从网上显示图片,而无需下载。 Please check the below function . 请检查以下功能。 It will show the images from the web into your image view. 它将把来自网络的图像显示到您的图像视图中。

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;
    }
}

then set image to imageview using code in your activity. 然后使用您活动中的代码将图片设置为imageview。

The issue is that you're dealing with a lossy format (JPG) and are re-compressing the image. 问题是您正在处理有损格式(JPG),并且正在重新压缩图像。 Even with quality at 100 you still get loss - you just get the least amount. 即使质量为100您仍然会蒙受损失-您获得的损失最少。

Rather than decompressing to a Bitmap then re-compressing when you write it to the file, you want to download the raw bytes directly to a file. 您希望将原始字节直接下载到文件中,而不是解压缩为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();
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM