L18132033730
L18132033730
2017-05-11 11:52

Android 我在网络上加载一图片到fragment上显示不出来代码如下 哪里出问题了

  • fragment
  • 图片
  • android

package com.example.li1.test;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;

import android.os.Bundle;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ListView;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.FindCallback;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class WeixinFragment extends Fragment {

ListView listView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.tab01, null);

    listView= (ListView) view.findViewById(R.id.lv_show_img);
    MyAdapter adapter = new MyAdapter(getActivity());
    listView.setAdapter(adapter);
    Log.d("logd", "--UI--");
    return view;
}

class MyAdapter extends BaseAdapter {

    /**
     * 网上的图片url
     */
    String[] picUrls = new String[]

            {"http://ac-morQn5xP.clouddn.com/346fafc9fcf778dee689.png"};
    /**
     * 名称对应url上的图片名称
     */
    String[] picNames = new String[] {
            "cat.png",
    };
    String path = Environment.getExternalStorageDirectory()
            + "/listviewImg/";// 文件目录

    LayoutInflater layoutInflater;
    Context context;
    File fileDir;

    public MyAdapter(Context context) {
        this.context = context;
        layoutInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);


        /**
         * 文件目录如果不存在,则创建
         */
        fileDir = new File(path);
        if (!fileDir.exists()) {
            fileDir.mkdirs();
        }

    }



    @Override
    public int getCount() {
        return picUrls.length;
    }

    @Override
    public Object getItem(int position) {
        return picUrls[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View view = layoutInflater.inflate(R.layout.listview_img, null);

        ImageView imageView = (ImageView) view.findViewById(R.id.img);

        /**
         * 创建图片文件
         */
        File file = new File(fileDir, picNames[position]);
        if (!file.exists()) {// 如果本地图片不存在则从网上下载
            downloadPic(picNames[position], picUrls[position]);
        } else {// 图片存在则填充到listview上
            Bitmap bitmap = BitmapFactory
                    .decodeFile(file.getAbsolutePath());
            imageView.setImageBitmap(bitmap);
        }
        return view;
    }

    /**
     * 使用子线程下载图片
     *
     * @param name
     * @param
     */
    private void downloadPic(final String name, final String picurl) {

        new Thread(new Runnable() {

            @Override
            public void run() {

                FileOutputStream fos = null;
                InputStream in = null;

                // 创建文件
                File file = new File(fileDir, name);

                try {

                    fos = new FileOutputStream(file);

                    URL url = new URL(picurl);

                    in = url.openStream();

                    int len = -1;
                    byte[] b = new byte[1024];
                    while ((len = in.read(b)) != -1) {
                        fos.write(b, 0, len);
                    }

                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        if (fos != null) {
                            fos.close();
                        }
                        if (in != null) {
                            in.close();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();

    }

}

}
这是Fragment下显示不出来的情况
图片说明

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

5条回答

为你推荐