2

I have been trying to work this out but not having any success.

I have a really simple app that captures an image and displays that image to the user. The problem is that it saves the image as the date is was captured, but instead I want to give the image a specific name.

Here is my code:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    this.imageView = (ImageView)this.findViewById(R.id.imageView1);
    Button photoButton = (Button) this.findViewById(R.id.button1);
    photoButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            //intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);

            startActivityForResult(intent, CAMERA_REQUEST); 
        }
    });

protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    System.gc();
    if (requestCode == CAMERA_REQUEST) {  
        Bitmap photo = (Bitmap) data.getExtras().get("data");
        imageView.setImageBitmap(photo);

    }  
}

I would really appreciate it if someone could show me what I need to add and where to set the image name.

SilentKnight
  • 13,761
  • 19
  • 49
  • 78
Ronny vdb
  • 2,324
  • 5
  • 32
  • 74
  • 2
    Look at this link. it can be helpful ;) [Android ACTION_IMAGE_CAPTURE Intent][1] [1]: http://stackoverflow.com/questions/1910608/android-action-image-capture-intent – Imrane Apr 01 '12 at 21:36
  • possiblely you must have solved your problem or shall i help – Ishu Apr 02 '12 at 02:27

1 Answers1

6

Try the below code is one of the solution to your problem::

static Uri capturedImageUri=null;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    this.imageView = (ImageView)this.findViewById(R.id.imageView1);
    Button photoButton = (Button) this.findViewById(R.id.button1);
    photoButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
        Calendar cal = Calendar.getInstance();
File file = new File(Environment.getExternalStorageDirectory(),  (cal.getTimeInMillis()+".jpg"));
    if(!file.exists()){
    try {
        file.createNewFile();
    } catch (IOException e) {
    // TODO Auto-generated catch block
        e.printStackTrace();
    }
    }else{
       file.delete();
    try {
       file.createNewFile();
    } catch (IOException e) {
    // TODO Auto-generated catch block
        e.printStackTrace();
    }
    }
    capturedImageUri = Uri.fromFile(file);
    Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    i.putExtra(MediaStore.EXTRA_OUTPUT, capturedImageUri);
    startActivityForResult(i, CAMERA_RESULT);
       }
    });

protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    if (requestCode == CAMERA_REQUEST) {  
        //Bitmap photo = (Bitmap) data.getExtras().get("data");
        //imageView.setImageBitmap(photo);
        try {
    Bitmap bitmap = MediaStore.Images.Media.getBitmap( getApplicationContext().getContentResolver(),  capturedImageUri);
    imageView.setImageBitmap(bitmap);
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }  
}
Ishu
  • 5,357
  • 4
  • 16
  • 17