请求运行时权限导致崩溃

时间:2017-10-01 10:37:03

标签: java android fileoutputstream export-to-text

我在这里发帖的主要原因是我目前没有可以运行模拟器的计算机,我一直不得不将APK发送到我的手机进行测试。即使我的手机连接到我的电脑,或者我有第三方模拟器正在运行,它也无法正常工作。由于这个......我没有错误日志。

该应用程序是一个简单的密码管理器,到目前为止所有其他功能都可以使用。我试图添加一个导出函数,我无法实际写任何东西。我已经在线检查了其他问题和各种来源,但我似乎无法弄清楚可能导致它的原因。调用该方法时,就我所知,它根本无法做任何事情。如果我遗漏了某些内容,或者确实存在同样问题的其他问题,我会道歉。我找不到任何遗失的东西。

以下是我使用的方法;

编辑:代码已更新,以反映更好的请求运行时权限的方法,建议使用here.这最终解决了应用程序的问题。

$VALUE[0] = (object)array("id" => "1", "name" => "test1");

$VALUE[1] = (object)array("id" => "3", "name" => "test3");

我的清单:

   //Method017: Exports the account info to a .txt file.
    public void exportData() throws IOException {

        //Opens dialog to request permission.
        ActivityCompat.requestPermissions(Main.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
    }

    //Method to handle result of permission request.
    @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) {

                    //Attempt to write a file to the Download folder.
                    String content = "hello world";
                    File file;
                    FileOutputStream outputStream;
                    try {
                        file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "MyCache");

                        outputStream = new FileOutputStream(file);
                        outputStream.write(content.getBytes());
                        outputStream.close();

                        //According to an online source, this is necessary to make the file viewable on the device.
                        Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                        intent.setData(Uri.fromFile(file));
                        sendBroadcast(intent);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                } else {

                    // permission denied, boo! Disable the
                    // functionality that depends on this permission.
                    Toast.makeText(Main.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
        }
    }

对于缺少错误日志感到抱歉...但如果我有这个,我可能不需要在这里发帖。

1 个答案:

答案 0 :(得分:0)

我已经尝试过你的代码了。

public class SaveFileSampleActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        TextView lblBackground = new TextView(this);
        lblBackground.setBackgroundColor(Color.WHITE);
        setContentView(lblBackground);

        ActivityCompat.requestPermissions(SaveFileSampleActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
    }


    @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) {

                    //Attempt to write a file to the Download folder.
                    String content = "hello world";
                    File file;
                    FileOutputStream outputStream;
                    try {
                        file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "MyCache");

                        outputStream = new FileOutputStream(file);
                        outputStream.write(content.getBytes());
                        outputStream.close();

                        //According to an online source, this is necessary to make the file viewable on the device.
                        Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                        intent.setData(Uri.fromFile(file));
                        sendBroadcast(intent);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } else {

                    // permission denied, boo! Disable the
                    // functionality that depends on this permission.
                    Toast.makeText(SaveFileSampleActivity.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
        }
    }
}

和mainifest

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".SaveFileSampleActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

它的工作和结果: enter image description here

您可以查看您的代码。我希望它可以帮到你!