Android Nougat在捕获视频时返回null

时间:2017-10-30 11:47:06

标签: java android android-7.0-nougat android-7.1-nougat

Android版本7返回空URI。

我的清单文件

  <?xml version="1.0" encoding="utf-8"?>
  <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myautochatapp.sampleapp">

<uses-permission android:name="android.permission.INTERNET" />

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <activity android:name=".VideoActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="@string/file_provider_authority"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths"/>
    </provider>
</application>

 </manifest>

在清单

中设置字符串
  <string name="file_provider_authority" translatable="false">com.myautochatapp.sampleapp.fileprovider</string>

MainActivity

     public class VideoActivity extends AppCompatActivity {

Button capture_video;
TextView path_text;
private Uri fileUri;
final int MEDIA_CAPTURE_VIDEO=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_video);
    capture_video= (Button) findViewById(R.id.capture_video);
    path_text= (TextView) findViewById(R.id.path_text);
    capture_video.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            callVideoCapFuc();
        }


    });

}

private void callVideoCapFuc() {
    File vfile= null;
    String dateTimeOrg= new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss").format(new Date()).toString();

    try {
        vfile = createTemporaryFile("VID_"+dateTimeOrg,".mp4");

    } catch (Exception e) {
        e.printStackTrace();
    }
    fileUri = FileProvider.getUriForFile(VideoActivity.this,
            getString(R.string.file_provider_authority),
            vfile);

    Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
    takeVideoIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    if (takeVideoIntent.resolveActivity(getPackageManager()) != null) {
        takeVideoIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

        startActivityForResult(takeVideoIntent, MEDIA_CAPTURE_VIDEO);
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
        case MEDIA_CAPTURE_VIDEO:
            Uri videoUri = null;
            try {
                 videoUri = data.getData();
            }catch (Exception e){
                e.printStackTrace();
            }


            if (videoUri.toString()!=null && !videoUri.toString().equalsIgnoreCase("")){
                path_text.setText(videoUri.toString());
            }

            break;
    }
}

private File createTemporaryFile(String part, String ext) throws Exception
{
    File tempDir= Environment.getExternalStorageDirectory();
    tempDir=new File(tempDir.getAbsolutePath()+"/Mine/");
    if(!tempDir.exists())
    {
        tempDir.mkdirs();
    }
    return File.createTempFile(part, ext, tempDir);
}
   }

提供商路径provider_paths

    <?xml version="1.0" encoding="utf-8"?>
  <paths>
<external-path
    name="external_files" path="." />
 </paths>

XML layout activity_video

     <?xml version="1.0" encoding="utf-8"?>
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.myautochatapp.sampleapp.VideoActivity">

<Button
    android:layout_width="match_parent"
    android:id="@+id/capture_video"
    android:layout_height="wrap_content" />

    <TextView
    android:layout_width="match_parent"
    android:id="@+id/path_text"
    android:layout_below="@+id/capture_video"
    android:layout_height="wrap_content" />
  </RelativeLayout>

未在代码中给予权限但在启动应用程序时设置,但仍然无法正常工作,如果它的android 7,则返回null onActivityResult()。在Android 6中工作正常

1 个答案:

答案 0 :(得分:0)

将这些行放在活动中的onCreate()方法或片段中的onCreateView()方法中。

StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();         StrictMode.setVmPolicy(builder.build());