访问getExternalStorageDirectory

jas*_*n m 7 android

使用一些非常简单的android代码我试图根据以下逻辑访问一个文件夹:

public void try_to_navigate_local_dir(){
    Environment e = new Environment();
    ArrayList<String> filesList = new ArrayList<String>();
    String sd_card = Environment.getExternalStorageDirectory().toString() + "/subdir_x";
    File file = new File( sd_card ) ;
    File list[] = file.listFiles();
    for( int i=0; i< list.length; i++) {
        filesList.add( list[i].getName() );
    }
}
Run Code Online (Sandbox Code Playgroud)

subdir_x由我的手机上其他应用程序创建的.

我可以使用文件管理器查看这些文件夹,但是当我尝试时,我的代码返回null file.listFiles().

我如何访问这些文件?

Guj*_*ana 10

我在Marshmallow(6.0)中尝试过这种逻辑:

  1. 确保你的清单中有这个:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

  1. 当尝试访问外部存储时,请在输入逻辑读取目录之前检查权限:

    public void checkPermissionReadStorage(Activity activity){
             if (ContextCompat.checkSelfPermission(activity,      Manifest.permission.READ_EXTERNAL_STORAGE) !=     PackageManager.PERMISSION_GRANTED) {
    
            // Should we show an explanation?
            if (ActivityCompat.shouldShowRequestPermissionRationale(activity,
                    Manifest.permission.READ_EXTERNAL_STORAGE)) {
    
                // Show an expanation to the user *asynchronously* -- don't block
                // this thread waiting for the user's response! After the user
                // sees the explanation, try again to request the permission.
    
            } else {
    
                // No explanation needed, we can request the permission.
    
                ActivityCompat.requestPermissions(activity,
                        new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                        MY_PERMISSIONS_REQUEST_READ_STORAGE);
    
                // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
                // app-defined int constant. The callback method gets the
                // result of the request.
            }
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  2. 然后在您的活动中覆盖此方法:

    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           String permissions[], int[] grantResults) {
        switch (requestCode) {
            case PermissionManager.MY_PERMISSIONS_REQUEST_READ_STORAGE: {
                //premission to read storage
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
    
                    // permission was granted, yay! Do the
                    // contacts-related task you need to do.
    
                } else {
    
                    // permission denied, boo! Disable the
                    // functionality that depends on this permission.
                    Toast.makeText(SnapSellAddActivity.this, "We Need permission Storage", Toast.LENGTH_SHORT).show();
                }
                return;
            }
    
            // other 'case' lines to check for other
            // permissions this app might request
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  3. 之后你终于可以在android设备上读取外部存储了,我试过这个方法来读取外部存储中的目录:

    public File getPreviewTempExternalDirectory(Context context) {
        File previewDir = new File(context.getExternalFilesDir(null), PLACE YOUT DIR HERE);
        if (!previewDir.exists()) previewDir.mkdir();
        return previewDir;
    }
    
    Run Code Online (Sandbox Code Playgroud)

    现在你有了目录,你可以在那里列出文件或创建文件.希望能帮助到你.


Cor*_*ton 0

确保您AndroidManifest.xml拥有适当的权限:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Run Code Online (Sandbox Code Playgroud)

下一个问题可能是你的文件名。我在我的一个应用程序中使用以下内容并且它有效(注意尾部斜杠和缺少前导斜杠):

directory = new File(Environment.getExternalStorageDirectory(), "Subdirectory/")
final File[] files = directory.listFiles();
Run Code Online (Sandbox Code Playgroud)

Environment.getExternalStorageDirectory()下一个问题可能是给你的路径。较新的设备在内部存储上有一个模拟 SD 卡的分区,并且Environment.getExternalStorageDirectory()可以返回该分区而不是真正的 SD 卡。