gpt4 book ai didi

android - 试图将图片保存到尚未创建的特定图片文件夹

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:06:40 25 4
gpt4 key购买 nike

我设法将 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();

关于android - 试图将图片保存到尚未创建的特定图片文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8564396/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com