示例代码:
try {
File file = new File("/sdcard/myfile.txt");
FileInputStream fis = new FileInputStream(file);
byte[] data = new byte[(int) file.length()];
fis.read(data);
fis.close();
String text = new String(data, "UTF-8"); // 获取文件内容
} catch (IOException e) {
e.printStackTrace();
}
示例代码:
String[] projection = {MediaStore.Images.Media.DATA};
Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
String filePath = cursor.getString(column_index); // 获取文件路径
cursor.close();
}
需要注意的是,在使用ContentProvider访问外部存储的文件时,需要在AndroidManifest.xml中添加以下权限: