I managed to get photo1.jpg
to save to the Pictures/mockFolder
I created on the SD card, here's the code for future reference.
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();
}
}
You probably have to create your new File too.
After you have done File file = new File(path);
, call createNewFile()
;
Or rather:
String dir = Environment.DIRECTORY_PICTURES + "mockFolder/";
String name = "photo1.jpg";
File file = new File(dir, name);
file.createNewFile();
You could improve your code by calling newFolder.mkdirs()
instead of newFolder.mkdir()
. mkdirs()
creates all the folders needed in the path specified.
Also, you forgot to add a slash after Environment.DIRECTORY_PICTURES
when you set the path variable.
Also, make sure your app has permission to write to the sd card.
Add this to AndroidManifest.xml:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Environment.DIRECTORY_PICTURES
only contains the folder name for the pictures. You need to provide a full path to the location.
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With