gpt4 book ai didi

android - 用于可编辑应用程序特定文件的内部或外部存储

转载 作者:行者123 更新时间:2023-11-29 23:53:22 25 4
gpt4 key购买 nike

我正在创建一个应用程序,该应用程序需要一个我称为“conf.cfg”的特定于应用程序的文件。我的应用程序需要读取此文件以创建一些对象等...文件的主体如下所示:

#activation、级别、类型、正则表达式或数组
0, "评论家", 0,"\\d{4}\\w\\d{3}"
1, "评论家", 1, [word1,word2]
1,"次要", 0,"\\d{2}-\\w{3}-\\d{4}\\s?\\/?\\s?\\d{2}:\\d{2}"

通过研究,我发现 android 中有两种类型的存储:

  1. 内部存储:

Internal storage is best when you want to be sure that neither the user nor other apps can access your files.

  1. 外部存储:

External storage is the best place for files that don't require access restrictions and for files that you want to share with other apps or allow the user to access with a computer.

因为我希望用户能够编辑/下载/上传/使用这个文件,外部存储似乎是一个不错的选择。然而在Developper Android他们说:

Caution: The external storage might become unavailable if the user removes the SD card or connects the device to a computer. And the files are still visible to the user and other apps that have the READ_EXTERNAL_STORAGE permission. So if your app's functionality depends on these files or you need to completely restrict access, you should instead write your files to the internal storage.

Caution: Files on external storage are not always accessible, because users can mount the external storage to a computer for use as a storage device. So if you need to store files that are critical to your app's functionality, you should instead store them on internal storage.

因为此文件需要始终可用并且对我的应用的功能至关重要所以...内部存储似乎更好。但我需要用户查看并能够使用该文件。我被困在这里了。

有人知道在何处以及如何放置/创建此文件吗?

编辑:按照@greenapps 的回答

heer 是我写的一段代码。我使用 getExternalFilesDir(null) 命令写入和存储我的文件

    String folderName = "Innovation";
String confFileName = "conf.txt";
String commentSymbol = "#";
String commentLine = commentSymbol + " activation, level, type , regex or array";

File storage = getExternalFilesDir(null);
File folder = new File(storage, folderName);
File conf = new File(folder, confFileName);

Log.d(TAG, "Folder action!");
if (!folder.exists()) {
if (folder.mkdirs()) {
Log.d(TAG, "Created : " + folder.getAbsolutePath());
} else {
Log.e(TAG, "folder not created!");
}
}

Log.d(TAG, "File action!");
if (!conf.exists()) {
try {
Log.d(TAG, "opening...");
FileOutputStream fos = new FileOutputStream(conf);
fos.write(commentLine.getBytes());
fos.close();
Log.d(TAG, "Created : " + conf.getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
}
}

if (conf.exists()) {
Log.d(TAG, "File exist at : " + conf.getAbsolutePath());
}

文件创建完毕,如上次日志所示

Created : /storage/emulated/0/Android/data/com.aralex.innovation/files/Innovation/conf.txt

但是当我用手机的本地文件浏览器应用程序搜索文件时,我找不到它。我可以转到文件夹,但文件夹“Innovation/”被隐藏。

这是个问题,因为我希望文件可见。

手机:三星 s7、s7edge、s9+

Default File Explorer Icon

Default File Explorer Oppened

最佳答案

好吧,我终于自己找到了答案。

关于这篇文章 Android create folders in Internal Memory @prodev 指定 Environment.getExternalStorageDirectory() 是一个好地方,因为文件可以访问 并且:

note that ExternalStorage in Environment.getExternalStorageDirectory() does not necessarily refers to sdcard, it returns phone primary storage memory

它需要权限(仅适用于构建版本 >= M):

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

所以这是一个代码来回答我的问题(它在运行时请求许可):

private ArrayList<Rule> ruleList; 

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

[...]

// Check for the storage permission before accessing the camera. If the
// permission is not granted yet, request permission.
if (hasPermissions(this, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE)
|| Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
ruleList = createRules();
} else {
requestStoragePermission();
}
}

private boolean hasPermissions(Context context, String... permissions) {
if (context != null && permissions != null) {
for (String permission : permissions) {
Log.d(TAG, "Checking permission : " + permission);
if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
Log.w(TAG, "not granted : " + permission);
return false;
} else {
Log.d(TAG, "granted : " + permission);
}
}
}
return true;
}

/**
* Handles the requesting of the storage permission. This includes
* showing a "Snackbar" errorMessage of why the permission is needed then
* sending the request.
*/
private void requestStoragePermission() {
Log.w(TAG, "Storage permission is not granted. Requesting permission");

final String[] permissions = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE};

if (!ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
ActivityCompat.requestPermissions(this, permissions, RC_HANDLE_EXTERNAL_PERM);
return;
}

final Activity thisActivity = this;

View.OnClickListener listener = view -> ActivityCompat.requestPermissions(thisActivity, permissions,
RC_HANDLE_EXTERNAL_PERM);

Snackbar.make(findViewById(android.R.id.content), R.string.permission_storage_rationale,
Snackbar.LENGTH_INDEFINITE)
.setAction(R.string.ok, listener)
.show();
}

@Override
public void onRequestPermissionsResult(int requestCode,
@NonNull String[] permissions,
@NonNull int[] grantResults) {
if (requestCode != RC_HANDLE_EXTERNAL_PERM) {
Log.d(TAG, "Got unexpected permission result: " + requestCode);
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
return;
}

if (grantResults.length != 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED
&& grantResults[1] == PackageManager.PERMISSION_GRANTED) {
Log.d(TAG, "Storage permission granted");
// We have permission
ruleList = createRules();
return;
}

Log.e(TAG, "Permission not granted: results len = " + grantResults.length +
" Result code = " + (grantResults.length > 1 ? grantResults[0] + " " + grantResults[1] : grantResults.length > 0 ? grantResults[0] : "(empty)"));

DialogInterface.OnClickListener listener = (dialog, id) -> finish();

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Assisting Tool")
.setMessage(R.string.no_storage_permission)
.setPositiveButton(R.string.ok, listener)
.show();
}

private ArrayList<Rule> createRules() {
Log.d(TAG, "=========================READING FILE======================");

ArrayList<Rule> ruleList = new ArrayList<>();

String folderName = "Innovation";
String confFileName = "conf.txt";
String commentSymbol = "#";
String commentLine = commentSymbol + " activation, level, type , regex or array";

File storage = Environment.getExternalStorageDirectory();
File folder = new File(storage, folderName);
File conf = new File(folder, confFileName);

Log.d(TAG, "Folder action!");
if (!folder.exists()) {
if (folder.mkdirs()) {
Log.d(TAG, "Created : " + folder.getAbsolutePath());
} else {
Log.e(TAG, "folder not created!");
}
}

Log.d(TAG, "File action!");
if (!conf.exists()) {
try {
Log.d(TAG, "opening...");
FileOutputStream fos = new FileOutputStream(conf);
fos.write(commentLine.getBytes());
fos.close();
Log.d(TAG, "Created : " + conf.getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
}
}

if (conf.exists()) {
Log.d(TAG, "File exist at : " + conf.getAbsolutePath());
} else {
Log.e(TAG, "The file doesn't exist...");
}
}

现在它创建一个特定于应用程序的文件

/storage/emulated/0/Innovation/conf.txt

用户可以访问!

关于android - 用于可编辑应用程序特定文件的内部或外部存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50639259/

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