从 SD 卡读取文件问题:看不到文件
Reading files from SD card issue: no file is visible
我的应用程序有问题。我试图从我的 SD 卡中读取文件,但找不到任何文件。文件被添加到我的 SD 卡中的主文件夹中。我也试过创建一个新文件夹并从文件夹中读取文件,但我遇到了同样的问题。 SD 卡中的其他文件(不是我为应用目的添加的)也不可见。我正在真实设备 Samsung Galaxy A3 上测试我的应用程序。你有什么想法,哪里有问题,或者我做错了什么?下面是我的代码:
清单文件:
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".RecipeActivity"></activity>
</application>
Activity class:
File dir = Environment.getExternalStorageDirectory();
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "receip.txt");
if(file.exists()) // check if file exist
{
//Read text from file
StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
text.append(line);
text.append("\n");
}
}
catch (IOException e) {
//You'll need to add proper error handling here
}
//Set the text
Log.d("File text:", text.toString() );
Toast.makeText(getApplicationContext(),"File Found", Toast.LENGTH_SHORT);
}
else
{
Log.d("Romek file:", "File not found" );
Toast.makeText(getApplicationContext(),"File not Found", Toast.LENGTH_SHORT);
}
我还添加了以下方法来检查我的SD卡是否可写和可读。所有方法 return 正确。
public boolean isExternalStorageWritable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
Log.d("Romek", "External Storage is writable");
return true;
}
Log.d("Romek", "External Storage is not writable");
return false;
}
/* 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)) {
Log.d("Romek", "External Storage is readable");
return true;
}
Log.d("Romek", "External Storage is not readable");
return false;
}
我使用了运行时权限,我可以读取文件,但我还有一个问题。如何获得外部 SDCARD 中文件的正确路径。当我使用 Environment.getExternalStorageDirectory().getAbsolutePath() 时,我在 phone 中收到了外部卡的路径,而不是 SDCARD。
如果您的 phone 有 android 6.0 或更高版本,您必须询问 运行 时间权限。您可以从 https://developer.android.com/training/permissions/requesting.html
获得更多信息
我的应用程序有问题。我试图从我的 SD 卡中读取文件,但找不到任何文件。文件被添加到我的 SD 卡中的主文件夹中。我也试过创建一个新文件夹并从文件夹中读取文件,但我遇到了同样的问题。 SD 卡中的其他文件(不是我为应用目的添加的)也不可见。我正在真实设备 Samsung Galaxy A3 上测试我的应用程序。你有什么想法,哪里有问题,或者我做错了什么?下面是我的代码:
清单文件:
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".RecipeActivity"></activity>
</application>
Activity class:
File dir = Environment.getExternalStorageDirectory();
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "receip.txt");
if(file.exists()) // check if file exist
{
//Read text from file
StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
text.append(line);
text.append("\n");
}
}
catch (IOException e) {
//You'll need to add proper error handling here
}
//Set the text
Log.d("File text:", text.toString() );
Toast.makeText(getApplicationContext(),"File Found", Toast.LENGTH_SHORT);
}
else
{
Log.d("Romek file:", "File not found" );
Toast.makeText(getApplicationContext(),"File not Found", Toast.LENGTH_SHORT);
}
我还添加了以下方法来检查我的SD卡是否可写和可读。所有方法 return 正确。
public boolean isExternalStorageWritable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
Log.d("Romek", "External Storage is writable");
return true;
}
Log.d("Romek", "External Storage is not writable");
return false;
}
/* 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)) {
Log.d("Romek", "External Storage is readable");
return true;
}
Log.d("Romek", "External Storage is not readable");
return false;
}
我使用了运行时权限,我可以读取文件,但我还有一个问题。如何获得外部 SDCARD 中文件的正确路径。当我使用 Environment.getExternalStorageDirectory().getAbsolutePath() 时,我在 phone 中收到了外部卡的路径,而不是 SDCARD。
如果您的 phone 有 android 6.0 或更高版本,您必须询问 运行 时间权限。您可以从 https://developer.android.com/training/permissions/requesting.html
获得更多信息