Java 剪切照片时FileNotFoundException

Java 剪切照片时FileNotFoundException,java,android,camera,crop,filenotfoundexception,Java,Android,Camera,Crop,Filenotfoundexception,我正在尝试裁剪一张照片,以便在实时壁纸中使用,但当裁剪活动尝试保存新裁剪的图像时,我遇到了FileNotFoundException。这是我正在使用的代码: File file = new File(getFilesDir(), "wallpaper.jpg"); Intent intent = new Intent("com.android.camera.action.CROP"); intent.setData(uri); DisplayMetrics metrics = new Dis

我正在尝试裁剪一张照片,以便在实时壁纸中使用,但当裁剪活动尝试保存新裁剪的图像时,我遇到了FileNotFoundException。这是我正在使用的代码:

File file = new File(getFilesDir(), "wallpaper.jpg");

Intent intent = new Intent("com.android.camera.action.CROP");
intent.setData(uri);

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);

intent.putExtra("outputX", metrics.widthPixels * 2);
intent.putExtra("outputY", metrics.heightPixels);
intent.putExtra("aspectX", metrics.widthPixels * 2);
intent.putExtra("aspectY", metrics.heightPixels);
intent.putExtra("scale", true);
intent.putExtra("noFaceDetection", true);
intent.putExtra("output", Uri.parse("file:/" + file.getAbsolutePath()));

startActivityForResult(intent, REQUEST_CROP_IMAGE);
wallpaper.jpg文件似乎存在于DDMS文件资源管理器上,因此我不确定我做错了什么。非常感谢您的建议

如果您使用的是API级别7或更低, 使用getExternalStorageDirectory,可以 打开表示的根目录的文件 外部存储器。那你应该 把你的数据写在下面 目录:

/Android/data//文件/ 这是您的Java风格 包名称,例如 com.example.android.app。如果 用户的设备正在运行API级别8 或更大,他们将卸载您的 应用程序、此目录和所有 其内容将被删除


getFilesDir返回应用程序的private目录。摄像头无法访问您的私人目录。要使wallpaper.jpg可被摄像头访问,请将其放在某个publix文件夹中。例如,就像Alex说的,它可以是sd卡根。固定代码为:

File file = new File(Environment.getExternalStorageDirectory(),  "wallpaper.jpg");