1. 程式人生 > >android8.0中呼叫相機拍照

android8.0中呼叫相機拍照

網址:https://blog.csdn.net/q992767879/article/details/79654733
  1.<!--android8.0需要的配置,主要是呼叫相機-->
  <provider
    
    android:name="android.support.v4.content.FileProvider"
    
    android:authorities="(你的包名).fileprovider"
    
    android:exported="false"
    
    android:grantUriPermissions="true">
    
    <meta-data
        
        android:name="android.support.FILE_PROVIDER_PATHS"
        
        android:resource="@xml/file_paths">
    </meta-data>
   
</provider>
   2.在rec檔案下建立xml資料夾,在裡面建立xml檔案


   <paths xmlns:android="http://schemas.android.com/apk/res/android">
    
      <external-path name="external_files" path="."/>
</paths>
   3.使用
private void useCamera() {
        
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        
file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()
                
+ "/test/" + System.currentTimeMillis() + ".jpg");
        
file.getParentFile().mkdirs();

        
//改變Uri  com.xykj.customview.fileprovider注意和xml中的一致
        
Uri uri = FileProvider.getUriForFile(this, 
"(你的包名).fileprovider", file);
        
//新增許可權
        
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

        
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
        
startActivityForResult(intent, REQUEST_CAMERA);
    }