1

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;  
    }  
  }  
}  
4

1 に答える 1

1

私の問題の一部の名前変更を解決できませんでしたが、このコードは私のために仕事をしました! 私はそれが少し手の込んだことができると確信していますが、うまくいきます! 写真を撮り、SD カードに保存し、パスを取得する必要があります。

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;  
    }  
  }  
} 
于 2012-05-18T00:28:17.557 に答える