0

I have a checkbox, that when checked saves a created bitmap to internal storage by doing this:

    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();
    }
}

I then am trying to get the bitmap from the file by doing this:

    @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;
}

But I get this in the 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)

But more specifically, I notice these two lines:

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)

Here is line 82:

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

So obviously somewhere, that directory is not being created.

Why is this?

PLEASE NOTE THAT I DO INDEED HAVE PERMISSIONS:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
user2909006
  • 253
  • 6
  • 22
  • Have checked if the file is being created somewhere else? use adb shell to check your device/emulator directories – insomniac Dec 06 '13 at 04:05

2 Answers2

1

This:

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

saves your file to the SD Card, while this:

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

opens a file named "file" in your app's private storage.

You need to pass the actual path of the file you created to openFileInput()

323go
  • 14,143
  • 6
  • 33
  • 41
  • Thanks for noting that he's trying to save the image to external storage and trying to read from internal storage, however, passing the actual path of the file to openFileInput() is not correct as it fails stating that path separators are visible. – marienke Jun 29 '17 at 14:24
0

If you are working with bitmaps. Try this

--write to file--

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();
Viswanath Lekshmanan
  • 9,945
  • 1
  • 40
  • 64