我无法在android中的本地存储中显示图像

时间:2016-07-19 11:44:51

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

我的本​​地存储空间中的文件夹中有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();

        }
    }

1 个答案:

答案 0 :(得分:3)

 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 
 }

enter image description here

你必须这样做

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

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