簡體   English   中英

何時在android studio中使用FileProvider…完全是初學者

[英]when to use FileProvider in android studio …totally beginner programmer

我試圖在android studio中獲取有關制作應用程序的知識,因此,如果發現此代碼段。

enter code herestatic final int REQUEST_TAKE_PHOTO = 1;
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
    // Create the File where the photo should go
    File photoFile = null;
    try {
        photoFile = createImageFile();
    } catch (IOException ex) {
        // Error occurred while creating the File
        ...
    }
    // Continue only if the File was successfully created
    if (photoFile != null) {
        Uri photoURI = FileProvider.getUriForFile(this,
                                              "com.example.android.fileprovider",
                                              photoFile);
        takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
        startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
    }
}

}

我幾乎了解所有代碼,但是我只是與“ FileProvider”混淆! 它是什么 ? 什么時候使用呢?

就您的代碼而言,它用於創建(照片的)實際文件並獲取handle ...

    // Create the File where the photo should go
    File photoFile = null;
    try {
        photoFile = createImageFile();
    } catch (IOException ex) {
        // Error occurred while creating the File
        ...
    }

..然后使用句柄獲取其URI(在手機存儲中的位置)

Uri photoURI = FileProvider.getUriForFile(this,"com.example.android.fileprovider", photoFile);

...然后拍照。 保存到該位置(URI)

takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);

您提供的代碼中的注釋相當不錯

暫無
暫無

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

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