【问题标题】:Application crashes when i try to access phone's external memory当我尝试访问手机的外部存储器时应用程序崩溃
【发布时间】:2018-12-26 06:13:46
【问题描述】:

我创建了一个简单的应用程序,它尝试访问手机的外部存储文件并将其显示在警报框上,代码如下所示。

btnShowDeviceFolders.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            CharSequence items[];

            final List<String> list = new ArrayList<String>();
            final File path =
                    Environment.getExternalStoragePublicDirectory
                            (
                                    //Environment.DIRECTORY_PICTURES
                                    Environment.DIRECTORY_DCIM
                                    //File.separator +"/"
                            );
            if(!path.exists())
            {
                //path.mkdirs();
             System.out.println("No file found");
            }
            String[] files = path.list();
            if (files.length == 0) {
                //System.out.println("The directory is empty");

            } else {
                for (String aFile : files) {

                    //System.out.println(aFile);
                    list.add(aFile);
                }
            }
            items = files;

            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            builder.setTitle("Select Project Folder");
            builder.setItems(items, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int item) {

                    Toast.makeText(getApplicationContext(),list.get(item),Toast.LENGTH_LONG).show();
                                        }
            });
            builder.show();
        }
    });

但是这段代码在模拟器上运行良好,它列出了存在的文件夹,但是当我构建apk 并在我的手机上测试它时它崩溃了(我正在使用 android Oreo 进行测试)。 Stacktrace 没有给出任何错误 我能做些什么来克服这个问题。

【问题讨论】:

  • 你能给我们stacktrace吗?我确实认为这是因为您忘记了许可。
  • 如果您的意思是读写外部存储的权限,那么我也添加了这些权限android.permission.READ_EXTERNAL_STORAGE@SamiTahri
  • 给出错误的堆栈跟踪(当它在您的设备上崩溃时,而不是在模拟器上)。您是否接受了设备上的权限,因为您可能没有请求它们(在您的 Android 参数中的应用程序下检查)?
  • 在 logcat 的左上角,下拉菜单设置为显示来自模拟器的数据。如果两台设备都在运行,您需要手动选择移动设备。
  • 是的,这是@SamiTahri 的权限问题。我没有授予任何特定权限来读取外部存储。你能告诉我怎么做吗?

标签: java android directory


【解决方案1】:

在 android_manifest.xml 文件中授予外部存储器的权限。

【讨论】:

    【解决方案2】:

    你必须给android.permission.READ_EXTERNAL_STORAGE 运行时间。 here is the tutorial

    【讨论】:

      【解决方案3】:

      您仅在清单中授予权限,而您需要授予运行时权限。

       if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
              if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                  //Show Information about why you need the permission
                  AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                  builder.setTitle("Need Storage Permission");
                  builder.setMessage("This app needs storage permission.");
                  builder.setPositiveButton("Grant", new DialogInterface.OnClickListener() {
                      @Override
                      public void onClick(DialogInterface dialog, int which) {
                          dialog.cancel();
                          ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, EXTERNAL_STORAGE_PERMISSION_CONSTANT);
                      }
                  });
                  builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                      @Override
                      public void onClick(DialogInterface dialog, int which) {
                          dialog.cancel();
                      }
                  });
                  builder.show();
              } else if (permissionStatus.getBoolean(Manifest.permission.WRITE_EXTERNAL_STORAGE,false)) {
                  //Previously Permission Request was cancelled with 'Dont Ask Again',
                  // Redirect to Settings after showing Information about why you need the permission
                  AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                  builder.setTitle("Need Storage Permission");
                  builder.setMessage("This app needs storage permission.");
                  builder.setPositiveButton("Grant", new DialogInterface.OnClickListener() {
                      @Override
                      public void onClick(DialogInterface dialog, int which) {
                          dialog.cancel();
                          sentToSettings = true;
                          Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                          Uri uri = Uri.fromParts("package", getPackageName(), null);
                          intent.setData(uri);
                          startActivityForResult(intent, REQUEST_PERMISSION_SETTING);
                          Toast.makeText(getBaseContext(), "Go to Permissions to Grant Storage", Toast.LENGTH_LONG).show();
                      }
                  });
                  builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                      @Override
                      public void onClick(DialogInterface dialog, int which) {
                          dialog.cancel();
                      }
                  });
                  builder.show();
              } else {
                  //just request the permission
                  ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, EXTERNAL_STORAGE_PERMISSION_CONSTANT);
              }
      
              SharedPreferences.Editor editor = permissionStatus.edit();
              editor.putBoolean(Manifest.permission.WRITE_EXTERNAL_STORAGE,true);
              editor.commit();
      
      
          } else {
              //You already have the permission, just go ahead.
              proceedAfterPermission();
          }
      

      【讨论】:

        猜你喜欢
        • 2018-01-14
        • 1970-01-01
        • 2018-04-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-22
        • 1970-01-01
        相关资源
        最近更新 更多