简体   繁体   中英

Saving/renaming taken photos on sd-card in Android

i've used code from link below,was very helpfull,thank you: http://www.tutorialforandroid.com/2010/10/take-picture-in-android-with.html

need help with some photo issues! takePhoto() starts MediaStore.ACTION_IMAGE_CAPTURE, getFile() creates "Image keeper" directory and then saves taken picture under "Image-SOMENUMBER.jpg" name!in onActivityResult() i'd like to show taken picture as ImageView and i'd like to rename picture to something user inputs in edittext or something!

have two questions:

1) why can't i get ImageView to show my picture in try{} part? what am i doing wrong? how can i get a path of saved image?

2) is there a way i can enable users to name photos the way they want?(something like "save as" or renaming on some button click ect.)

any idea is welcome!thank you!

here is the code:

private static final int TAKE_PHOTO_CODE = 1;  

private void takePhoto(){  
  final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);  
  intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(getFile(this)) );   
  startActivityForResult(intent, TAKE_PHOTO_CODE);  
}  

private File getFile(Context context){  

  final File path = new File( Environment.getExternalStorageDirectory(), "Image keeper" );  
  if(!path.exists()){  
    path.mkdir(); 
  }
  String name;
  int n = 100000;
  int rand;
  rand = new Random().nextInt(n);
  name = "Image-" + rand + ".jpg";
  File file = new File(path,name);

  return file;  
}  

@Override  
protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  if (resultCode == RESULT_OK) {
      switch(requestCode){  
      case TAKE_PHOTO_CODE:  
        final File file = getFile(this);
        try {  
          Bitmap captureBmp;
          captureBmp = Media.getBitmap(getContentResolver(), Uri.fromFile(file) );  

          iv = (ImageView) findViewById(R.id.imageView1);
          iv.setImageBitmap(captureBmp);

        } catch (FileNotFoundException e) {  
          e.printStackTrace();  
        } catch (IOException e) {  
          e.printStackTrace();  
        }  
      break;  
    }  
  }  
}  

couldn't solve renaming part of my problem but this piece of code did a job for me! i'm sure it can be done a bit fancier but it works! it involves taking picture, saving to sd card and getting path to it!

ImageView iv;
static File image;
Uri ImageUri;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

   Button btn = (Button) findViewById(R.id.button1);

   btn.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {

        takePhoto();

    }
});
}

private static final int TAKE_PHOTO_CODE = 1;  

private void takePhoto(){
  image = getFile(this);
  final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);  
  intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(image) );   
  startActivityForResult(intent, TAKE_PHOTO_CODE);  
}  

private File getFile(Context context){  

  final File path = new File( Environment.getExternalStorageDirectory(), "Image keeper" );  
  if(!path.exists()){  
    path.mkdir(); 
  }
  String name;
  int n = 100000;
  int rand;
  rand = new Random().nextInt(n);
  name = "Image-" + rand + ".jpg";
  File fileimage = new File(path, name);

  return fileimage;  
}  

@Override  
protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  if (resultCode == RESULT_OK) {
      switch(requestCode){  
      case TAKE_PHOTO_CODE:  

        try {  
          Bitmap captureBmp;
          captureBmp = Media.getBitmap(getContentResolver(), Uri.fromFile(image) );

          ImageUri = Uri.fromFile(image);
          String pathToImage = ImageUri.getPath();

          iv = (ImageView) findViewById(R.id.imageView1);
          iv.setImageBitmap(captureBmp);

        } catch (FileNotFoundException e) {  
          e.printStackTrace();  
        } catch (IOException e) {  
          e.printStackTrace();  
        } 
        iv.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        iv.setAdjustViewBounds(true);

      break;  
    }  
  }  
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM