[英]Camera Intent - how to?
很抱歉打扰你们,但是我无法在Intent中使用意图拍照时得到解决方案。 我知道本机相机的默认行为是将图片保存在操作系统的默认目录/位置。我有一些要求,当我使用相机应用程序单击时,我不想保存图片。 必须解决此问题,就像一旦我们拍照后就可以立即将其删除,或者应该有其他替代方法使我们不允许操作系统保存图像,请提供帮助。
这是我尝试过的一段代码,通过创建目录然后删除文件尝试了几种方法,没有任何效果。
public void takeImageFromCamera() {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check for the integer request code originally supplied to startResolutionForResult().
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
if (isCameraPermissionGranted()) {
bitmap= (Bitmap) data.getExtras().get("data");
// bitmap = processReuiredImage(picUri);
getProfileDetailViaFace(encodeImageBitmapToString(bitmap));
Log.d("path",String.valueOf(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES)));
// getApplicationContext().getContentResolver().delete(, "/storage/emulated/0/Pictures", null);
// mediaStorageDir.getPath().delete();
} else {
requestCameraPermission();
}
}
public void takeImageFromCamera() {
File file = getOutputMediaFile(CAMERA_FILE_TYPE);
if (Build.VERSION.SDK_INT >= 24) {
try {
Method m = StrictMode.class.getMethod("disableDeathOnFileUriExposure");
m.invoke(null);
} catch (Exception e) {
e.printStackTrace();
}
}
picUri = Uri.fromFile(file);
Intent takePictureIntent = new
Intent(MediaStore.ACTION_IMAGE_CAPTURE_SECURE);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, picUri);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, CAMERA_REQUEST);
}
}
private File getOutputMediaFile(int type) {
mediaStorageDir = new
File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "peppercard");
/**Create the storage directory if it does not exist*/
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
return null;
}
}
/**Create a media file name*/
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
if (type == CAMERA_FILE_TYPE) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"IMG_" + timeStamp + ".jpeg");
} else {
return null;
}
} return mediaFile;
}
问题是我有一些要求,我不想在使用相机应用程序单击时保存图片
是否保存图像取决于相机应用程序,而不是您。 数百种相机应用程序可能会响应ACTION_IMAGE_CAPTURE
,并且这些应用程序的开发人员可以执行所需的任何操作。
必须解决此问题,例如,一旦我们拍照后就可以立即将其删除,或者应该有一种替代方法,我们将不允许操作系统保存图像,
使用相机API或环绕它们的库(例如CameraKit-Android,Fotoapparat)自己拍摄照片。
There has to be a solution of this issue, be it like once we take
a picture we could delete it right away
确实有。 您可以指定相机应用程序必须将图像放入文件的路径(甚至使用文件提供程序)。
然后,在完成相机应用程序后,您可以从该文件中获取图像,然后删除该文件。
看看Intent.EXTRA_OUTPUT
。
很标准的问题。 您可以在此站点上找到很多示例代码。
最终,在过去2天的等待之后,我找到了答案,是的。它不会保存文件,因为我只是从活动返回后删除文件。
String [] projection = {MediaStore.Images.Media.DATA};
游标游标= managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,projection,null,null,null)
int column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToLast();
String imagePath = cursor.getString(column_index_data);
Bitmap bitmapImage = BitmapFactory.decodeFile(imagePath );
Log.d("bitmapImage", bitmapImage.toString()); /*delete file after taking picture*/
Log.d("imagePath", imagePath.toString());
File f = new File(imagePath);
if (f.exists()){
f.delete();
}
sendBroadcast(newIntent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,Uri.fromFile(f)));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.