找不到文件异常

时间:2013-12-06 03:15:35

标签: android filenotfoundexception fileinputstream fileoutputstream

我有一个复选框,选中此复选框可以通过执行以下操作将创建的位图保存到内部存储:

    public void SaveImage(Bitmap default_b) {

    String root = Environment.getExternalStorageDirectory().toString();
    File myDir = new File(root + "/saved_images");
    myDir.mkdirs();
    Random generator = new Random();
    int n = 100000;
    n = generator.nextInt(n);
    String fname = "Image-" + n +".png";
    File file = new File (myDir, fname);
    Log.i("AppInfoAdapter", "" + file);
    if (file.exists()) file.delete();
    try {
        // File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath()
            //  + "/" + fname + ".png");
        FileOutputStream out = mContext.getApplicationContext().openFileOutput("file", Context.MODE_WORLD_WRITEABLE);
        // FileOutputStream out = new FileOutputStream(file);
        default_b.compress(Bitmap.CompressFormat.PNG, 90, out);
        out.flush();
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

然后我尝试通过这样做从文件中获取位图:

    @Override
public View getView(int position, View convertView, ViewGroup parent) {
    // Try to reuse the views
    ImageView view = (ImageView) convertView;
    @SuppressWarnings("unused")
    boolean checked = (mCheckBox==null)?false:(((CheckBox)  mCheckBox).isChecked());
    // if convert view is null then create a new instance else reuse it
    if (view == null) {
        view = new ImageView(Context);
        Log.d("GridViewAdapter", "new imageView added");
    }
    if(checked = true){
    try {
        Bitmap bitmapA = null;
        FileInputStream in = Context.openFileInput("file");
        bitmapA = BitmapFactory.decodeStream(in);
        in.close();
        /*BufferedInputStream buf = new BufferedInputStream(in);
        byte[] bitMapA = new byte[buf.available()];
        buf.read(bitMapA);
        Bitmap bM = BitmapFactory.decodeByteArray(bitMapA, 0, bitMapA.length);
        */view.setImageBitmap(bitmapA);
        if (in != null) {
            in.close();
        }
        /*if (buf != null) {
            buf.close();
        }*/
    } catch (Exception e) {
        e.printStackTrace();
    }}
    else {
        Toast.makeText(Context.getApplicationContext(), "no icons found",
                   Toast.LENGTH_LONG).show();
    }
    view.setImageResource(drawables.get(position));
    view.setScaleType(ImageView.ScaleType.CENTER_CROP);
    view.setLayoutParams(new android.widget.GridView.LayoutParams(70, 70));
    view.setTag(String.valueOf(position));
    return view;
}

但是我在LogCat中得到了这个:

12-05 20:07:01.227: W/System.err(14277): java.io.FileNotFoundException: /data/data/com.example.awesomefilebuilderwidget/files/file (No such file or directory)
12-05 20:07:01.247: W/System.err(14277):    at org.apache.harmony.luni.platform.OSFileSystem.open(Native Method)
12-05 20:07:01.247: W/System.err(14277):    at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:232)
12-05 20:07:01.247: W/System.err(14277):    at java.io.FileInputStream.<init>(FileInputStream.java:80)
12-05 20:07:01.247: W/System.err(14277):    at android.app.ContextImpl.openFileInput(ContextImpl.java:463)
12-05 20:07:01.257: W/System.err(14277):    at android.content.ContextWrapper.openFileInput(ContextWrapper.java:152)
12-05 20:07:01.257: W/System.err(14277):    at com.example.awesomefilebuilderwidget.GridViewAdapter.getView(GridViewAdapter.java:82)
12-05 20:07:01.257: W/System.err(14277):    at android.widget.AbsListView.obtainView(AbsListView.java:1449)
12-05 20:07:01.257: W/System.err(14277):    at android.widget.GridView.onMeasure(GridView.java:935)
12-05 20:07:01.257: W/System.err(14277):    at android.view.View.measure(View.java:8541)
12-05 20:07:01.267: W/System.err(14277):    at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:581)
12-05 20:07:01.267: W/System.err(14277):    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:365)
12-05 20:07:01.267: W/System.err(14277):    at android.view.View.measure(View.java:8541)
12-05 20:07:01.267: W/System.err(14277):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3257)
12-05 20:07:01.267: W/System.err(14277):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
12-05 20:07:01.267: W/System.err(14277):    at android.view.View.measure(View.java:8541)
12-05 20:07:01.277: W/System.err(14277):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:531)
12-05 20:07:01.277: W/System.err(14277):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
12-05 20:07:01.277: W/System.err(14277):    at android.view.View.measure(View.java:8541)
12-05 20:07:01.277: W/System.err(14277):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3257)
12-05 20:07:01.277: W/System.err(14277):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
12-05 20:07:01.277: W/System.err(14277):    at android.view.View.measure(View.java:8541)
12-05 20:07:01.277: W/System.err(14277):    at android.view.ViewRoot.performTraversals(ViewRoot.java:903)
12-05 20:07:01.277: W/System.err(14277):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1961)
12-05 20:07:01.297: W/System.err(14277):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-05 20:07:01.297: W/System.err(14277):    at android.os.Looper.loop(Looper.java:150)
12-05 20:07:01.297: W/System.err(14277):    at android.app.ActivityThread.main(ActivityThread.java:4333)
12-05 20:07:01.297: W/System.err(14277):    at java.lang.reflect.Method.invokeNative(Native Method)
12-05 20:07:01.297: W/System.err(14277):    at java.lang.reflect.Method.invoke(Method.java:507)
12-05 20:07:01.307: W/System.err(14277):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-05 20:07:01.307: W/System.err(14277):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-05 20:07:01.307: W/System.err(14277):    at dalvik.system.NativeStart.main(Native Method)

但更具体地说,我注意到这两行:

12-05 20:07:01.227: W/System.err(14277): java.io.FileNotFoundException: /data/data/com.example.awesomefilebuilderwidget/files/file (No such file or directory)
12-05 20:07:01.257: W/System.err(14277):    at com.example.awesomefilebuilderwidget.GridViewAdapter.getView(GridViewAdapter.java:82)

这是第82行:

FileInputStream in = Context.openFileInput("file");

显然在某处,该目录尚未创建。

为什么会这样?

请注意我做出的许可是允许的:

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

2 个答案:

答案 0 :(得分:1)

此:

String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/saved_images");
...

将文件保存到SD卡,同时:

FileInputStream in = Context.openFileInput("file");

在您应用的私人存储空间中打开名为“file”的文件。

您需要将您创建的文件的实际路径传递给openFileInput()

答案 1 :(得分:0)

如果您正在使用位图。试试这个

- 写入文件 -

ByteArrayOutputStream bytes = new ByteArrayOutputStream();
screen.compress(Bitmap.CompressFormat.JPEG, 15, bytes);    // screen is a bitmap , use 100 for good quality
File f = new File(Environment.getExternalStorageDirectory()+ File.separator + "test.jpg");
            f.createNewFile();
//write the bytes in file
    FileOutputStream fo = new FileOutputStream(f);
    fo.write(bytes.toByteArray());
// remember close de FileOutput

    fo.close();