I'm stucking with a really wired problem on Android R. Here is what I did.
- Claim that I would use WRITE_EXTERNAL_STORAGE and READ_EXTERNAL_STORAGE permissions in Manifest.xml.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
- Check and request permissions in my code
private void checkPermission() {
int pid = android.os.Process.myPid();
int read = checkPermission(Manifest.permission.READ_EXTERNAL_STORAGE, pid, 1);
int write = checkPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, pid, 1);
ArrayList<String> permissions = new ArrayList<>();
if (read != PackageManager.PERMISSION_GRANTED)
permissions.add(Manifest.permission.READ_EXTERNAL_STORAGE);
if (write != PackageManager.PERMISSION_GRANTED)
permissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permissions.size() > 0) {
String[] permissionArray = new String[permissions.size()];
permissions.toArray(permissionArray);
requestPermissions(permissionArray,1);
}
}
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
//I ignored the result check here since I definitely know that I clicked on the "Allow" button when
//I was asked to allow the permissions
File directory = new File("/storage/emulated/0/ebook");
File[] files = directory.listFiles();
for(File file : files)
{
try {
Log.i("tagd", "file name: " +file.getCanonicalPath());
} catch (IOException e) {
e.printStackTrace();
}
}
}
OK, here comes the wired thing. From the log, I got only one line:
file name: /storage/emulated/0/ebook/temp
But in the folder "ebook" there are actually 40 items. 39 PDF documents and 1 folder "temp". All PDF documents are missing. Same thing happened in other directories with any type of file.
generic_x86_64_arm64:/sdcard/ebook $ ls
1.pdf dovado\ 4GR\ Reference_manua62X.pdf.pdf
111(1).pdf fit_diff_pages.pdf
2013\ Mar.pdf help.pdf
2017-08-03_SZ_NK.pdf interactiveform_enabled.pdf
223622-100518.pdf invoice\ 2013-1-28.pdf
59df269384986.pdf pass-111111.pdf
732101500002013-07-21_03-55-53-3352579.pdf pdf_commenting_new.pdf
Aeroplane\ -\ bjo.pdf pdfa_pagina_a4_gear_movie.pdf
BoardMeetingMinutes.1024.pdf signature.pdf
CreditCardReckoning201211.pdf temp
DEMO_SV_lowsize.pdf test.pdf
Geografie_\ Gr\ 10\ Portefeuljeboek\ -\ vnull.pdf test.pfx
Geografie_\ Gr\ 10\ Portefeuljeboek\ -\ vnull_unlocked.pdf test1.pdf
Layali_Ashar[1].pdf test28k.pdf
PDF\ markup\ design-Model.pdf test_att.pdf
Praise\ And\ Thanksgiving\ (Morning\ Has\ Broken).pdf visa-china.pdf
Professional_Android_2_Application_Development.pdf 为人父母.pdf
Welcome_to_Radaee_PDF_Master.pdf 张霁\ 简历\ 3\ -\ 副本.pdf
Xaml_Introduction.pdf 比亚迪.pdf
__ViewPoint\ 8000________________\ (2).pdf 美时每客CakeTime移动平台项目开发分析报告.pdf
The issue can only be reproduced on Android Q. The code is really simple and works fine on other Android releases. Any one got any idea for what happened here? Any help would be appreciated, thank you.