I'm trying to get text file from the storage.
Here is my code. It is catching IOexception
, why?
I had created a button which lets me choose the text file but as soon as click on the
file it does give me any path TOAST and it does not send the text to my text view.
Here is my code below:
package com.example.filechooser2;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.BufferUnderflowException;
public class MainActivity extends AppCompatActivity {
private static final int PERMISSION_REQUEST_STORAGE = 1000;
private static final int READ_REQUEST_CODE =400 ;
Button b_load;
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b_load = (Button) findViewById(R.id.b_load);
tv = (TextView) findViewById(R.id.tv);
b_load.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
performFileSearch();
}
});
//requesting permission
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED)
{
requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},PERMISSION_REQUEST_STORAGE);
}
}
This is where the code creates problem i think because it shows the Exception error , please help
// Reading Content
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);
text.append("\n");
} br.close();
}
catch (IOException e){
e.printStackTrace();
Toast.makeText(this,"Messed up",Toast.LENGTH_SHORT).show();
}
return text.toString();
}
// NOW TO GET FROM FILE SYSTEM
private void performFileSearch()
{
Intent i = new Intent (Intent.ACTION_OPEN_DOCUMENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("text/*");
startActivityForResult(i,READ_REQUEST_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent 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);
tv.setText(readText(Path));
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if(requestCode==PERMISSION_REQUEST_STORAGE){
if (grantResults[0]==PackageManager.PERMISSION_GRANTED)
{
Toast.makeText(this,"Permisssion Granted",Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(this,"Permission Not Given",Toast.LENGTH_SHORT).show();
finish();
}
}
}
}