gpt4 book ai didi

java - 有没有办法在Android(外部存储)上的我的文件中创建一个文件夹并将文件写入该文件夹?

转载 作者:行者123 更新时间:2023-11-30 05:08:17 25 4
gpt4 key购买 nike

我正在编写一个 Android 应用程序(使用 Java),它使用 OCR 将手写内容转换为数字文本。我试图在我的代码中获取 OCR 函数生成的字符串并将其写入文本文件(OCR 部分当前正在工作)。然后我想创建一个文件夹(在手机的外部存储中,例如三星上的我的文件)并将文本文件添加到该文件夹​​,该文件夹仅包含用户创建的文件(用户应该能够访问和共享) ).

我对写入手机的外部存储进行了一些研究(包括其他 StackOverflow 问题),但没有任何教程对我有用。

/* Checks if external storage is available for read and write */
public boolean isExternalStorageWritable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
return true;
}
return false;
}


public File writeFolder ()

{

File file = null;

if (isExternalStorageWritable())

{
// Get the directory for the user's public directory.
file = new File(Environment.getExternalStorageDirectory() + File.separator + "OCR Documents");

}

if (!file.mkdirs())
Log.e(LOG_TAG, "Directory not created");

else
System.out.println(file.getAbsolutePath());

return file;

}

上面的代码是我的,但是经过测试,AbsolutePath 为空。它似乎没有在手机的外部存储上创建文件夹。我将如何处理这个以便创建一个文件夹并且我可以将文件添加到该文件夹​​?

最佳答案

您创建目录的代码没问题。

但您可能会缺少权限,因为较新版本的 Android 需要用户同意才能将文件写入外部存储。

首先,确保您在 Manifest.xml 中具有此权限:

<manifest ...>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
...
</manifest>

之后,由于 WRITE_EXTERNAL_STORAGE 被列为危险权限,如下所示:https://developer.android.com/guide/topics/permissions/overview#normal-dangerous ,您还需要明确请求用户的许可。

最后,请求权限:

// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.READ_CONTACTS)
!= PackageManager.PERMISSION_GRANTED) {

// Permission is not granted
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
Manifest.permission.READ_CONTACTS)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed; request the permission
ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.READ_CONTACTS},
MY_PERMISSIONS_REQUEST_READ_CONTACTS);

// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
} else {
// Permission has already been granted
}

您还需要处理请求的响应:

@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {
// 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.
}
return;
}

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

以上代码复制自:https://developer.android.com/training/permissions/requesting

您应该更彻底地阅读该链接,因为它很好地解释了您需要向用户解释的内容,因为当应用程序请求修改其存储中的文件的权限时,用户通常会非常谨慎。

关于java - 有没有办法在Android(外部存储)上的我的文件中创建一个文件夹并将文件写入该文件夹?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54165640/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com