1

I'm trying to add a bitmap to my directory. It saves the image but i'm getting this weird error which is in blue writing.

ample.myapplication W/System.err﹕ java.io.FileNotFoundException: /CameraApp: open failed: EROFS (Read-only file system)
04-23 17:07:26.259  12120-12120/com.example.myapplication W/System.err﹕ at libcore.io.IoBridge.open(IoBridge.java:409)
04-23 17:07:26.259  12120-12120/com.example.myapplication W/System.err﹕ at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
04-23 17:07:26.259  12120-12120/com.example.myapplication W/System.err﹕ at java.io.FileOutputStream.<init>(FileOutputStream.java:128)
04-23 17:07:26.259  12120-12120/com.example.myapplication W/System.err﹕ at java.io.FileOutputStream.<init>(FileOutputStream.java:117)
04-23 17:07:26.259  12120-12120/com.example.myapplication W/System.err﹕ at com.example.myapplication.MainActivity.save_btn(MainActivity.java:119)
04-23 17:07:26.259  12120-12120/com.example.myapplication W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
04-23 17:07:26.259  12120-12120/com.example.myapplication W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)
04-23 17:07:26.259  12120-12120/com.example.myapplication W/System.err﹕ at android.view.View$1.onClick(View.java:3825)
04-23 17:07:26.259  12120-12120/com.example.myapplication W/System.err﹕ at android.view.View.performClick(View.java:4445)
04-23 17:07:26.259  12120-12120/com.example.myapplication W/System.err﹕ at android.view.View$PerformClick.run(View.java:18446)
04-23 17:07:26.259  12120-12120/com.example.myapplication W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:733)
04-23 17:07:26.259  12120-12120/com.example.myapplication W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95)
04-23 17:07:26.259  12120-12120/com.example.myapplication W/System.err﹕ at android.os.Looper.loop(Looper.java:136)
04-23 17:07:26.259  12120-12120/com.example.myapplication W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5146)
04-23 17:07:26.259  12120-12120/com.example.myapplication W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
04-23 17:07:26.259  12120-12120/com.example.myapplication W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)
04-23 17:07:26.259  12120-12120/com.example.myapplication W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
04-23 17:07:26.259  12120-12120/com.example.myapplication W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
04-23 17:07:26.259  12120-12120/com.example.myapplication W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
04-23 17:07:26.259  12120-12120/com.example.myapplication W/System.err﹕ Caused by: libcore.io.ErrnoException: open failed: EROFS (Read-only file system)
04-23 17:07:26.259  12120-12120/com.example.myapplication W/System.err﹕ at libcore.io.Posix.open(Native Method)
04-23 17:07:26.259  12120-12120/com.example.myapplication W/System.err﹕ at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
04-23 17:07:26.259  12120-12120/com.example.myapplication W/System.err﹕ at libcore.io.IoBridge.open(IoBridge.java:393)
04-23 17:07:26.259  12120-12120/com.example.myapplication W/System.err﹕ ... 18 more

This is the code it uses to save the bitmap. When the save_btn button is clicked it runs that code.

 public void save_btn(View v) {
       FileOutputStream out = null;
        try {
            out = new FileOutputStream(filename);
            image.compress(Bitmap.CompressFormat.PNG, 100, out);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();

            }

            AlertDialog alertDialog = new AlertDialog.Builder(this).create();
            alertDialog.setTitle("Saved");
            alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {

                }
            });
            alertDialog.show();
        }
    }

Camera intent

public void capture_btn(View v) {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        File file = new File(Environment.getExternalStorageDirectory() + File.separator + "image.jpg");
        takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
    }
MiguelHincapieC
  • 5,445
  • 7
  • 41
  • 72
smither123
  • 357
  • 3
  • 6
  • 21

1 Answers1

1

Try using this snippet of code:

public static final int MEDIA_TYPE_IMAGE = 1;
public static final int MEDIA_TYPE_VIDEO = 2;

/** Create a File for saving an image or video */
public static File getOutputMediaFile(int type){
    // To be safe, you should check that the SDCard is mounted
    // using Environment.getExternalStorageState() before doing this.

    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES), "App");
    // This locat ion works best if you want the created images to be shared
    // between applications and persist after your app has been uninstalled.

    // Create the storage directory if it does not exist
    if (! mediaStorageDir.exists()){
        if (! mediaStorageDir.mkdirs()){
            android.util.Log.d("log", "failed to create directory");
            return null;
        }
    }

    // Create a media file name
    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE){
        mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                "IMG_"+ "IdelityPhotoTemp" + ".png");
    } else if(type == MEDIA_TYPE_VIDEO) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                "VID_"+ "idelityVideoTemp" + ".mp4");
    } else {
        return null;
    }

    return mediaFile;
}

And then:

try {
    File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
    if (pictureFile == null){
        android.util.Log.d("log", "Error creating media file, check storage permissions");
        return null;
    }

    FileOutputStream fos = new FileOutputStream(pictureFile);
    fos.write(photoData);
    fos.close();

} catch (FileNotFoundException e) {
    android.util.Log.d("log", "File not found: " + e.getMessage());
} catch (IOException e) {
    android.util.Log.d("log", "Error accessing file: " + e.getMessage());
}

In the case you are using Intent from Camera, you can use the approach in the next link

Community
  • 1
  • 1
MiguelHincapieC
  • 5,445
  • 7
  • 41
  • 72
  • Thanks, everything looks good a part from these two errors. http://i.imgur.com/t7mRvoB.png Sorry , i'm pretty new to Java. Could someone help me solve them? – smither123 Apr 23 '15 at 18:32
  • Sure! just need to wrap it into try catch, Im gonna add it to the answer – MiguelHincapieC Apr 23 '15 at 18:48
  • Thank you! all the errors have gone. What do i put inside here "fos.write(photoData);" ? – smither123 Apr 23 '15 at 19:19
  • photoData is a byte[] which contains your bitmap. – MiguelHincapieC Apr 23 '15 at 19:23
  • It creates the file name but when i go to open the image it says "Media not found." – smither123 Apr 23 '15 at 19:51
  • Ok, sounds like the data in `photoData` has some problem, can you post where are you getting the bitmap? I mean we successfully created the file but it is not getting filled (it's what Im thinking is) – MiguelHincapieC Apr 23 '15 at 19:55
  • I've created a variable called "byte image;" and i use "bitmap image" on my onActivityResult when i open the camera intent application for my app. – smither123 Apr 23 '15 at 20:51
  • The image has been saving in the root directory called "image.jpg" for some reason. I'm not getting any errors like i was before. – smither123 Apr 23 '15 at 20:52
  • Hold on, hahaha, there is no way with the code above it could be ".jpg" :D – MiguelHincapieC Apr 23 '15 at 20:56
  • oh, it's my fault. In my camera intent i've said "public void capture_btn(View v) { Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); File file = new File(Environment.getExternalStorageDirectory() + File.separator + "image.jpg"); takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file)); startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE); } " – smither123 Apr 23 '15 at 20:59
  • I've put it in my question. Makes more sense now. – smither123 Apr 23 '15 at 20:59
  • ohh ok, you are getting it from camera intent, Im going to add a link to the answer which can help you. – MiguelHincapieC Apr 23 '15 at 21:11