Android中截图的路径

16

有没有一种方法可以找到Android用于保存截图的路径?

我能否通过代码获取路径?

4个回答

12

安卓的API没有固定的屏幕截图路径,但是

File pix = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File screenshots = new File(pix, "Screenshots");

可能有效。这就是ICS使用的路径。


2
那么你唯一能做的就是为每个设备创建一个路径列表,并将它们与http://developer.android.com/reference/android/os/Build.html进行匹配。 - zapl
1
在我的三星Galaxy S3上,是“手机\图片\截图”。 - Delcasda
1
@Delcasda 我没有S3来测试,但是Phone\Pictures可能是Environment.DIRECTORY_PICTURES在S3上指向的位置。 - zapl
在运行4.4(奇巧巧克力)的Nexus4上,/Pictures/Screenshots/。 - palaniraja
在三星S6 Edge上,“屏幕截图”目录位于DCIM文件夹中,而不是图片文件夹中... - Rudy Rudolf
显示剩余6条评论

7

“屏幕截图”价值是私有API的一部分。

这里是设置该值的源代码链接。由于该类在我们的应用程序上下文中加载,因此无法访问该字段。我会像@zapi建议的那样硬编码它。


1

这可能对一些人有用...

 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;
}

0

递归在这里是你的朋友。 已在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);
            }
        }
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接