有没有一种方法可以找到Android用于保存截图的路径?
我能否通过代码获取路径?
安卓的API没有固定的屏幕截图路径,但是
File pix = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File screenshots = new File(pix, "Screenshots");
可能有效。这就是ICS使用的路径。
这可能对一些人有用...
public static File mDir= new File(String.valueOf(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)));
public static File mDirScreenshots = new File(mDir,"Screenshots");
获取屏幕截图的方法:
public static ArrayList<File> getAllScreenshots(File dir){
File listFile[] = dir.listFiles();
if (listFile != null && listFile.length > 0) {
for (int i = 0; i < listFile.length; i++) {
Log.e(YOUR_TAG, "getAllScreenshots: " + i + listFile[i].getName());
mScreenshotsFiles.add(listFile[i]);
}
}
return mScreenshotsFiles;
}
递归在这里是你的朋友。 已在Android 9和12版本上进行了测试。效果很好。
final String findThisDirectory = "screenshots";
final String rootPath = Environment.getExternalStorageDirectory().getPath();
//we do not want to include 'Android' directory because looking inside it
//or some directories inside of it can and will cause crash in some Android versions due to permissions related issues
String ignoreThisDirectory = "android";
void getPathRecursively(String rootPath, String matchThisDirectory, String ignoreThisDirectory){
File[] filesArray = new File(rootPath).listFiles();
if(filesArray.length > 0){
for(File file: filesArray){
if(file.isDirectory()){
if(file.getName().toLowerCase().equals(ignoreThisDirectory)){
continue;
}
else if(file.getName().toLowerCase().equals(matchThisDirectory)){
// here you found the path to 'screenshots' in file object
// get it from the file.getAbsolutePath()
break;
}
getPathRecursively(file.getAbsolutePath(), matchThisDirectory, ignoreThisDirectory);
}
}
}
}
Phone\Pictures
可能是Environment.DIRECTORY_PICTURES
在S3上指向的位置。 - zapl