Android 安卓:为什么我会出现这个错误?
我正在尝试将位图添加到我的目录中。它保存了图像,但是我得到了一个奇怪的错误,它是用蓝色书写的Android 安卓:为什么我会出现这个错误?,android,save,android-camera,android-bitmap,Android,Save,Android Camera,Android Bitmap,我正在尝试将位图添加到我的目录中。它保存了图像,但是我得到了一个奇怪的错误,它是用蓝色书写的 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.
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
摄像机意图
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);
}
尝试使用以下代码片段:
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;
}
然后: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());
}
如果您正在使用照相机的意图,您可以在下一步中使用该方法。请检查您是否已添加到清单中。重新安装。并确保此包含目录的位图未在任何其他文件资源管理器上打开。i、 e.最好关闭所有应用程序并重新安装。是的,我的清单确实具有该权限。我已尝试重新安装,仍然相同error@smither123将清单添加到您的问题中。谢谢,这两个错误中的一部分看起来都很好。对不起,我对Java还很陌生。有人能帮我解决吗?当然!只需要把它包装成try catch,我会把它添加到回答中谢谢!所有的错误都消失了。我在“fos.write(photoData);”中放了什么?photoData是一个字节[],它包含位图。它创建了文件名,但当我打开图像时,它会显示“找不到媒体”
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());
}