【问题标题】:I am not able to display image from my local storage in android我无法在 android 中显示本地存储中的图像
【发布时间】:2016-07-19 11:44:51
【问题描述】:

我的本​​地存储中的文件夹中有 20k 个图像文件,我需要通过本地存储中的文件名显示图像。怎么做?我在这里附上了我的代码:

 if (Environment.getExternalStorageState()
            .equals(Environment.DIRECTORY_PICTURES)) {
        String filePath = File.separator + "sdcard" + File.separator + "Android" + File.separator + "obb" + File.separator + "convertMp3ToDb" + File.separator + "ldoce6pics" + File.separator + fileName;
        try {
            FileInputStream fis = new FileInputStream(filePath);
            String entry = null;
            while ((entry = fis.toString()) != null) {
                if (!entry.toString().isEmpty()) {
                    File Mytemp = File.createTempFile("TCL", "jpg", getContext().getCacheDir());
                    Mytemp.deleteOnExit();
                    FileOutputStream fos = new FileOutputStream(Mytemp);
                    for (int c = fis.read(); c != -1; c = fis.read()) {
                        try {
                            fos.write(c);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    fos.close();
                    FileInputStream MyFile = new FileInputStream(Mytemp);
                    final Bitmap bit = BitmapFactory.decodeStream(MyFile);
                    imageView.setImageBitmap(bit);

                }
            }
            fis.close();

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

        }
    }

【问题讨论】:

  • 显示你的 logcat 日志
  • 尝试 1. 使用 Glide 之类的图像库(还有其他的我更喜欢 glide) - 将文件路径从字符串转换为 uri 2. 您也可以使用加载器进行相同的设置位图
  • 做一些日志并发布
  • 没有 logcat,错误但他们没有执行任何操作,图像没有显示
  • 您正在使用 try catch。删除它并再次检查日志。该块中可能发生错误。

标签: android image android-studio local-storage fileinputstream


【解决方案1】:
 if (Environment.getExternalStorageState()
            .equals(Environment.DIRECTORY_PICTURES)) {
        String filePath = File.separator + "sdcard" + File.separator + "Android" + File.separator + "obb" + File.separator + "convertMp3ToDb" + File.separator + "ldoce6pics" + File.separator + fileName;
        File f = new File(filePath);
         if(f.exits()){

            Bitmap bm = BitmapFactory.decodeFile(f.getAbsolutePath());
            imageView.setImageBitmap(bm);

         }else{
             // invalid path
           }
     }
  }

更新部分:

if (Environment.getExternalStorageState()
                .equals(Environment.DIRECTORY_PICTURES)) {
  // this never be true see below image 
 }

你必须这样做

 File file  = new File(Environment.getExternalStorageState()+"/Android/obb/convertMp3ToDb/ldoce6pics/"+fileName);

        if (file.exists()) {
            // your condition should ne likr this.
        }

【讨论】:

  • if (Environment.getExternalStorageState() .equals(Environment.DIRECTORY_PICTURES)) 这个条件是假的,我知道那会是什么问题吗
  • 在我看来你做错了吗? if (Environment.getExternalStorageState() .equals(Environment.DIRECTORY_PICTURES) 永远不会为真。
  • 那么如何访问内部存储镜像
  • @MahalakshmiSaravanan 在文件探索中打开您的文件并转到其属性,然后将其写入您在手机中看到的路径。
猜你喜欢
  • 2017-01-12
  • 2020-12-29
  • 2021-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-05
  • 2017-05-11
  • 1970-01-01
相关资源
最近更新 更多