簡體   English   中英

從Facebook提取圖像並將其寫入SD時,圖像質量非常低。

[英]Image quality very low when images fetched from Facebook and written to 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);

即使使用選項后,畫質仍然很差。 有什么可以改善的呢?

要將圖片從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;

使用此代碼將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);

否則用這個

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。

問題是您正在處理有損格式(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();
...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM