即使在 android 的文件夹中获得权限和文件后,也没有在文件夹目录中获取文件
Not getting files in folder directory even after getting permission and files in the folder in android
即使在 android.I put
中获得权限后我也没有得到文件目录
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
在清单文件中。我添加了
Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED
.
权限 window 弹窗,并允许应用程序中的权限。我仍然得到 **dir 存在但文件数为零 **。
这是我的完整代码。
public class MainActivity extends AppCompatActivity {
private Handler handler = new Handler();
ArrayList<Object> filesList = new ArrayList<>();
public static final int GALLERY_PERMISSION_CODE = 1002;
private File[] files;
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i("Omm","GalleryPermission before");
GalleryPermission();
Log.i("Omm","GalleryPermission after");
StringBuilder sb = new StringBuilder();
sb.append(Environment.getExternalStorageDirectory());
sb.append(File.separator);
sb.append("ABC/Media/.abc");
Log.i("Omm"," STATUS_DIR before");
File STATUS_DIR = new File(sb.toString());
Log.i("Omm"," STATUS_DIR after "+ sb.toString() );
Log.i("Omm", "dir exists = "+STATUS_DIR.exists());
/****CALLING FUNCTION************/
getData();
/****CALLING FUNCTION************/
}
@RequiresApi(api = Build.VERSION_CODES.M)
private void GalleryPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED
&& checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) {
String[] galleryPermissions = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};
requestPermissions(galleryPermissions, GALLERY_PERMISSION_CODE);
} else {
}
} else {
}
}
private ArrayList<Object> getData() {
StringBuilder sb = new StringBuilder();
sb.append(Environment.getExternalStorageDirectory());
sb.append(File.separator);
sb.append("Abc/Media/efg");//Consider this folder exits and
//contains data.
Log.i("Omm", "ArrayList inovked");
StoryModel f;
String targetPath = sb.toString();
File targetDirector = new File(targetPath);
files = targetDirector.listFiles();
/**************I AM GETTING FILESLENGTH AS ZERO*****BUT I HAVE FILE IN THAT DIRECTORY**************/
Log.i("Omm", "Files = "+files.length);
/**************I AM GETTING FILESLENGTH AS ZERO*****BUT I HAVE FILE IN THAT DIRECTORY**************/
if (files == null) {
Log.i("Omm", "Null");
}
try {
for (int i = 0; i < files.length; i++) {
File file = files[i];
f = new StoryModel();
f.setName("NBM Story Saver: " + (i + 1));
f.setUri(Uri.fromFile(file));
f.setPath(files[i].getAbsolutePath());
f.setFilename(file.getName());
filesList.add(f);
}
} catch (Exception e) {
e.printStackTrace();
}
/**************I AM GETTING FILESLIST AS ZERO*****BUT I HAVE FILE IN THAT DIRECTORY**************/
Log.i("Omm", "dir exists = "+filesList.size());
/**************I AM GETTING FILESLIST AS ZERO*****BUT I HAVE FILE IN THAT DIRECTORY**************/
return filesList;
}
}
我刚刚在 <application>
标签的 AndroidMainfest.xml 内添加了 android:requestLegacyExternalStorage="true"
。
一切正常。
即使在 android.I put
中获得权限后我也没有得到文件目录<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
在清单文件中。我添加了
Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED
.
权限 window 弹窗,并允许应用程序中的权限。我仍然得到 **dir 存在但文件数为零 **。
这是我的完整代码。
public class MainActivity extends AppCompatActivity {
private Handler handler = new Handler();
ArrayList<Object> filesList = new ArrayList<>();
public static final int GALLERY_PERMISSION_CODE = 1002;
private File[] files;
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i("Omm","GalleryPermission before");
GalleryPermission();
Log.i("Omm","GalleryPermission after");
StringBuilder sb = new StringBuilder();
sb.append(Environment.getExternalStorageDirectory());
sb.append(File.separator);
sb.append("ABC/Media/.abc");
Log.i("Omm"," STATUS_DIR before");
File STATUS_DIR = new File(sb.toString());
Log.i("Omm"," STATUS_DIR after "+ sb.toString() );
Log.i("Omm", "dir exists = "+STATUS_DIR.exists());
/****CALLING FUNCTION************/
getData();
/****CALLING FUNCTION************/
}
@RequiresApi(api = Build.VERSION_CODES.M)
private void GalleryPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED
&& checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) {
String[] galleryPermissions = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};
requestPermissions(galleryPermissions, GALLERY_PERMISSION_CODE);
} else {
}
} else {
}
}
private ArrayList<Object> getData() {
StringBuilder sb = new StringBuilder();
sb.append(Environment.getExternalStorageDirectory());
sb.append(File.separator);
sb.append("Abc/Media/efg");//Consider this folder exits and
//contains data.
Log.i("Omm", "ArrayList inovked");
StoryModel f;
String targetPath = sb.toString();
File targetDirector = new File(targetPath);
files = targetDirector.listFiles();
/**************I AM GETTING FILESLENGTH AS ZERO*****BUT I HAVE FILE IN THAT DIRECTORY**************/
Log.i("Omm", "Files = "+files.length);
/**************I AM GETTING FILESLENGTH AS ZERO*****BUT I HAVE FILE IN THAT DIRECTORY**************/
if (files == null) {
Log.i("Omm", "Null");
}
try {
for (int i = 0; i < files.length; i++) {
File file = files[i];
f = new StoryModel();
f.setName("NBM Story Saver: " + (i + 1));
f.setUri(Uri.fromFile(file));
f.setPath(files[i].getAbsolutePath());
f.setFilename(file.getName());
filesList.add(f);
}
} catch (Exception e) {
e.printStackTrace();
}
/**************I AM GETTING FILESLIST AS ZERO*****BUT I HAVE FILE IN THAT DIRECTORY**************/
Log.i("Omm", "dir exists = "+filesList.size());
/**************I AM GETTING FILESLIST AS ZERO*****BUT I HAVE FILE IN THAT DIRECTORY**************/
return filesList;
}
}
我刚刚在 <application>
标签的 AndroidMainfest.xml 内添加了 android:requestLegacyExternalStorage="true"
。
一切正常。