1

我使用了下面链接中的代码,非常有帮助,谢谢: http ://www.tutorialforandroid.com/2010/10/take-picture-in-android-with.html

需要帮助解决一些照片问题!takePhoto() 启动 MediaStore.ACTION_IMAGE_CAPTURE,getFile() 创建“Image keeper”目录,然后将拍摄的照片保存在“Image-SOMENUMBER.jpg”名称下!在 onActivityResult() 我想将拍摄的照片显示为 ImageView,我想喜欢将图片重命名为用户在edittext中输入的内容!

有两个问题:

1) 为什么我不能让 ImageView 在 try{} 部分显示我的图片?我究竟做错了什么?如何获取保存图像的路径?

2)有没有一种方法可以让用户按照他们想要的方式命名照片?(比如“另存为”或在某些按钮单击等上重命名。)

欢迎任何想法!谢谢!

这是代码:

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 回答