0

my program captures an image (as bitmap) but I don't know (and couldn't find) how to save it with a custom name at a custom path... Here's my image capture codes. Can I do it? Or is there any way saving it as another data type, not bitmap? some help will be great. Thanks.

public class ShowMessagesPage extends Activity {
final static int CAMERA_RESULT = 0;
DBAdapter db = new DBAdapter(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_show_messages_page);

    Button userButton = (Button)findViewById(R.id.button1);
    userButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        error();
        }});

    Button buttonPhoto = (Button)findViewById(R.id.buttonPhoto);
    buttonPhoto.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(i, CAMERA_RESULT);
        }});}

protected void onActivityResult(int requestCode, int resultCode,
          Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);

        if (resultCode == RESULT_OK) {
          Bundle extras = intent.getExtras();
          Bitmap bmp = (Bitmap) extras.get("data");



        }
      }

and the other codes..........
Arda Oğul Üçpınar
  • 881
  • 1
  • 14
  • 38

1 Answers1

1

Try this:

Intent getCameraImage = new Intent("android.media.action.IMAGE_CAPTURE");

File cameraFolder;

if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
    cameraFolder = new File(android.os.Environment.getExternalStorageDirectory(),"folder_name_of_your_choice");
else
    cameraFolder= ShowMessagesPage.this.getCacheDir();
if(!cameraFolder.exists())
    cameraFolder.mkdirs();

File photo = new File(Environment.getExternalStorageDirectory(), "folder_name_of_your_choice/camera_snap.jpg");
getCameraImage.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
initialURI = Uri.fromFile(photo);

startActivityForResult(getCameraImage, CAMERA_RESULT);

And in the onActivityResult(), process the Uri initialURI to get your Image which is already saved to the path you have chosen in the code posted above. The name of the image file will be: camera_snap.jpg

Note: The Uri initialURI is declared globally.

Siddharth Lele
  • 27,623
  • 15
  • 98
  • 151