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..........
1

1 Answer 1

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.

5
  • Sorry but Eclipse says there is no methods or variables like "StatusUpdate" and "reqcdCameraImage"... Also there is no imports for them :/ Jan 12, 2013 at 13:31
  • @ArdaOğulÜçpınar: My bad, I pasted my code as is and forgot these modifications. Check the edit. Jan 12, 2013 at 13:32
  • OMG :D I should have seen it :D thanx :D Jan 12, 2013 at 13:34
  • @ArdaOğulÜçpınar: Happens to the best of us. :-) Jan 12, 2013 at 13:44
  • I have a problem with that... When I take the picture and press "ok", program doesn't go back to the previous activity. Just stuck at the camera activity. :/ Jan 12, 2013 at 15:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.