android - 图片下载代码适用于所有图片格式,PNG 格式渲染问题

标签 android imageview imagedownload

我正在使用下面的代码从服务器下载图像并将其显示到我的 ImageView

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

public class HTTPTest extends Activity {


     ImageView imView;
     String imageUrl="http://11.0.6.23/";
     Random r= new Random();
    /** Called when the activity is first created. */ 
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);

        Button bt3= (Button)findViewById(R.id.get_imagebt);
        bt3.setOnClickListener(getImgListener);
        imView = (ImageView)findViewById(R.id.imview);
    }    

    View.OnClickListener getImgListener = new View.OnClickListener()
    {

          @Override
          public void onClick(View view) {
               // TODO Auto-generated method stub

               //i tried to randomize the file download, in my server i put 4 files with name like
                        //png0.png, png1.png, png2.png so different file is downloaded in button press
               int i =r.nextInt(4);
               downloadFile(imageUrl+"png"+i+".png");
               Log.i("im url",imageUrl+"png"+i+".png");
          }

    };


    Bitmap bmImg;
    void downloadFile(String fileUrl){
          URL myFileUrl =null;          
          try {
               myFileUrl= new URL(fileUrl);
          } catch (MalformedURLException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
          }
          try {
               HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection();
               conn.setDoInput(true);
               conn.connect();
               int length = conn.getContentLength();
               InputStream is = conn.getInputStream();

               bmImg = BitmapFactory.decodeStream(is);
               imView.setImageBitmap(bmImg);
          } catch (IOException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
          }
     }
}

这段代码适用于所有格式的图像文件,但是当涉及到 PNG 时,它不会让图像在下载并显示在 ImageView 上后透明。

有什么想法吗?

最佳答案

我不知道它是否适合你

但是你可以使用 Drawable 而不是 Bitmap

这是代码

void downloadFile(String fileUrl) {
try{
      InputStream is = (InputStream) new URL(fileUrl).getContent();
      Drawable d = Drawable.createFromStream(is, "src name");
      imgView.setImageDrawable(d);            
        } catch (IOException e) {
            e.printStackTrace();                
        }

}

这将正确显示 png

关于android - 图片下载代码适用于所有图片格式,PNG 格式渲染问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5052437/

相关文章:

android - 加载图像并单击上一个按钮不会在 android 中加载图像

javascript - 按 Node 下载图像(请求)

linux - 使用wget下载图像并以指定文件名保存

android - fragment 没有膨胀

java - 中心 ImageView

android - 如何使 ImageButton 仅显示图像?

android - Volley : Pause/Resume Download

android - 约束布局 : Realign baseline constraint in case if dependent view visibility was set to GONE

android - 在 appComponent dagger 2 中动态添加测试模块?

java - 一个特定的 ImageView 导致严重滞后