Question

Theres a problem I can't seem to fix: In my OnCreate in the CameraActivity, I delete the picture first if it's there. If there is a situation where this is done, the picture file is created but the picture is blank. (so only creates the picture successfully if the file isn't there in the first place). How do I delete the file and create it successfully?

My CameraActivity is defined as follows:

    public class CameraActivity extends Activity
{   
    final int PICTURE_ACTIVITY = 1; 
    @Override
    public void onCreate(Bundle savedInstanceState)
    {  


        Intent h = getIntent();
        String filename = h.getStringExtra("string") + ".jpg";

        String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/" + getString(R.string.app_name)+ "/"; 
        File newdir = new File(dir); 
        try{
            newdir.mkdirs();
        }
        catch(Exception e){}
        String file = dir + filename;
        File newfile = new File(file);
        boolean deleted = newfile.delete();

        try {
            System.out.println("creating:");
            newfile.createNewFile();
        } catch (IOException e) {} 

        Uri outputFileUri = Uri.fromFile(newfile);
        Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
        super.onCreate(savedInstanceState);
        startActivityForResult(cameraIntent, PICTURE_ACTIVITY);
    }

}
Was it helpful?

Solution

I realised the reason it wasn't saving sometimes was because I was taking the SD card out of the phone before it had the chance to save.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top