【问题标题】:Creating text files from android app not visible in LG-D850从 LG-D850 中不可见的 Android 应用程序创建文本文件
【发布时间】:2017-08-16 15:42:25
【问题描述】:

我创建了一个 android 应用程序,它可以创建文本文件并写入设备本地存储。它在所有其他设备上工作正常,我可以在内部存储中看到创建的文件。但是在 LG-D850(更新到 android 版本 7.1)中,应用程序运行良好,但无法在内部存储中创建/显示文件。我已经检查了 storage/emulated/0 但它不存在。

【问题讨论】:

  • “在所有设备上都能正常工作”是什么意思?这些设备可能有另一个安卓版本吗?
  • @ZEESHAN 你有运行时权限还是只有清单中的权限?
  • @cunnniemm 是的,我在清单中有权限。我在三星设备上测试了我的应用程序,它在这些设备上运行良好。
  • @ZEESHAN 好的,在 android 6 及更高版本中有所不同的是,您必须使用运行时权限!检查答案
  • 请提供minimal reproducible example 展示您是如何创建此文件的。另外,请准确解释您是如何“检查 storage/emulated/0”(adb shell ls?还有什么?)

标签: android lg android-internal-storage


【解决方案1】:

解决方案

尝试在您的 on create 中使用此调用此方法并接受权限,您应该能够看到您放置它的文本文件。

public  boolean isStoragePermissionGranted() {
    if (Build.VERSION.SDK_INT >= 23) {
        if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
            == PackageManager.PERMISSION_GRANTED) {
            Log.v(TAG,"Permission is granted");
            return true;
        } else {

            Log.v(TAG,"Permission is revoked");
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
            return false;
        }
    }
    else { //permission is automatically granted on sdk<23 upon installation
        Log.v(TAG,"Permission is granted");
        return true;
    }
}

检查用户点击了什么并在他们选择后决定如何处理应用程序的方法。

@Override
public void onRequestPermissionsResult(int requestCode,
                                   String permissions[], int[] grantResults) {
switch (requestCode) {
    case 1: {

      // 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.
            Toast.makeText(MainActivity.this, "Permission denied to read your External storage", Toast.LENGTH_SHORT).show();
        }
            return;
        }

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

【讨论】:

    猜你喜欢
    • 2015-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-09
    相关资源
    最近更新 更多