android 7.0的手机, 想读里面的txt文件,搞不定 , 请指点。工作如下:
一、:
<?xml version="1.0" encoding="utf-8"?>
<!--suppress AndroidDomInspection -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.my20200312">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<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="com.example.my20200312.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"/>
</provider>
</application>
二、:res下xml 下file_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<root-path name="root" path="" />
<files-path name="files" path="zhangbak" />
<external-path name="external" path="zhang5" />
<external-files-path name="name" path="path" />
</paths>
三、主文件:
package com.example.my20200312;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.FileProvider;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.widget.TextView;
import java.io.File;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView zhang2 = (TextView) findViewById(R.id.textView2);
String filename ="3.txt";
File test = new File(Environment.getExternalStorageDirectory()+File.separator+"zhang5", filename);
zhang2.setText((""+test));
Uri uri;
uri = FileProvider.getUriForFile(MainActivity.this,"com.example.my20200312.fileprovider",test);
}
}
别的没有, 执行程序,就闪退,真机模拟器都不行。一共200个园斗