[英]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.