我处于打开sdcard
的状态,需要从其中单击.txt
文件之一,然后单击该文件的文本将其写入textview
。我已经使用此代码尝试了很多,但是当我单击.txt
时,没有任何内容输入textview
。请提出您的建议,我错了?
这里有趣的是,它从onActivityResult
上打印吐司:Toast.makeText(this, "" + path, Toast.LENGTH_SHORT).show();
方法,但不会将内容从.txt打印到TextView。
TextView tv_out;
Button button;
button.setOnClickListener (new View.OnClickListener() {
@Override
public void onClick(View v) {
if (checkPermission()) {
performFileSearch();
} else {
requestPermission2();
}
}
这是从textview将文本保存到.txt的权限
private void requestPermission() {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
Toasty.info(this, "Write External Storage permission allows us to create files.", Toast.LENGTH_LONG).show();
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_REQUEST_CODE);
}
}
这是从textview .txt读取文本到textview的权限
private void requestPermission2() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, PERMISSION_REQUEST_STORAGE);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case 1: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
} else {
Toasty.warning(this, getString(R.string.text), Toast.LENGTH_SHORT).show();
}
return;
}
}
switch (requestCode) {
case PERMISSION_REQUEST_CODE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.e("value", "Permission Granted, Now you can use local drive .");
Toasty.info(this, getString(R.string.save_click), Toast.LENGTH_LONG).show();
} else {
Log.e("value", "Permission Denied, You cannot use local drive .");
}
break;
}
switch (requestCode) {
case PERMISSION_REQUEST_STORAGE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toasty.info(this, "Permission granted!", Toast.LENGTH_LONG).show();
} else {
Toasty.info(this, "Permission Denied", Toast.LENGTH_LONG).show();
finish();
}
}
}
这是阅读文本
private String readText(String input) {
File file = new File(Environment.getExternalStorageDirectory(), input);
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) {
e.printStackTrace();
}
return text.toString();
}
private void performFileSearch() {
Intent chooser = new Intent(Intent.ACTION_OPEN_DOCUMENT);
........
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == READ_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
if (data != null) {
Uri uri = data.getData();
String path = uri.getPath();
path = path.substring(path.indexOf(":") + 1);
if (path.contains("emulated")){
path = path.substring(path.indexOf("0") + 1);
}
Toast.makeText(this, "" + path, Toast.LENGTH_SHORT).show();
tv_out.setText(readText(path));
}
}
}
[请尝试使用以下方法来获取字符串中的文本文件内容。您可以将此字符串设置为textview。
private String readText(Uri fileUri){
BufferedReader reader = null;
StringBuilder stringBuilder = new StringBuilder();
try {
reader = new BufferedReader(new InputStreamReader(getContentResolver().openInputStream(fileUri)));
String line = "";
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null){
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return stringBuilder.toString();
}