我試圖讀取我的SDCARD上的CSS文件的內容。使用此路徑檢索SD卡上的文件:「/ document/primary:...」
我選擇從操作的文件:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("text/css");
startActivityForResult(intent, 1);
這裏我搶路徑:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
SharedPreferences sharedPref = PreferenceManager
.getDefaultSharedPreferences(getActivity().getApplicationContext());
SharedPreferences.Editor editor = sharedPref.edit();
if (requestCode == 1) {
if(resultCode == Activity.RESULT_OK){
Log.d("FilePicker", data.getData().toString());
editor.putString("custom_style_file", data.getData().getPath());
editor.commit();
}
if (resultCode == Activity.RESULT_CANCELED) {
//Write your code if there's no result
}
}
}
我得到以下路徑/document/primary:CustomCSS/myCustom.css
然後我嘗試閱讀具有此功能的文件內容:
public static String readFromSDcard(String path) {
File sdcard = Environment.getExternalStorageDirectory();
//Get the text file
File file = new File(sdcard, path);
//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);
}
br.close();
}
catch (IOException e) {
//You'll need to add proper error handling here
}
Log.d("Read from file", text.toString());
return text.toString();
}
該函數什麼也沒有返回,我不知道我在這裏做錯了什麼。
發佈您的logcat –