【发布时间】:2017-10-01 10:37:02
【问题描述】:
我在这里发帖的主要原因是我目前没有一台可以运行模拟器的计算机,我不得不将 APK 发送到我的手机进行测试。即使我的手机连接到我的电脑,或者我运行了第三方模拟器,它也无法工作。因此...我没有错误日志。
该应用程序是一个简单的密码管理器,到目前为止所有其他功能都可以使用。我试图添加一个导出功能,我无法真正写任何东西。我已经在网上检查了其他问题和各种资源,但我似乎无法弄清楚是什么原因造成的。当调用该方法时,据我所知,它根本没有做任何事情。如果我遗漏了一些东西,或者确实有另一个关于同一问题的问题,我深表歉意。我找不到任何丢失的东西。
这是我正在使用的方法;
编辑: 代码已更新以反映请求运行时权限的更好方法,建议 here. 这最终是修复应用程序的原因。
//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
}
}
还有我的清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.brand.psync">
<application
android:allowBackup="true"
android:icon="@drawable/psynclogo"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:screenOrientation="portrait">
<activity android:name=".Main">
<intent-filter>
<action
android:name="android.intent.action.MAIN"
android:screenOrientation="portrait" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<category
android:name="android.intent.category.LAUNCHER"
android:screenOrientation="portrait" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>
很抱歉缺少错误日志……但如果我有,我可能不需要在这里发帖。
【问题讨论】:
-
您在 build.gradle 文件中提到的目标 sdk 版本是什么。如果它高于 22,你必须在运行时询问 WRITE_EXTERNAL_STORAGE 权限
-
Min 是 15,但 target 是 26,我在 24 号测试它...很有可能,我会调查一下。
-
如果您无法提供堆栈跟踪,请使用 Toast 在 catch 块中显示
e.getMessage()并发布错误消息。 -
您需要请求权限 'WRITE_EXTERNAL_STORAGE' 而不是 'new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}'。
-
@Dungnbhut,尝试进行建议的更改,出现错误。 prntscr.com/gs4p7j
标签: java android fileoutputstream export-to-text