私のアプリに問題があります。 SDカードからファイルを読み込もうとしていますが、ファイルが見つかりません。ファイルは私のSDカードのメインフォルダに追加されます。私も新しいフォルダを作成してフォルダからファイルを読み込もうとしましたが、同じ問題があります。 SDカードの他のファイル(アプリケーション目的のために私によって追加されていない)も見えません。私は実際のデバイスサムスンギャラクシーA3で私のアプリをテストしています。あなたは何か考えているのですか、どこに問題があるのですか、あるいは私が間違っていることはありますか?以下は私のコードです:SDカードからファイルを読み込む問題:ファイルが表示されない
マニフェストファイル:
<?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クラス:
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かどうかをチェックすることも方法の下に追加カードは書き込み可能です読める。すべてのメソッドがtrueを返します。
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()を使用すると、SDCARDではなく、自分の電話機の外部カードへのパスを取得しました。
から入手できます。これは私の問題でした。ご協力いただきありがとうございます。 – romek510