简体   繁体   English

在Android中的外部存储和内部存储上存储文件

[英]Storing file on External Storage and Internal Storage in android

I am doing a OCR application in android. 我正在使用android中的OCR应用程序。 For that I need to store the trained data and the images in my application folder. 为此,我需要将经过训练的数据和图像存储在我的应用程序文件夹中。 Until now I was doing it using 直到现在我一直在使用

public static String DATA_PATH = Environment.getExternalStorageDirectory().toString() + "/NewsReader/"

and that works absolutely fine on devices that have external SD cards. 并且在带有外部SD卡的设备上绝对可以正常工作。

But when I try to run the same application on device which either don't have SD cards inserted and work on internal storage or device like Nexus which have only internal storage the app fails. 但是,当我尝试在没有插入SD卡并在内部存储设备上运行的设备上运行同一应用程序时,或者在只有内部存储设备的Nexus等设备上运行该应用程序时,该应用程序将失败。 What alternatives do I have here. 我在这里有什么选择。 I can check the state of the External storage to see if its available or not but If not then what should my app do instead of just crashing. 我可以检查外部存储的状态,以查看其是否可用,如果不可用,那么我的应用程序应该做什么而不是崩溃。 I even tried doing getFilesDir().toString() + "/NewsReader/" but the same results. 我什至尝试执行getFilesDir().toString() + "/NewsReader/"但结果相同。

EDIT 编辑

From some answers that I am getting my above question does not tell what my real problem is Let me clear it more. 从我得到上述问题的一些答案中,并不能得出我真正的问题是什么,请让我进一步解决。

I am doing this : public static String DATA_PATH = Environment.getExternalStorageDirectory().toString() + "/NewsReader/" and it perfectly creates the folder NewsReader in the phone when I run it on the device which have external SD cards. 我正在这样做: public static String DATA_PATH = Environment.getExternalStorageDirectory().toString() + "/NewsReader/" ,当我在具有外部SD卡的设备上运行NewsReader时,它会在手机中完美创建NewsReader文件夹。 But fails to run on the devices with only internal storage. 但是无法在仅具有内部存储的设备上运行。 And to add up currently I am doing all this file creating business in my splsh activity in a thread but earlier when I was doing the same code in my normal Activities OnCreate it used to run on all devices. 现在,总而言之,我正在线程中的splsh活动中完成所有这些文件创建业务,但是在我之前在正常的Activity OnCreate中执行相同的代码时,它曾经在所有设备上运行。

read this I hope help you http://developer.android.com/guide/topics/data/data-storage.html 阅读本文,希望对您有所帮助http://developer.android.com/guide/topics/data/data-storage.html

You can use SharedPreferences: 您可以使用SharedPreferences:

To save data: 要保存数据:

SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
 editor.putString("text", mSaved.getText().toString());
 editor.putInt("selection-start", mSaved.getSelectionStart());
 editor.putInt("selection-end", mSaved.getSelectionEnd());
 editor.commit();

To retrieve data: 要检索数据:

SharedPreferences prefs = getPreferences(MODE_PRIVATE); 
String restoredText = prefs.getString("text", null);
int selectionStart = prefs.getInt("selection-start", -1);
int selectionEnd = prefs.getInt("selection-end", -1);

It is logical that if there is no external storage than you can not store in external storage.You can check whether device has external storage. 逻辑上说,如果没有外部存储,则无法存储在外部存储中。您可以检查设备是否具有外部存储。

If not then you must store data it in internal storage. 如果没有,那么您必须将数据存储在内部存储器中。

`String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        //do your code for external storage
    }else {
        //for internal storage
    }`

You must first check weather the external storage exists or not by the following code: 您必须首先通过以下代码检查天气外部存储是否存在:

/* Checks if external storage is available to at least read */
public boolean isExternalStorageReadable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state) ||
    Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
    return true;
}
return false;}

You can use internal Storage instead. 您可以改为使用内部存储。 Internal Storage can be used by following code: 内部存储可以通过以下代码使用:

File internal_storagefile = new File("/data/YourPackageName/files/abc.jpg");

You can even get internal storage path by: 您甚至可以通过以下方式获取内部存储路径:

File internalPath = File("/data/" + getApplicationContext().getPackageName() + "/");

Source : Android Storage Options 资料来源: Android储存选项

Thank you. 谢谢。

Environment.getExternalStorageState() returns path to internal SD mount point like "/mnt/sdcard"

No, Environment.getExternalStorageDirectory() refers to whatever the device manufacturer considered to be "external storage". 不,Environment.getExternalStorageDirectory()引用设备制造商认为是“外部存储”的任何内容。 On some devices, this is removable media, like an SD card. 在某些设备上,这是可移动媒体,例如SD卡。 On some devices, this is a portion of on-device flash. 在某些设备上,这是设备上闪存的一部分。 Here, "external storage" means "the stuff accessible via USB Mass Storage mode when mounted on a host machine", at least for Android 1.x and 2.x. 此处,“外部存储空间”是指“至少在Android 1.x和2.x上可以通过安装在主机上的USB大容量存储模式访问的内容”。 But the question is about external SD. 但是问题是关于外部SD。 How to get a path like "/mnt/sdcard/external_sd" (it may differ from device to device)? 如何获得“ / mnt / sdcard / external_sd”之类的路径(可能因设备而异)?

Android has no concept of "external SD", aside from external storage, as described above.

If a device manufacturer has elected to have external storage be on-board flash and also has an SD card, you will need to contact that manufacturer to determine whether or not you can use the SD card (not guaranteed) and what the rules are for using it, such as what path to use for it. 如果设备制造商选择外部存储为板载闪存,并且还具有SD卡,则需要与该制造商联系,以确定是否可以使用SD卡(不保证)以及适用的规则。使用它,例如使用什么路径。

https://stackoverflow.com/a/5695129/596555 https://stackoverflow.com/a/5695129/596555
https://stackoverflow.com/a/15131810/596555 https://stackoverflow.com/a/15131810/596555

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM