Android Bitmap FileNotFoundException ENOENT仅在文件存在时调用方法

时间:2014-06-25 12:16:24

标签: android bitmap filenotfoundexception

我要从我的DCIM文件夹加载.png文件,但它会抛出Execption

06-25 14:03:09.350: E/BitmapFactory(12552): Unable to decode stream: java.io.FileNotFoundException: /android.graphics.Bitmap@421569f8: open failed: ENOENT (No such file or directory)

我在Manifest中添加了这些权限:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

我将此方法称为加载文件:

public static Bitmap getPreview(File parentDir){
    Bitmap toReturn=null;
    File file = new File(parentDir,"preview.png");

    if (file.exists()) {
        file.getParentFile().mkdirs();
        toReturn=BitmapFactory.decodeFile(file.getAbsolutePath());

    }
    return toReturn;

}

文件确实存在,当我显示完整路径时

06-25 14:03:09.100: I/(12552): /storage/emulated/0/DCIM/TextureVisualiser/SetA/preview.png

我知道我可以从这条路径加载Textfiles:

06-25 14:03:09.110: I/(12552): /storage/emulated/0/DCIM/TextureVisualiser/SetA/info.txt

对于txt文件,我得到了方法:

public static String[] readTitleAndSubtitle(File parentDir){
    String[] toReturn = new String[2];
    File file = new File(parentDir,"info.txt");
    Log.i("",""+file);
    try {
        BufferedReader br = new BufferedReader(new FileReader(file));
        toReturn[0]=br.readLine();
        toReturn[1]=br.readLine();
        br.close();
    } catch (Exception e) {
        // TODO: handle exception
    }

    return toReturn;
}

基本上我在文件夹中有一个.txt文件和一个.png文件,只能加载txt文件。 png文件抛出异常。

编辑: 我试图将它移动到另一个lokation但仍然无法读取.png文件,但我可以读取.txt文件。

提前,

Lightbringa

2 个答案:

答案 0 :(得分:0)

找到解决方案。

我将这些东西用于ListView,我试图通过

抓取想要的View
    View v = dataAdapter.getView(0,null,null);
    ImageView iv =(ImageView) v.findViewById(R.id.Image);
    iv.setImageBitmap(DataHelper.getPreview(allDirects.get(0)));

但它不能像这样工作:D

所以我建立了自己的BaseAdapter。对于那些想知道我是怎么做的人来说,这里是完整的班级:

package de.bachelorarbeit.main;

import java.io.File;
import java.util.List;
import de.bachelorarbeit.R;
import de.bachelorarbeit.helper.DataHelper;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CustomList extends BaseAdapter {
    private final Activity context;
    private final List<File> allDirects;

    public CustomList(Activity context,List<File> allDirects) {
        this.context = context;
        this.allDirects=allDirects;
}

@Override
public View getView(int position, View view, ViewGroup parent) {
    LayoutInflater inflater = context.getLayoutInflater();
    View rowView = inflater.inflate(R.layout.item, null, true);
    TextView txtTitle = (TextView) rowView.findViewById(R.id.Title);
    TextView subTitle = (TextView) rowView.findViewById(R.id.SubTitle);
    ImageView imageView = (ImageView) rowView.findViewById(R.id.Image);


    String[] both = DataHelper.readTitleAndSubtitle(allDirects.get(position));      
    txtTitle.setText(both[0]);
    subTitle.setText(both[1]);
    imageView.setImageBitmap(DataHelper.getPreview(allDirects.get(position)));
    return rowView;
}

@Override
public int getCount() {
    return allDirects.size();
}

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

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

感谢我得到的每一个帮助, Lightbringa

答案 1 :(得分:-1)

更改为:

 if (!file.exists()) {
        file.getParentFile().mkdirs();

    }


toReturn=BitmapFactory.decodeFile(file.getAbsolutePath());