图片下载code适用于所有的图像格式,问题PNG格式渲染 [英] Image download code works for all image format, issues with PNG format rendering

查看:130
本文介绍了图片下载code适用于所有的图像格式,问题PNG格式渲染的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在用的是code以下,从服务器下载并显示图像我的ImageView

I am using the code below to download and show image from server to my 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();
          }
     }
}

这code工作文件图像的所有格式,但是当涉及到​​PNG它不会让图像下载后,并显示在ImageView的透明。

This code works file for all format of images but when it comes to PNG it wont let the image transparent after download and displaying on ImageView.

你知道吗?

推荐答案

我不知道这将是对你还是没有办法了。

I dont know it will be a solution for you or not

但是你可以使用绘制对象而不是位图

But you can use a Drawable instead of Bitmap

下面是code

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 正确

这篇关于图片下载code适用于所有的图像格式,问题PNG格式渲染的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆