简体   繁体   English

Android:为什么会出现此错误?

[英]Android: Why am i getting this error?

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. 单击save_btn按钮时,它将运行该代码。

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

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 如果您使用的是“相机中的意图”,则可以使用下一个链接中的方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么在android中使用webview时出现错误? - Why I am getting an error when I use webview in android? 为什么我的Android代码中出现空指针错误? - Why am I getting a null pointer error in my Android Code? 为什么我会得到一个nullpointerexception,可能是类错误(Android)? - Why am I getting a nullpointerexception, probably a class error (Android)? 为什么我在android中的活动出错? - Why I am getting error with my activity in android? 为什么我在使用 android studio 时会出现此错误。? - Why am I getting this error while using android studio.? 为什么在Android Studio中出现与主题相关的错误? - why am I getting an Theme related error in android studio? 为什么在Android Studio中的SearchView和OnQueryTextListener上出现此错误? - Why am I getting this error in Android Studio on SearchView and OnQueryTextListener? ANDROID&amp;PHP - 为什么我会收到这个 JSONArray 解析错误 - ANDROID&PHP - Why am I getting this JSONArray parsing error 为什么我在 flutter android 项目中收到 gradle 错误? - Why I am getting gradle error in flutter android project? 为什么我在Android清单中出现“整数预期”错误? - why am I getting “integer expected” error in the Android manifest?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM