java.io.IOException权限即使在请求权限后也被拒绝
我在某些设备(api 23之前和之后)上遇到异常: 在我的java.io.IOException权限即使在请求权限后也被拒绝,java,android,android-permissions,android-file,Java,Android,Android Permissions,Android File,我在某些设备(api 23之前和之后)上遇到异常: 在我的AndroidManifest.xml中,我确实拥有以下权限: <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 这是我的密码: File downloadsFolder = Environment.getExternalStoragePublicDirectory( Environment.DIREC
AndroidManifest.xml
中,我确实拥有以下权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
这是我的密码:
File downloadsFolder = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS);
if (!downloadsFolder.exists()) {
downloadsFolder.mkdir();
}
File imageFile = new File(downloadsFolder, fileName);
if (!imageFile.exists()) {
try {
imageFile.createNewFile(); // Error on some devices
...
统计数字:
统计信息:以下是minSdkVersion 15和targetSdkVersion 28的解决方案 基于您的问题的工作代码段如下所示 已成功下载映像快照 代码布局视图 格雷德尔先生
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.zackdawood.albummanager"
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
strings.xml
<resources>
<string name="app_name">AlbumManager</string>
<string name="write_button">Write Image</string>
<string name="read_button">Read Image</string>
<string name="request_access">Request Access</string>
</resources>
FileDownloader.java
package com.zackdawood.albummanager;
import android.util.Log;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class FileDownloader {
private static final String TAG = "FileDownloader";
private static final int MEGABYTE = 1024 * 1024;
public static void downloadFile(String fileUrl, File directory) {
try {
Log.v(TAG, "downloadFile() invoked ");
Log.v(TAG, "downloadFile() fileUrl " + fileUrl);
Log.v(TAG, "downloadFile() directory " + directory);
URL url = new URL(fileUrl);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.connect();
InputStream inputStream = urlConnection.getInputStream();
FileOutputStream fileOutputStream = new FileOutputStream(directory);
int totalSize = urlConnection.getContentLength();
byte[] buffer = new byte[MEGABYTE];
int bufferLength = 0;
while ((bufferLength = inputStream.read(buffer)) > 0) {
fileOutputStream.write(buffer, 0, bufferLength);
}
fileOutputStream.close();
Log.v(TAG, "downloadFile() completed ");
} catch (FileNotFoundException e) {
e.printStackTrace();
Log.e(TAG, "downloadFile() error" + e.getMessage());
Log.e(TAG, "downloadFile() error" + e.getStackTrace());
} catch (MalformedURLException e) {
e.printStackTrace();
Log.e(TAG, "downloadFile() error" + e.getMessage());
Log.e(TAG, "downloadFile() error" + e.getStackTrace());
} catch (IOException e) {
e.printStackTrace();
Log.e(TAG, "downloadFile() error" + e.getMessage());
Log.e(TAG, "downloadFile() error" + e.getStackTrace());
}
}
}
MainActivity.java
package com.zackdawood.albummanager;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import java.io.File;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private static final String[] PERMISSIONS = {android.Manifest.permission.READ_EXTERNAL_STORAGE,
android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
android.Manifest.permission.INTERNET};
private static boolean hasPermissions(Context context, String... permissions) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
for (String permission : permissions) {
if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
}
return true;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActivityCompat.requestPermissions(MainActivity.this, PERMISSIONS, 112);
Log.v(TAG, "onCreate() Method invoked ");
}
public void write(View view) {
if (!hasPermissions(MainActivity.this, PERMISSIONS)) {
Log.v(TAG, "download() Method DON'T HAVE PERMISSIONS ");
Toast t = Toast.makeText(getApplicationContext(), "You don't have write access !", Toast.LENGTH_LONG);
t.show();
} else {
new DownloadFile().execute("https://upload.wikimedia.org/wikipedia/commons/thumb/c/c2/Adler.jpg/547px-Adler.jpg", "lab_result_2019-04-06.jpg");
}
}
public void read(View view) {
if (!hasPermissions(MainActivity.this, PERMISSIONS)) {
Log.v(TAG, "download() Method DON'T HAVE PERMISSIONS ");
Toast t = Toast.makeText(getApplicationContext(), "You don't have read access !", Toast.LENGTH_LONG);
t.show();
} else {
File folder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
File directory = new File(folder + "/TB System");
File imgFile = new File(directory, "lab_result_2019-04-06.jpg");
Log.v(TAG, "view() Method imgfile " + imgFile.getAbsolutePath());
Uri path = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + ".fileprovider", imgFile);
Log.v(TAG, "view() Method path " + path);
Intent imageIntent = new Intent(Intent.ACTION_VIEW);
imageIntent.setDataAndType(path, "image/*");
imageIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
imageIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
try {
startActivity(imageIntent);
} catch (ActivityNotFoundException e) {
Toast.makeText(MainActivity.this, "No Application available to view Image", Toast.LENGTH_SHORT).show();
}
}
}
public void request(View view) {
ActivityCompat.requestPermissions(MainActivity.this, PERMISSIONS, 112);
}
private class DownloadFile extends AsyncTask<String, Void, Void> {
@Override
protected Void doInBackground(String... strings) {
Log.v(TAG, "doInBackground() Method invoked ");
String fileUrl = strings[0]; // -> http://maven.apache.org/maven-1.x/maven.pdf
String fileName = strings[1]; // -> maven.pdf
File folder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
File directory = new File(folder + "/TB System");
if (!directory.exists()) {
directory.mkdir();
Log.v(TAG, "doInBackground() Method Directory Created ");
}
File imgFile = new File(directory, fileName);
Log.v(TAG, "doInBackground() imgFile invoked " + imgFile.getAbsolutePath());
Log.v(TAG, "doInBackground() imgFile invoked " + imgFile.getAbsoluteFile());
try {
imgFile.createNewFile();
Log.v(TAG, "doInBackground() file created" + imgFile);
} catch (IOException e) {
e.printStackTrace();
Log.e(TAG, "doInBackground() error" + e.getMessage());
Log.e(TAG, "doInBackground() error" + e.getStackTrace());
}
FileDownloader.downloadFile(fileUrl, imgFile);
Log.v(TAG, "doInBackground() file download completed");
return null;
}
}
}
package com.zackdawood.albummanager;
导入android.content.ActivityNotFoundException;
导入android.content.Context;
导入android.content.Intent;
导入android.content.pm.PackageManager;
导入android.net.Uri;
导入android.os.AsyncTask;
导入android.os.Build;
导入android.os.Bundle;
导入android.os.Environment;
导入android.support.v4.app.ActivityCompat;
导入android.support.v4.content.FileProvider;
导入android.support.v7.app.AppActivity;
导入android.util.Log;
导入android.view.view;
导入android.widget.Toast;
导入java.io.File;
导入java.io.IOException;
公共类MainActivity扩展了AppCompatActivity{
私有静态最终字符串TAG=“MainActivity”;
私有静态最终字符串[]权限={android.Manifest.permission.READ\u外部存储,
android.Manifest.permission.WRITE\u外部存储,
android.Manifest.permission.INTERNET};
私有静态权限(上下文、字符串…权限){
if(Build.VERSION.SDK_INT>=Build.VERSION_code.M&&context!=null&&permissions!=null){
用于(字符串权限:权限){
if(ActivityCompat.checkSelfPermission(context,permission)!=PackageManager.permission\u已授予){
返回false;
}
}
}
返回true;
}
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActivityCompat.requestPermissions(MainActivity.this,PERMISSIONS,112);
v(标记“onCreate()方法已调用”);
}
公共无效写入(视图){
如果(!hasPermissions(MainActivity.this,PERMISSIONS)){
v(标记“download()方法没有权限”);
Toast t=Toast.makeText(getApplicationContext(),“您没有写权限!”,Toast.LENGTH\u LONG);
t、 show();
}否则{
新建下载文件()。执行(“https://upload.wikimedia.org/wikipedia/commons/thumb/c/c2/Adler.jpg/547px-Adler.jpg“,“实验室结果2019-04-06.jpg”);
}
}
公共无效读取(视图){
如果(!hasPermissions(MainActivity.this,PERMISSIONS)){
v(标记“download()方法没有权限”);
Toast t=Toast.makeText(getApplicationContext(),“您没有读取权限!”,Toast.LENGTH\u LONG);
t、 show();
}否则{
File folder=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY\u DCIM);
文件目录=新文件(文件夹+“/TB系统”);
文件imgFile=新文件(目录“lab_result_2019-04-06.jpg”);
Log.v(标记“view()方法imgfile”+imgfile.getAbsolutePath());
Uri路径=FileProvider.getUriForFile(这个,BuildConfig.APPLICATION_ID+“.FileProvider”,imgFile);
Log.v(标记“view()方法路径”+path);
Intent imageIntent=新的意图(Intent.ACTION\u视图);
setDataAndType(路径,“image/*”);
imageIntent.setFlags(Intent.FLAG\u ACTIVITY\u CLEAR\u TOP);
imageIntent.addFlags(Intent.FLAG\授予\读取\ URI\权限);
试一试{
星触觉(imageIntent);
}捕获(ActivityNotFounde异常){
Toast.makeText(MainActivity.this,“没有可用于查看图像的应用程序”,Toast.LENGTH_SHORT.show();
}
}
}
公共作废请求(视图){
ActivityCompat.requestPermissions(MainActivity.this,PERMISSIONS,112);
}
私有类下载文件扩展异步任务{
@凌驾
受保护的Void doInBackground(字符串…字符串){
v(标记“doInBackground()方法已调用”);
String fileUrl=strings[0];//->http://maven.apache.org/maven-1.x/maven.pdf
字符串文件名=字符串[1];//->maven.pdf
File folder=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY\u DCIM);
文件目录=新文件(文件夹+“/TB系统”);
如果(!directory.exists()){
mkdir()目录;
v(标记“doInBackground()方法目录已创建”);
}
File imgFile=新文件(目录、文件名);
Log.v(标记“doInBackground()imgFile已调用”+imgFile.getAbsolutePath());
Log.v(标记“doInBackground()调用的imgFile”+imgFile.getAbsoluteFile());
试一试{
imgFile.createNewFile();
Log.v(标记“doInBackground()文件已创建”+imgFile);
}捕获(IOE异常){
e、 printStackTrace();
Log.e(标记“doInBackground()error”+e.getMessage());
Log.e(标记“doInBackground()error”+e.getStackTrace());
}
下载文件(fileUrl,imgFile);
Log.v(标记“doInBackground()文件下载完成”);
返回null;
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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=".MainActivity">
<LinearLayout
android:layout_width="395dp"
android:layout_height="715dp"
android:orientation="vertical"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp">
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/write_button"
android:onClick="write"/>
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/read_button"
android:onClick="read"/>
<Button
android:id="@+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/request_access"
android:onClick="request"/>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
读取图像输出
在onActivityResult中,你可以完成写作
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_SAVE_PICTURE) {
if (resultCode == RESULT_OK) {
final Uri outUri = (data == null) ? null : data.getData();
OutputStream outStream = getContentResolver().openOutputStream(outUri, "w");
// write to outStream
outStream.close();
} else finish();
return;
}
super.onActivityResult(requestCode, resultCode, data);
}
如果您的目标api级别>=29,则可能会出现以下错误:bcz of new。
api级别29的解决方案
在应用程序的mani中将requestLegacyExternalStorage设置为true
package com.zackdawood.albummanager;
import android.support.v4.content.FileProvider;
public class GenericFileProvider extends FileProvider {
}
package com.zackdawood.albummanager;
import android.util.Log;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class FileDownloader {
private static final String TAG = "FileDownloader";
private static final int MEGABYTE = 1024 * 1024;
public static void downloadFile(String fileUrl, File directory) {
try {
Log.v(TAG, "downloadFile() invoked ");
Log.v(TAG, "downloadFile() fileUrl " + fileUrl);
Log.v(TAG, "downloadFile() directory " + directory);
URL url = new URL(fileUrl);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.connect();
InputStream inputStream = urlConnection.getInputStream();
FileOutputStream fileOutputStream = new FileOutputStream(directory);
int totalSize = urlConnection.getContentLength();
byte[] buffer = new byte[MEGABYTE];
int bufferLength = 0;
while ((bufferLength = inputStream.read(buffer)) > 0) {
fileOutputStream.write(buffer, 0, bufferLength);
}
fileOutputStream.close();
Log.v(TAG, "downloadFile() completed ");
} catch (FileNotFoundException e) {
e.printStackTrace();
Log.e(TAG, "downloadFile() error" + e.getMessage());
Log.e(TAG, "downloadFile() error" + e.getStackTrace());
} catch (MalformedURLException e) {
e.printStackTrace();
Log.e(TAG, "downloadFile() error" + e.getMessage());
Log.e(TAG, "downloadFile() error" + e.getStackTrace());
} catch (IOException e) {
e.printStackTrace();
Log.e(TAG, "downloadFile() error" + e.getMessage());
Log.e(TAG, "downloadFile() error" + e.getStackTrace());
}
}
}
package com.zackdawood.albummanager;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import java.io.File;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private static final String[] PERMISSIONS = {android.Manifest.permission.READ_EXTERNAL_STORAGE,
android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
android.Manifest.permission.INTERNET};
private static boolean hasPermissions(Context context, String... permissions) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
for (String permission : permissions) {
if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
}
return true;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActivityCompat.requestPermissions(MainActivity.this, PERMISSIONS, 112);
Log.v(TAG, "onCreate() Method invoked ");
}
public void write(View view) {
if (!hasPermissions(MainActivity.this, PERMISSIONS)) {
Log.v(TAG, "download() Method DON'T HAVE PERMISSIONS ");
Toast t = Toast.makeText(getApplicationContext(), "You don't have write access !", Toast.LENGTH_LONG);
t.show();
} else {
new DownloadFile().execute("https://upload.wikimedia.org/wikipedia/commons/thumb/c/c2/Adler.jpg/547px-Adler.jpg", "lab_result_2019-04-06.jpg");
}
}
public void read(View view) {
if (!hasPermissions(MainActivity.this, PERMISSIONS)) {
Log.v(TAG, "download() Method DON'T HAVE PERMISSIONS ");
Toast t = Toast.makeText(getApplicationContext(), "You don't have read access !", Toast.LENGTH_LONG);
t.show();
} else {
File folder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
File directory = new File(folder + "/TB System");
File imgFile = new File(directory, "lab_result_2019-04-06.jpg");
Log.v(TAG, "view() Method imgfile " + imgFile.getAbsolutePath());
Uri path = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + ".fileprovider", imgFile);
Log.v(TAG, "view() Method path " + path);
Intent imageIntent = new Intent(Intent.ACTION_VIEW);
imageIntent.setDataAndType(path, "image/*");
imageIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
imageIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
try {
startActivity(imageIntent);
} catch (ActivityNotFoundException e) {
Toast.makeText(MainActivity.this, "No Application available to view Image", Toast.LENGTH_SHORT).show();
}
}
}
public void request(View view) {
ActivityCompat.requestPermissions(MainActivity.this, PERMISSIONS, 112);
}
private class DownloadFile extends AsyncTask<String, Void, Void> {
@Override
protected Void doInBackground(String... strings) {
Log.v(TAG, "doInBackground() Method invoked ");
String fileUrl = strings[0]; // -> http://maven.apache.org/maven-1.x/maven.pdf
String fileName = strings[1]; // -> maven.pdf
File folder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
File directory = new File(folder + "/TB System");
if (!directory.exists()) {
directory.mkdir();
Log.v(TAG, "doInBackground() Method Directory Created ");
}
File imgFile = new File(directory, fileName);
Log.v(TAG, "doInBackground() imgFile invoked " + imgFile.getAbsolutePath());
Log.v(TAG, "doInBackground() imgFile invoked " + imgFile.getAbsoluteFile());
try {
imgFile.createNewFile();
Log.v(TAG, "doInBackground() file created" + imgFile);
} catch (IOException e) {
e.printStackTrace();
Log.e(TAG, "doInBackground() error" + e.getMessage());
Log.e(TAG, "doInBackground() error" + e.getStackTrace());
}
FileDownloader.downloadFile(fileUrl, imgFile);
Log.v(TAG, "doInBackground() file download completed");
return null;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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=".MainActivity">
<LinearLayout
android:layout_width="395dp"
android:layout_height="715dp"
android:orientation="vertical"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp">
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/write_button"
android:onClick="write"/>
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/read_button"
android:onClick="read"/>
<Button
android:id="@+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/request_access"
android:onClick="request"/>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.zackdawood.albummanager">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
<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"
android:networkSecurityConfig="@xml/network_security_config">
<provider
android:name=".GenericFileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/external_files"/>
</provider>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
File imageFile = new File(downloadsFolder, fileName);
imageFile.createNewFile(); // <<<< throws exception
String proposedFileName = "MyDownloadedDocument.jpg";
// DocumentsContract#EXTRA_INITIAL_URI
Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT)
.setType(IMAGE_JPEG_MIME)
.addCategory(Intent.CATEGORY_OPENABLE)
.putExtra(Intent.EXTRA_TITLE, proposedFileName)
.putExtra(DocumentsContract.EXTRA_PROMPT, getString(R.string.label_save_as))
.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION
| Intent.FLAG_GRANT_WRITE_URI_PERMISSION
| Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION)
;
startActivityForResult(intent, folderpickerCode);
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_SAVE_PICTURE) {
if (resultCode == RESULT_OK) {
final Uri outUri = (data == null) ? null : data.getData();
OutputStream outStream = getContentResolver().openOutputStream(outUri, "w");
// write to outStream
outStream.close();
} else finish();
return;
}
super.onActivityResult(requestCode, resultCode, data);
}
<application android:requestLegacyExternalStorage="true">