gpt4 book ai didi

android - 访问 getExternalStorageDirectory

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:18:23 25 4
gpt4 key购买 nike

使用一些非常简单的 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() );
}
}

其中 subdir_x 是由我手机上的另一个应用程序创建的。

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

我如何访问这些文件?

最佳答案

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

  1. 确保你的 list 中有这个:

<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.
    }
    }
    }
  2. 然后在您的 Activity 中覆盖此方法:

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

    现在您有了目录,您可以在那里列出文件或创建文件。希望对您有所帮助。

关于android - 访问 getExternalStorageDirectory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34865206/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com