簡體   English   中英

是否可以在沒有Android v6.0的WRITE_EXTERNAL_STORAGE或READ_EXTERNAL_STORAGE權限的情況下從相機捕獲圖像並從圖庫中選取圖片?

[英]Is it possible to capture image from Camera and pick from Gallery without WRITE_EXTERNAL_STORAGE or READ_EXTERNAL_STORAGE permission in Android v6.0?

通過使用/ data / Android //作為存儲路徑,我可以在沒有API 22中任何權限的情況下從相機獲取圖像(因為Android允許在無需顯式請求API 19+權限的情況下可寫此目錄)。

不幸的是,這在API 23中不起作用。實際上,相機返回的responseCode為0( RESULT_CANCELLED

我究竟做錯了什么?

編輯:如果不超過API 22,則使用此版本的代碼

Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File image_file = null;
try {
    image_file = File.createTempFile("image_file", ".jpg", getFilesDir());
    image_file.setWritable(true, false);
} catch (IOException e) {
    e.printStackTrace();
}
image_file_uri = Uri.fromFile(image_file);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, image_file_uri);
Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
galleryIntent.setType("image/*");
Intent chooserIntent = Intent.createChooser(galleryIntent, getString(R.string.image_intent_chooser_title));
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[]{cameraIntent});
startActivityForResult(chooserIntent, CODE_IMAGE_CAPTURE);

是否有使用您的應用程序捕獲圖像的要求? 如果看不到此鏈接以使用現有的相機應用程序,請執行以下操作:

使用現有的相機應用程序

private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
private Uri fileUri;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // create Intent to take a picture and return control to the calling application
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name

    // start the image capture Intent
    startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}

當執行startActivityForResult()方法時,用戶會看到相機應用程序界面。 用戶完成拍照(或取消操作)之后,用戶界面將返回到您的應用程序,並且您必須攔截onActivityResult()方法以接收意圖的結果並繼續執行應用程序。 有關如何接收完成的意圖的信息,請參閱接收相機意圖結果

是的,可以使用Cory Charlton回答的相機意圖。 要點對您應該非常有用。 它提供了具有兩個方法的幫助程序類:selectImageFromGallery()和takePhotoWithCamera()。 第一個允許從Gallery或設備文件夾中選擇照片,第二個允許使用默認相機拍攝照片。 您還可以將圖片發送到裁剪意向中進行編輯和應用濾鏡。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM