我正在尝试创建一个应用,允许用户拍摄照片,然后在应用中显示并保存。
我正在使用以下代码拍摄照片(来自Google)
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePictureIntent, 1);
如何判断用户何时按下后退按钮并返回我的应用程序,以便显示图像???
答案 0 :(得分:0)
使用此代码拍照...
File pictureFileDir = getDir();
if (!pictureFileDir.exists() && !pictureFileDir.mkdirs()) {
Log.d("Photo Take", "Can't create directory to save image.");
Toast.makeText(PhotoTake.this , "Can't create directory to save image.",
Toast.LENGTH_LONG).show();
return;
}
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmddhhmmss");
String date = dateFormat.format(new Date());
String photoFile = "Picture_" + date + ".jpg";
String filepath = pictureFileDir.getPath() + File.separator;
File imageFile = new File(filepath , photoFile);
ContentValues image = new ContentValues();
image.put(Images.Media.TITLE, photoFile);
image.put(Images.Media.DISPLAY_NAME, photoFile);
image.put(Images.Media.DESCRIPTION, "Accident data Accachment " + date);
image.put(Images.Media.DATE_ADDED, date);
image.put(Images.Media.DATE_TAKEN, date);
image.put(Images.Media.DATE_MODIFIED, date);
image.put(Images.Media.MIME_TYPE, "image/jpeg");
image.put(Images.Media.ORIENTATION, 0);
File parent = imageFile.getParentFile();
String path = parent.toString().toLowerCase();
String name = parent.getName().toLowerCase();
image.put(Images.ImageColumns.BUCKET_ID, path.hashCode());
image.put(Images.ImageColumns.BUCKET_DISPLAY_NAME, name);
image.put(Images.Media.SIZE, imageFile.length());
image.put(Images.Media.DATA, imageFile.getAbsolutePath());
mCapturedImageURI = PhotoTake.this.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, image);
Intent intent = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,
mCapturedImageURI);
startActivityForResult(intent, req);
现在在onActivityResult中检索它......
if (requestCode == 100 && resultCode == Activity.RESULT_OK) {
if (mCapturedImageURI != null) {
img1.setImageBitmap(getScaledBitmap(mCapturedImageURI));;
System.out.println("Onactivity Result uri = " + mCapturedImageURI.toString());
} else {
Toast.makeText(PhotoTake.this, "Error getting Image",
Toast.LENGTH_SHORT).show();
}
}
您需要此方法来避免OutOfMemory Exception
private Bitmap getScaledBitmap(Uri uri){
Bitmap thumb = null ;
try {
ContentResolver cr = getContentResolver();
InputStream in = cr.openInputStream(uri);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize=8;
thumb = BitmapFactory.decodeStream(in,null,options);
} catch (FileNotFoundException e) {
Toast.makeText(PhotoTake.this , "File not found" , Toast.LENGTH_SHORT).show();
}
return thumb ;
}
希望它有所帮助。
答案 1 :(得分:0)
你在同一个活动中做了startActivityForResult
you need to call
onActivityResult(int requestcode, int resultCode, Intent data){
if(resultCode==RESULT_OK){
if(requestCode==1){
// get your data from the intent
}
}
}