2 回答
TA贡献1828条经验 获得超3个赞
将此添加到清单文件
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
然后在运行时请求权限,如下所示
// Check whether this app has write external storage permission or not.
int writeExternalStoragePermission = ContextCompat.checkSelfPermission(ExternalStorageActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
// If do not grant write external storage permission.
if(writeExternalStoragePermission!= PackageManager.PERMISSION_GRANTED)
{
// Request user to grant write external storage permission.
ActivityCompat.requestPermissions(ExternalStorageActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE_WRITE_EXTERNAL_STORAGE_PERMISSION);
}
您需要在与任何外部读取、写入过程交互之前执行这些代码。
TA贡献1793条经验 获得超6个赞
我在这里找到了解决方案。
问题是我想使用以下代码读取文件:
File file = new File(intent.getData().getPath());
BufferedReader br = new BufferedReader(new FileReader(file));
相反,我应该使用这个:
InputStream is = getContentResolver().openInputStream(intent.getData());
BufferedReader br = new BufferedReader(new InputStreamReader(is));
添加回答
举报