如何将图像从存储显示到cardview?

时间:2017-11-02 05:59:34

标签: android android-studio android-recyclerview android-cardview

我试图将图像显示到卡片视图但它不起作用并且不显示错误。有人帮我请。我是新的机器人。我检查文件存在于下载文件夹:/ storage / emulated / 0 / hismart / hinhmon

public class Album {
private String name;
private String gia;
private String thumbnail;
private String url;

public Album() {
}

public Album(String name, String gias, String thumbnail, String url) {
    this.name = name;
    this.gia = gias;
    this.thumbnail = thumbnail;
    this.url = url;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getGia() {
    return gia;
}

public void setGia(String gia) {
    this.gia = gia;
}

public String getThumbnail() {
    return thumbnail;
}

public void setThumbnail(String thumbnail) {
    this.thumbnail = thumbnail;
}


public String getUrl() {
    return url;
}

public void setUrl(String url) {
    this.url = url;
}

}

创建数据并将数据添加到ArrayList:

List<String> ArrTenmon = new ArrayList<String>();
List<String> ArrGia = new ArrayList<String>();
List<String> ArrImgLocal = new ArrayList<String>();
List<String> ArrImgUrl = new ArrayList<String>();

并添加

    }  Cursor c = db.getdata("select * from tbl_mon_app");
    int count = c.getCount();

    for (int i = 0; i < count; i++) {

        new DownloadFile().execute(ArrImgUrl.get(i));
        if (!fileloc.exists()) {
            fileloc.mkdirs();
        }

        Album a = new Album(ArrTenmon.get(i), ArrGia.get(i), ArrImgLocal.get(i), ArrImgUrl.get(i));
        albumList.add(a);
        adapter.notifyDataSetChanged();
    }

并在适配器中:

String folder_main = "hismart/hinhmon";
File fileloc = new File(Environment.getExternalStorageDirectory(), folder_main);

public class MyViewHolder extends RecyclerView.ViewHolder {
    public TextView title, count;
    public ImageView thumbnail, overflow;


    public MyViewHolder(View view) {
        super(view);
        title = (TextView) view.findViewById(R.id.title);
        count = (TextView) view.findViewById(R.id.count);
        thumbnail = (ImageView) view.findViewById(R.id.thumbnail);
        overflow = (ImageView) view.findViewById(R.id.overflow);

    }
}


public AlbumsAdapter(Context mContext, List<Album> albumList) {
    this.mContext = mContext;
    this.albumList = albumList;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.album_card, parent, false);

    return new MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
    Album album = albumList.get(position);
    holder.title.setText(album.getName());
    holder.count.setText(album.getGia() + " vnđ");

  File imgFile = new File(fileloc+"/"+ "album" + position);

  Glide.with(mContext).load(imgFile).into(holder.thumbnail);

}

 @Override
public int getItemCount() {
    return albumList.size();
}

以下问题无法显示图像,图像名称为:album1,alubm2 ......

File imgFile = new File(fileloc+"/"+ "album" + position);

Glide.with(mContext).load(imgFile).into(holder.thumbnail);

下载文件:

 class DownloadFile extends AsyncTask<String, Integer, String> {
    ProgressDialog mProgressDialog = new ProgressDialog(BookActivity.this);// Change Mainactivity.this with your activity name.
    String strFolderName;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

    }

    @Override
    protected String doInBackground(String... aurl) {
        int count;
        String targetFileName = null;
        try {
            URL url = new URL((String) aurl[0]);
            URLConnection conexion = url.openConnection();
            conexion.connect();
            targetFileName = aurl[0].substring(aurl[0].lastIndexOf("/") + 1);
            int lenghtOfFile = conexion.getContentLength();
            String PATH = Environment.getExternalStorageDirectory() + "/hismart/hinhmon/";
            File folder = new File(PATH);
            if (!folder.exists()) {
                folder.mkdir();//If there is no folder it will be created.
            }
            InputStream input = new BufferedInputStream(url.openStream());
            OutputStream output = new FileOutputStream(PATH + targetFileName);
            byte data[] = new byte[1024];
            long total = 0;
            while ((count = input.read(data)) != -1) {
                total += count;
                publishProgress((int) (total * 100 / lenghtOfFile));
                output.write(data, 0, count);
            }
            output.flush();
            output.close();
            input.close();
        } catch (Exception e) {
        }
        return targetFileName;
    }

    protected void onProgressUpdate(Integer... progress) {
        mProgressDialog.setProgress(progress[0]);
        if (mProgressDialog.getProgress() == mProgressDialog.getMax()) {
            mProgressDialog.dismiss();

        }
    }

    protected void onPostExecute(String result) {



    }
}

调用asyntask下载文件

 Cursor c = db.getdata("select * from tbl_mon_app");
    int count = c.getCount();

    for (int i = 0; i < count; i++) {

        new DownloadFile().execute(ArrImgLocal.get(i));
        if (!fileloc.exists()) {
            fileloc.mkdirs();
        }

    }

Logcat:不显示任何错误

结果如下:image after run

1 个答案:

答案 0 :(得分:0)

我确定您使用的Glide代码可以使用,我已经使用了很多项目。您的图像文件可能存在问题。交叉检查所有点Sd卡权限,文件路径使用是否正确

如果没有工作尝试将文件转换为Uri,它可能有帮助

    File imgFile = new File(fileloc+"/"+ "album" + position);
    Uri imageUri = Uri.fromFile(file);

    Glide.with(this).load(imageUri).into(imgView);