何时在android studio中使用FileProvider ...完全是初学者程序员

时间:2016-09-03 14:20:54

标签: java android class oop

我想在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”混淆!! 它是什么 ?当我们使用它?

1 个答案:

答案 0 :(得分:0)

对于您的代码,它用于创建(照片的)实际文件并获取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);

您提供的代码中有相当好的评论