有人可以告诉我为什么这段代码不再将照片保存到画廊了?我让它在某个时刻工作,然后我改变了其他地方的东西,现在这不起作用。
private void openImageIntent() {
// Determine Uri of camera image to save.
final File storageDir = Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
final String fname = ClassName.getUniqueImageFilename();
final File sdImageMainDirectory = new File(storageDir, fname);
outputFileUri = Uri.fromFile(sdImageMainDirectory);
// Camera.
final List<Intent> cameraIntents = new ArrayList<Intent>();
final Intent captureIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
final PackageManager packageManager = getPackageManager();
final List<ResolveInfo> listCam = packageManager.queryIntentActivities(
captureIntent, 0);
for (ResolveInfo res : listCam) {
final String packageName = res.activityInfo.packageName;
final Intent intent = new Intent(captureIntent);
intent.setComponent(new ComponentName(res.activityInfo.packageName,
res.activityInfo.name));
intent.setPackage(packageName);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
cameraIntents.add(intent);
}
// Filesystem.
final Intent galleryIntent = new Intent();
galleryIntent.setType("image/*");
galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
// Chooser of filesystem options.
final Intent chooserIntent = Intent.createChooser(galleryIntent,
"Chose a source");
// Add the camera options.
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
cameraIntents.toArray(new Parcelable[] {}));
startActivityForResult(chooserIntent, REQUEST_IMAGE_CAPTURE);
}
private static String getUniqueImageFilename() {
// TODO Auto-generated method stub
String fileName = "img_" + System.currentTimeMillis() + ".jpg";
return fileName;
}
这是我得到的错误:
10-29 23:53:50.692: E/BitmapFactory(12547): Unable to decode stream: java.io.FileNotFoundException: /file:/storage/emulated/0/DCIM/img_1414623222659.jpg: open failed: ENOENT (No such file or directory)
10-29 23:53:50.692: E/BitmapFactory(12547): Unable to decode stream: java.io.FileNotFoundException: /file:/storage/emulated/0/DCIM/img_1414623222659.jpg: open failed: ENOENT (No such file or directory)
答案 0 :(得分:2)
final File storageDir = Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
您应该在Android Manifest中添加权限以写入外部存储
Android存储分为不同版本的Android SDK。您的代码可以在某些设备上运行,但不是全部。
由于缺少权限,系统无法在系统上创建文件,因此它会给您错误。我找到的最好的方法就是看看如果安装了外部存储器,那么在外部创建一个文件。
以下两个代码片段都可以使用。
Environment.getExternalStorageDirectory() + "/" + "myImages" + "/someimage.jpg";
之后,您可以启动Media Store更新Intent以更新Android Gallery。
或使用此代码
final File dir = new File(context.getFilesDir() + "/nfs/guille/groce/users/nicholsk/workspace3/SQLTest");
dir.mkdirs(); //create folders where write files
final File file = new File(dir, "BlockForTest.txt");