[英]How can I access(read/write) file in the external sdcard in Android Kitkat?
這是我的源代碼; 我已經在external_SD上安裝了microsdsvc應用。
szSDCardFileName = "/storage/external_SD/Android/data/com.tmnt.microsdsvc/files/AAA.DAT";
if ((fp = open(szSDCardFileName, O_RDWR|O_DIRECT|O_SYNC, S_IRWXU)) == -1) {
if ((fp = open(szSDCardFileName, O_RDWR|O_DIRECT|O_SYNC|O_CREAT, S_IRWXU)) == -1) {
return -1;
}
}
memset(g_buf, 0x00, BLOCK_LENGTH);
if (read(fp, (char *)g_buf, BLOCK_LENGTH) == ERROR) {
__android_log_print(ANDROID_LOG_INFO, "JNI", "<Err>file read error[errno=%d, handle=%d]", errno, fp);
return -1;
}
在Lg G2 Kitkat版本上運行代碼時,open()正常,但是下一個read()失敗,錯誤號為22。
我不知道我錯了!
也許這就是為什么...
KitKat應用程序將不再能夠在帶有內部閃存和可移動/外部SD卡的“二級外部存儲設備”或雙存儲設備上分別創建,修改或刪除文件和文件夾。
“ WRITE_EXTERNAL_STORAGE權限只能授予對設備上主要外部存儲設備的寫訪問權限。除綜合權限允許的應用程序包特定目錄中的應用程序外,不得將應用程序寫入輔助外部存儲設備。”
package com.example.readfilefromexternalresource;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Build;
public class MainActivity extends Activity {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView = (TextView)findViewById(R.id.textView);
String state = Environment.getExternalStorageState();
if (!(state.equals(Environment.MEDIA_MOUNTED))) {
Toast.makeText(this, "There is no any sd card", Toast.LENGTH_LONG).show();
} else {
BufferedReader reader = null;
try {
Toast.makeText(this, "Sd card available", Toast.LENGTH_LONG).show();
File file = Environment.getExternalStorageDirectory();
File file2 = new File(file.getAbsolutePath()+File.separator + "myText.txt");
reader = new BufferedReader(new FileReader(file2));
StringBuilder textBuilder = new StringBuilder();
String line;
while((line = reader.readLine()) != null) {
textBuilder.append(line);
textBuilder.append("\n");
}
textView.setText(textBuilder);
} catch (FileNotFoundException e) {
// TODO: handle exception
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
if(reader != null){
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
}
調用StringBuiler的toString()方法創建您可以使用的String
String state = Environment.getExternalStorageState(); if(!state.equals(Environment.MEDIA_MOUNTED)) { } else { File externalDir = Environment.getExternalStorageDirectory(); File textFile = new File(externalDir.getAbsolutePath() + File.separator + fileName); BufferedReader reader = new BufferedReader(new FileReader(textFile)); StringBuilder textBuilder = new StringBuilder(); String line; while((line = reader.readLine()) != null) { textBuilder.append(line); textBuilder.append("\\n"); } reader.close(); String yourString = textBuilder.toString(); }
它會引發FileNotFound異常。 您可以在寫入文件時執行相同的操作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.