我设法将photo1.jpg保存到我在SD卡上创建的Pictures / mockFolder,这是将来参考的代码.
public class CameraActivity extends Activity
{
final int PICTURE_ACTIVITY = 1;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/mockFolder/";
File newdir = new File(dir);
newdir.mkdirs();
String file = dir + "photo1.jpg";
File newfile = new File(file);
try {
newfile.createNewFile();
} catch (IOException e) {}
Uri outputFileUri = Uri.fromFile(newfile);
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(cameraIntent, PICTURE_ACTIVITY);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
AlertDialog msgDialog;
if (resultCode == RESULT_CANCELED) {
msgDialog = createAlertDialog("Alert", "Picture was not saved. Please try again.", "OK");
} else {
msgDialog = createAlertDialog("Alert", "Picture was saved successfully.", "OK");
}
msgDialog.show();
}
private AlertDialog createAlertDialog(String title, String msg, String buttonText){
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
AlertDialog msgDialog = dialogBuilder.create();
msgDialog.setTitle(title);
msgDialog.setMessage(msg);
msgDialog.setButton(buttonText, new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int idx){
finish();
}
});
return msgDialog;
}
public void finish()
{
super.finish();
}
}
解决方法:
您可能还需要创建新文件.
完成File file = new File(path);之后,调用createNewFile();
更确切地说:
String dir = Environment.DIRECTORY_PICTURES + "mockFolder/";
String name = "photo1.jpg";
File file = new File(dir, name);
file.createNewFile();