我使用了以下链接中的代码,非常有帮助,谢谢: 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和i'd喜欢在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;
}
}
}
答案 0 :(得分: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;
}
}
}