Android如何使用FileProvider从相机捕获图片并保存在根目录中
创始人
2024-10-09 22:34:48
0
  1. 在AndroidManifest.xml文件中添加FileProvider,设置authorities属性和对应的paths。

   

  1. 创建一个xml文件provider_paths.xml,定义要共享的文件路径。在此示例中,我们要分享应用程序的根目录。

   

  1. 在MainActivity.java中,先检查权限,然后定义一个临时文件以及一个FileProvider URI。通过Intent启动相机应用程序,并将URI传递给它。收到相机应用程序返回的result后,将图像保存到获得的URI中,并使用ContentResolver通知媒体库更新。注意,我们使用FileProvider.getUriForFile()方法获取此URI。
// 检查权限
String[] permissions = new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE};
boolean hasPermission = true;
for (String permission : permissions) {
   if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
       hasPermission = false;
       break;
   }
}
if (!hasPermission) {
   ActivityCompat.requestPermissions(this, permissions, 1);
   return;
}

// 定义一个临时文件和URI
File photoFile = null;
try {
   photoFile = new File(getExternalFilesDir(null), "temp.jpg");
   photoFile.createNewFile();
} catch (IOException e) {
   e.printStackTrace();
}
Uri photoURI = FileProvider.getUriForFile(this, "com.example.myapp.fileprovider", photoFile);

// 打开相机应用程序并传递URI
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePicture

相关内容

热门资讯

透视能赢"poker... 透视能赢"pokerworld辅助器"详细辅助黑科技教程(从前真的有挂);人气非常高,ai更新快且高...
透视工具"wepok... 透视工具"wepoker永久免费脚本"详细辅助详细教程(一向有挂);建议优先通过wepoker永久免...
透视科技"菠萝辅助器... 透视科技"菠萝辅助器免费版的功能介绍"详细辅助微扑克教程(竟然是真的有挂);菠萝辅助器免费版的功能介...
透视美元局"wepo... 透视美元局"wepoker辅助器有哪些功能"详细辅助德州论坛(一向存在有挂);人气非常高,ai更新快...
透视私人局"aapo... 1、透视私人局"aapoker脚本怎么用"详细辅助必赢教程(固有真的有挂);详细教程。2、aapok...
透视玄学"wepok... 透视玄学"wepoker辅助器官方"详细辅助2025新版教程(一直真的有挂)是一款可以让一直输的玩家...
透视辅助"wepok... 透视辅助"wepoker怎么发冤家牌"详细辅助AI教程(一向有挂);《WPK辅助透视》‌:支持手机实...
透视安卓版"impo... 透视安卓版"impoker辅助"详细辅助黑科技教程(真是是有挂)关于impoker辅助机制的,其中提...
透视透视"aapok... 透视透视"aapoker怎么控制牌"详细辅助力荐教程(都是是真的有挂);1分钟了解详细教程(微信 1...
辅助透视"sohoo... 1、辅助透视"sohoo poker辅助"详细辅助揭秘教程(最初是有挂)。2、sohoo poker...