【问题标题】:How to display image from storage to cardview?如何将图像从存储显示到cardview?
【发布时间】:2018-04-14 13:53:53
【问题描述】:

我曾尝试将图像显示到卡片视图,但它不起作用并且不显示错误。请任何人帮助我。我是新的安卓。我从下载的文件夹中检查了文件是否存在:/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

【问题讨论】:

  • 贴出图片文件保存的完整代码
  • 调试和检查你的获取文件,Glide代码没有问题
  • @Munir 我更新了下载文件的代码。
  • @rajahsekar 我检查了文件夹中的文件。下载成功。
  • 您是否在 logcat 中收到任何错误消息

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


【解决方案1】:

我确信您使用的 Glide 代码可以正常工作,我在很多项目中都使用过它。您的图像文件可能有问题。交叉检查所有点SD卡权限,文件路径使用是否正确

如果不成功尝试将文件转换为 Uri,它可能会有所帮助

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

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

【讨论】:

  • 感谢您的帮助,我之前尝试过此代码,但仍然没有任何改变。 :(
猜你喜欢
  • 2019-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-10
  • 2021-02-02
  • 2019-12-24
  • 2015-02-28
  • 1970-01-01
相关资源
最近更新 更多