简体   繁体   English

我无法以编程方式获取Android外部SD卡中的文件

[英]I cannot get the files in Android's external sd card programmatically

When I try the code below: 当我尝试下面的代码时:

File home = new File(Environment.getExternalStorageDirectory().getAbsolutePath());

File [] f = home.listFiles();

I cannot get the files in the sd card, can anyone explain why? 我无法获取SD卡中的文件,任何人都可以解释原因吗? and the path of the external directory is /storage/emulated/0/ 并且外部目录的路径是/storage/emulated/0/

Thank you 谢谢

I think you are using emulator with API-24/25 .. 我认为你正在使用API​​-24/25的模拟器..

Unfortunately with both you have no access to storage. 不幸的是,两者都无法访问存储空间。 I have no idea id that was intended to be change or its just a bug 我不知道想要改变的id或它只是一个bug

You can check if you have access to your storage by adb shell ls 您可以通过adb shell ls检查您是否可以访问您的存储

You must use Emulator with API-23 or less to be able to access storage. 您必须使用API​​-23或更低版本的Emulator才能访问存储。

Include this permission in the manifest file: 在清单文件中包含此权限:

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

For API 23+ you need to request the read/write permissions even if they are already in your manifest. 对于API 23+,您需要请求读/写权限,即使它们已经在您的清单中。

// Storage Permissions
private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {
        Manifest.permission.READ_EXTERNAL_STORAGE,
        Manifest.permission.WRITE_EXTERNAL_STORAGE
};

/**
 * Checks if the app has permission to write to device storage
 *
 * If the app does not has permission then the user will be prompted to grant permissions
 *
 * @param activity
 */
public static void verifyStoragePermissions(Activity activity) {
    // Check if we have write permission
    int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);

    if (permission != PackageManager.PERMISSION_GRANTED) {
        // We don't have permission so prompt the user
        ActivityCompat.requestPermissions(
                activity,
                PERMISSIONS_STORAGE,
                REQUEST_EXTERNAL_STORAGE
        );
    }
}

And handle the responce , some example: 并处理响应,例如:

@Override
public void onRequestPermissionsResult(int requestCode,
        String permissions[], int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {
            // If request is cancelled, the result arrays are empty.
            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.
            }
            return;
        }

        // other 'case' lines to check for other
        // permissions this app might request
    }
}

For official documentation about requesting permissions for API 23+, check https://developer.android.com/training/permissions/requesting.html 有关请求API 23+权限的官方文档,请查看https://developer.android.com/training/permissions/requesting.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM