使用fileinput / output流时找不到文件异常

时间:2013-12-05 23:53:49

标签: android

打开我的应用时,我在LogCat中收到此警告:

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

这是因为它在我想要它之前运行我对fileinputstream的编码。

为了给出一些背景知识,我有一个复选框,在选中时创建一个位图,然后通过这样做将该位图保存到内部存储器: (这是我保存的地方)

addCheckbox
            .setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    if (addCheckbox.isChecked()) {
                        System.out.println("Checked");
                        PackageManager pm = mContext.getPackageManager();
                        Drawable icon = null;
                        try {
                            icon = pm
                            .getApplicationIcon(entry.activityInfo.packageName);
                        } catch (NameNotFoundException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        Drawable default_icon = pm.getDefaultActivityIcon();
                        if (icon instanceof BitmapDrawable
                                && default_icon instanceof BitmapDrawable) {
                            BitmapDrawable icon_bd = (BitmapDrawable) icon;
                            Bitmap icon_b = icon_bd.getBitmap();
                            BitmapDrawable default_bd = (BitmapDrawable) pm
                                    .getDefaultActivityIcon();
                            Bitmap default_b = default_bd.getBitmap();
                            if (icon_b == default_b) {
                                // It's the default icon
                                scaleDownBitmap(default_b, 100, v.getContext());
                                Log.d("AppInfoAdapter", "Scale Bitmap Chosen");

                                SaveImage(default_b);
                                Log.d("AppInfoAdapter", "Scaled BM saved to External Storage");

                                Intent intent = new Intent(v.getContext(), GridViewAdapter.class);
                                // intent.hasExtra("bitmapA");
                                v.getContext().startActivity(intent);
                                Log.d("AppInfoAdapter", "Intent started to send Bitmap");

                            }
                        }
                    } else {
                        System.out.println("Un-Checked");
                    }

                }
            });

saveImage类:

    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("bitmapA", 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;
    // 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");
    }
    try {
        Bitmap bitmapA = null;
        FileInputStream in = Context.openFileInput("bitmapA");
        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();
    }
    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;
}

由于所有这一切,当我选中复选框时,它不会创建位图,然后将其保存到内存然后从内存中获取并将其设置为imageView。我所有的复选框都打印出“已检查”(就像它应该的那样)

为什么会这样?

请注意,这两个类都扩展了基本适配器

1 个答案:

答案 0 :(得分:0)

您是否确定您的应用对磁盘具有写入权限?