amardco
amardco

Reputation: 17

Save image in specific folder in android device

In my app, i can take a picture and save in gallery(folder 'camera').But i need save it in a specific folder in external memory.This is my code.How i can do it?

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.get_pic);
    init();


    getPic.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(intent, 0);

        }
    });

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

   if (resultCode == RESULT_OK) {

       Bundle ex = data.getExtras();
        bitmap = (Bitmap) ex.get("data");
        myPic.setImageBitmap(bitmap);
    }
}

Upvotes: 0

Views: 2711

Answers (1)

Simon
Simon

Reputation: 1731

This should do it:

private void createDirectoryAndSaveFile(Bitmap imgSave, String fileName) {

    File direct = new File(Environment.getExternalStorageDirectory() + "/DirName");

    if (!direct.exists()) {
        File imageDirectory = new File("/sdcard/DirName/");
        imageDirectory.mkdirs();
    }

    File file = new File(new File("/sdcard/DirName/"), fileName);
    if (file.exists()) {
        file.delete();
    }
    try {
        FileOutputStream out = new FileOutputStream(file);
        imgSave.compress(Bitmap.CompressFormat.JPEG, 100, out);
        out.flush();
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Upvotes: 1

Related Questions