I wrote program that give a file from asset folder and save it on text file ,my app work on Android 8 but when I install on Android 13 it doesn’t work , here is part of my code
final File FolderProgramSample = new File(Environment.getExternalStorageDirectory(), "Sample");
String assetFileName1 = "keys";
copyFileFromAssetsToStorage(getApplicationContext(), assetFileName1, FolderProgramSample.toString());
public static void copyFileFromAssetsToStorage(Context context, String assetFileName, String destinationPath) {
InputStream inputStream = null;
OutputStream outputStream = null;
try {
// Open the asset file
inputStream = context.getAssets().open(assetFileName);
File destinationDirectory = new File(destinationPath);
if (!destinationDirectory.exists()) {
destinationDirectory.mkdirs();
if (destinationDirectory.mkdirs()) {
Log.d("destinationFile", "Directory created successfully");
} else {
Log.e("destinationFile", "Failed to create directory");
}
}
File destinationFile = new File(destinationDirectory, assetFileName);
outputStream = new FileOutputStream(destinationFile);
// Copy the file
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
}
// Close the streams
outputStream.flush();
outputStream.close();
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}enter code here`
And my manifest file is:
`<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
<uses-permission android:name="android.permission.READ_MEDIA_VIDEO" />
<uses-permission android:name="android.permission.READ_MEDIA_AUDIO" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.RECORD_AUDIO" />
Has anyone encountered this problem? , can you help me?
2
Answers
With API30+ you no longer have access to the entire memory for privacy reasons, see the short overview article Scoped storage. Your string
destinationPath
must point tointernal storage
(like/storage/emulated/0/Android/data/(YOUR_PACKAGE)
) orpublic folders
(like/storage/emulated/0/Download
or/storage/emulated/0/Documents
) as described in the article. The pathEnvironment.getExternalStorageDirectory() = /storage/emulated/0
in your sample doesn’t work anymore.I created simple util for cases like you’re (and my was :’D)
You need to grand MEDIA permissions ofc (SDK 33 )