Java 何时在android studio中使用FileProvider…完全是初学者
我正试图在android studio中获得制作应用程序的知识,所以如果找到这个代码片段Java 何时在android studio中使用FileProvider…完全是初学者,java,android,class,oop,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 ac
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);
}
}
}
我了解几乎所有的代码,但我只是混淆了“文件提供商”!!
它是什么?我们什么时候用 对于您的代码,它用于创建(照片的)实际文件并获取
句柄
// 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)
您提供的代码中的注释非常好谢谢您的回答。我真的得到了新的信息。你能告诉我Uri是用来做什么的吗。它是一个紧凑的字符序列,用于标识抽象资源或物理资源……它可用于命名资源或定位资源(如上所述)。它类似于特定项目的文件路径、web地址或ftp地址……只是在这种情况下,它给出了要保存拍摄照片的文件的位置。否则(谷歌其余部分)[前往(Android开发者)[
Uri photoURI = FileProvider.getUriForFile(this,"com.example.android.fileprovider", photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);