Java Can';t从NotificationPublisher写入外部存储器
我在Android studio中编写来自NotificationPublisher的文件时遇到问题。当我试图编写它时,我得到了一个“Android open failed:EROFS(只读文件系统)”错误 我在清单中有读写外部存储的权限。我在清单中还有NotificationPublisher作为接收者。我已经将其他文件写入了这个特定的目录,并且这些文件已经工作了。我将文件作为额外文件写入的目录传递给intent:Java Can';t从NotificationPublisher写入外部存储器,java,android,file-writing,Java,Android,File Writing,我在Android studio中编写来自NotificationPublisher的文件时遇到问题。当我试图编写它时,我得到了一个“Android open failed:EROFS(只读文件系统)”错误 我在清单中有读写外部存储的权限。我在清单中还有NotificationPublisher作为接收者。我已经将其他文件写入了这个特定的目录,并且这些文件已经工作了。我将文件作为额外文件写入的目录传递给intent: private void scheduleNotification(Notif
private void scheduleNotification(Notification notification, int delay, File directory) {
Intent notificationIntent = new Intent(this, NotificationPublisher.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
notificationIntent.putExtra(NotificationPublisher.NOTIFICATION_ID, 1);
notificationIntent.putExtra(NotificationPublisher.NOTIFICATION, notification);
notificationIntent.putExtra(NotificationPublisher.DIRECTORY, directory);
Toast.makeText(this, directory.toString(), Toast.LENGTH_LONG)
.show();
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
long futureInMillis = SystemClock.elapsedRealtime() + delay;
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, futureInMillis, pendingIntent);
这是正确的目录,我举杯祝贺。在NotificationPublisher中,我解压缩目录并尝试写入现有(但为空)文件:
你们有谁知道问题出在哪里吗?
提前谢谢
目录
的确切值是多少?您从哪里获得的?@commonware在这一行中定义:directory=new File(Environment.getExternalStorageDirectory()+“/postcard”+i);目录存在吗?我看不到您正在对其调用mkdirs()
。另外,不要使用连接:将其替换为新文件(Environment.getExternalStorageDirectory(),“明信片”+i)
@commonware我已经在这个目录上创建了其他文件,没有任何问题,甚至收到了.txt文件本身。该目录存在于我的手机中。我在几行之后调用mkdir()。但是我会改变连接方式,即使这并不重要。
public class NotificationPublisher extends BroadcastReceiver {
public static String DIRECTORY = "directory";
public void onReceive(Context context, Intent intent) {
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = intent.getParcelableExtra(NOTIFICATION);
File directory = intent.getParcelableExtra(DIRECTORY);
final File file = new File(directory, "received.txt");
try
{
file.createNewFile();
FileOutputStream fOut = new FileOutputStream(file);
OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
myOutWriter.append("True");
myOutWriter.close();
fOut.flush();
fOut.close();
}
catch (IOException e)
{
Log.e("Exception", "File write failed: " + e.toString());
}