为了账号安全,请及时绑定邮箱和手机立即绑定

老师您imageview那个例子中HttoThread.java写的是不是有些小问题呀,我运行后是空白

老师您imageview那个例子中HttoThread.java写的是不是有些小问题呀,我运行后是空白,后来是这么解决的
53行改为String fileName = String.valueOf(System.currentTimeMillis())+".jpg";//您没有指定后缀
59行加上if (!downloadFile.exists()) downloadFile.createNewFile();//您没有新创建文件,不能写入数据
为什么您的代码没有这两行可以运行。。。。卡我三天才解决,希望知道自己是不是哪儿漏写了。。。

正在回答

3 回答

第一就是不写createNewFile方法也是会创建文件的。
然后第二那个没有后缀名也是一样可以打开。因为他是转化为BitMap这种编码格式来显示图片。

亲测。贴出代码吧。估计是其他的有问题。然后误打误撞。

0 回复 有任何疑惑可以回复我~
#1

decho 提问者

http://paste.ubuntu.com/22539919/ 这是源码。。。
2016-08-07 回复 有任何疑惑可以回复我~
#2

iMcLaren 回复 decho 提问者

我试了你的代码,把你说的这两个条件去掉正常运行。你再去试试?
2016-08-07 回复 有任何疑惑可以回复我~
#3

iMcLaren 回复 decho 提问者

楼下贴出了你的代码了
2016-08-07 回复 有任何疑惑可以回复我~
#4

decho 提问者

非常感谢!
2016-08-07 回复 有任何疑惑可以回复我~
查看1条回复

没什么问题QAQ。。。。。可能是当时网不好的缘故吧,不加也没关系的。。。。谢谢

iMcLaren


0 回复 有任何疑惑可以回复我~
#1

iMcLaren

嗯嗯。其实你可以自己搭个tomcat来测试嘛,这样排bug也更容易点
2016-08-07 回复 有任何疑惑可以回复我~
package com.bmiehis.httpurlconnection;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.os.Environment;import android.os.Handler;import android.util.Log;import android.widget.ImageView;import org.json.JSONObject;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.URL;/** * Created by dell on 2016/8/2. */public class HttpImageThread extends Thread {
    private String url;
    private Handler handler;
    private ImageView imageView;

    public HttpImageThread(String url, ImageView imageView, Handler handler) {
        this.url = url;
        this.handler = handler;
        this.imageView = imageView;
    }

    @Override
    public void run() {
        try {

            URL httpUrl = new URL(url);
            InputStream in;
            /*            if(url.contains("https://"))            {                HttpsURLConnection conn= (HttpsURLConnection) httpUrl.openConnection();                conn.setReadTimeout(5000);                conn.setRequestMethod("GET");                conn.setDoInput(true);                in=conn.getInputStream();            }            else            */
            {
                HttpURLConnection conn = (HttpURLConnection) httpUrl.openConnection();
                conn.setReadTimeout(5000);
                conn.setRequestMethod("GET");
                conn.setDoInput(true);
                in = conn.getInputStream();
            }


            FileOutputStream out = null;
            String fileName = String.valueOf(System.currentTimeMillis())+".jpg";
            File downloadFile = null;
            if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                File parent = Environment.getExternalStorageDirectory();
                downloadFile = new File(parent, fileName);

                if (!downloadFile.exists()) downloadFile.createNewFile();


                out = new FileOutputStream(downloadFile);

                Log.d(HttpImageThread.class.getSimpleName(), "++++++++++++++++getExternalStorageState_success!");

            }
            byte[] b = new byte[2 * 1024];
            int len;
            if (out != null) {
                while ((len = in.read(b)) != -1) {
                    out.write(b, 0, len);
                }
            }
            final Bitmap bitmap = BitmapFactory.decodeFile(downloadFile.getAbsolutePath());

            Log.d(HttpImageThread.class.getSimpleName(), "++++++++++++++++++++++++downloadFile_success");

            handler.post(new Runnable() {
                @Override
                public void run() {
                    imageView.setImageBitmap(bitmap);
                    Log.d(HttpImageThread.class.getSimpleName(), "++++++++++++++++++++++++setImageBitmap_success");
                }
            });


        } catch (IOException e) {
            e.printStackTrace();
        }
    }}


0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
Android中的Http通信
  • 参与学习       64653    人
  • 解答问题       347    个

了解Android-http网络编程应用,常见忘了请求相关应用

进入课程

老师您imageview那个例子中HttoThread.java写的是不是有些小问题呀,我运行后是空白

我要回答 关注问题
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号