Android open text file to read after Intent.ACTION_GET_CONTENT - android

The flow is:
The user needs to select text file for use and the default Android explorer whatever pops up.
Then I want to store string containing the file name, to actually open the file for reading.
I want to open that file and rewrite him to new file on app internal storage.
I want to open the new created file from app internal storage.
Bonus 1 - If it's now .txt file but .doc, I want to convert him to regular .txt file in step 3 above of rewriting.
Bonus 2 - How to handle large text files?
Here's the code:
// 1. Start with user action pressing on button to select file
addButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
startActivityForResult(intent, PICKFILE_RESULT_CODE);
}
});
// 2. Come back here
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICKFILE_RESULT_CODE) {
// Get the Uri of the selected file
Uri uri = data.getData();
String filePathName = "WHAT TODO ?";
LaterFunction(filePathName);
}
}
// 3. Later here
public void LaterFunction(String filePathName) {
BufferedReader br;
FileOutputStream os;
try {
br = new BufferedReader(new FileReader("WHAT TODO ?"));
//WHAT TODO ? Is this creates new file with
//the name NewFileName on internal app storage?
os = openFileOutput("newFileName", Context.MODE_PRIVATE);
String line = null;
while ((line = br.readLine()) != null) {
os.write(line.getBytes());
}
br.close();
os.close();
lastFunction("newFileName");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
// 4. And in the end here
public void lastFunction(String newFileName) {
//WHAT TODO? How to read line line the file
//now from internal app storage?
}

Step #1: Delete String filePathName = "WHAT TODO ?";
Step #2: Change LaterFunction(filePathName); to LaterFunction(uri);
Step #3: Change br = new BufferedReader(new FileReader("WHAT TODO ?")); to br = new BufferedReader(new InputStreamReader(getContentResolver().openInputStream(uri));
That is the minimum necessary to address your question.
However, a MIME type of */* will match any type of file, not just text files. Binary files should not be copied using readLine(). If you only want plain text files, use text/plain instead of */*.

For those who struggle to fix that code here is the fixed one
ab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
startActivityForResult(intent, PICKFILE_RESULT_CODE);
}
});
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICKFILE_RESULT_CODE) {
Uri uri = data.getData();
LaterFunction(uri);
}
}
public void LaterFunction(Uri uri) {
BufferedReader br;
FileOutputStream os;
try {
br = new BufferedReader(new InputStreamReader(getContentResolver().openInputStream(uri)));
//WHAT TODO ? Is this creates new file with
//the name NewFileName on internal app storage?
os = openFileOutput("newFileName", Context.MODE_PRIVATE);
String line = null;
while ((line = br.readLine()) != null) {
os.write(line.getBytes());
Log.w("nlllllllllllll",line);
}
br.close();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}

Related

File Object is Not Getting Created

I am giving file browser to user to select file. On onActivityResult I am getting file path as - /file/sdcard/Android/data/com.coca_cola.android.conferenceapp/cache/Conference/export.txt. When I try to create file object on this I am not able to create. When I remove /file/ and create file object on sdcard/Android/data/com.coca_cola.android.conferenceapp/cache/Conference/export.txt its getting created. But I cant hardcode to remove /file/ from filepath as on other device it will be giving some other path. below is the code
private void readContactFromFile(String path) {
StringBuilder text = new StringBuilder();
try {
String st = "/file/sdcard/Android/data/com.coca_cola.android.conferenceapp/cache/Conference/export.txt";
File file = new File(st);
if (file.exists()) {
Log.v("TTT", "file exist");
}
Log.v("TTT", file.toString());
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
text.append(line);
Log.i("Test", "text : " + text + " : end");
text.append('\n');
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
and I am getting path on OnActivityResult
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch (requestCode) {
case REQUEST_PICK_FILE:
Uri uri = data.getData();
String path = data.getData().getPath();
Log.v("PATH", path);
readContactFromFile(path);
break;
}
}
}
And this is how I am calling for opening file browser
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*"); //all files
//intent.setType("text/xml"); //XML file only
intent.addCategory(Intent.CATEGORY_OPENABLE);
try {
startActivityForResult(Intent.createChooser(intent, "Select a File to Upload"), REQUEST_PICK_FILE);
} catch (android.content.ActivityNotFoundException ex) {
// Potentially direct the user to the Market with a Dialog
Toast.makeText(this, "Please install a File Manager.", Toast.LENGTH_SHORT).show();
}
How can I get the perfect path or How can I remove this issue?
Thanks in Advance
Because of the circumstance that the path differs on different devices you should use the frameworks api to retrieve the appropiate path for you.
Have a look at the Environment class.
Instead of using hardcoded string path get the path using Environment class
File file=new File(String.valueOf(Environment.getExternalStorageDirectory())+"YOUR_REQUIRED_FILE_PATH");
It will not defer from device to device.
Hope this helps :)

android Get file name and path from Download directory

I'm trying to copy a file from download folder to another directory.
i used this code to get the file path
int PICKFILE_RESULT_CODE=1;
Intent chooseFile = new Intent(Intent.ACTION_GET_CONTENT);
chooseFile.setType("*/*");
chooseFile = Intent.createChooser(chooseFile, "Choose a file");
startActivityForResult( chooseFile,PICKFILE_RESULT_CODE);
I also used
#Override
public void onActivityResult(int requestCode, int resultCode,
Intent returnIntent) {
// If the selection didn't work
if (resultCode != RESULT_OK) {
// Exit without doing anything else
return;
} else {
returnUri = returnIntent.getData();
String src = returnUri.getPath();
Toast.makeText(this, src, Toast.LENGTH_SHORT).show();
}
}
The code works fine if the file is outside the download directory, when in it the path which i get is in the form of number not the actual name of the file like:
/document/2399
this gives an error of file not found
while the path from the root is:
/storage/emulated/0/myDB.db3
this works fine
pls help me to fix this
The code works fine if the file is outside the download directory
No, it does not. It works fine if the scheme of the Uri happens to be file. Most of the time, it will be content.
I'm trying to copy a file from download folder to another directory.
Use openInputStream() on a ContentResolver to get an InputStream on the content identified by the Uri. This works for both file and content schemes. Then, use standard Java I/O to copy the content from the InputStream to your desired location.
Here is the new code:
int PICKFILE_RESULT_CODE=1;
Intent chooseFile = new Intent(Intent.ACTION_GET_CONTENT);
chooseFile.setType("*/*");
chooseFile = Intent.createChooser(chooseFile, "Choose a file");
startActivityForResult( chooseFile,PICKFILE_RESULT_CODE);
And used:
#Override
public void onActivityResult(int requestCode, int resultCode,
Intent returnIntent) {
InputStream is = null;
// If the selection didn't work
if (resultCode != RESULT_OK) {
// Exit without doing anything else
return;
} else {
// Get the file's content URI from the incoming Intent
Uri returnUri = returnIntent.getData();
try {
is = getContentResolver().openInputStream(returnUri);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
BackUpHelper.importDB(is);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void importDB(InputStream is) throws IOException {
OutputStream os = null;
try {
String currentDBPath = DataBaseHelper2.DB_PATH+DataBaseHelper2.DB_NAME;
File outPut = new File(currentDBPath);
os = new FileOutputStream(outPut);
byte[] buffer = new byte[1024];
while (is.read(buffer) > 0) {
os.write(buffer);
}
Toast.makeText(context, R.string.export_successful,
Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(context, R.string.export_failed, Toast.LENGTH_SHORT)
.show();
}finally {
os.flush();
os.close();
is.close();
}
}

Writer file TXT in Memory Internal android

I need to write text file in the smartphone internal memory, and then need to copy this .txt file to your computer via the USB cable and accessing his memory (This copy process will make manual, need to locate this file and understand which memory location will be recorded).
I am using the code below, which shows no errors when I run, but I do not know if this recording, and I do not know exactly what directory on your smartphone it should be.
This function is the button to call the function salvarInternalStorage
findViewById(R.id.distance_demo_button).setOnClickListener(new View.OnClickListener() {
#Override public void onClick(View v) {
Trilateration tri = new Trilateration(v.getContext());
try {
tri.salvarInternalStorage("Trying to Save This text Example");
} catch (IOException e) {
e.printStackTrace();
}
startListBeaconsActivity(DistanceBeaconActivity.class.getName());
}
});
This function is where you should write to the "File.txt" the text passed by parameter.
public void salvarInternalStorage(String texto) throws IOException{
// Use Activity method to create a file in the writeable directory
FileOutputStream fos = context.openFileOutput("FileTeste.txt", context.MODE_PRIVATE);
// Create buffered writer
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(fos));
writer.write(String.valueOf(texto.getBytes()));
writer.close();
}
With SAF:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
createFile("text/plain","Arquivo.txt");
}
private static final int WRITE_REQUEST_CODE = 43;
private void createFile(String mimeType, String fileName) {
Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
// Filter to only show results that can be "opened", such as
// a file (as opposed to a list of contacts or timezones).
intent.addCategory(Intent.CATEGORY_OPENABLE);
// Create a file with the requested MIME type.
intent.setType(mimeType);
intent.putExtra(Intent.EXTRA_TITLE, fileName);
startActivityForResult(intent, WRITE_REQUEST_CODE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode==WRITE_REQUEST_CODE&&resultCode==RESULT_OK)
alterDocument(data.getData());
}
private void alterDocument(Uri uri) {
try {
ParcelFileDescriptor txt = getContentResolver().
openFileDescriptor(uri, "w");
FileOutputStream fileOutputStream =
new FileOutputStream(txt.getFileDescriptor());
fileOutputStream.write(("Tentando Gravar Esse Texto Exemplo").getBytes());
fileOutputStream.close();
txt.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
Or you can use the OPEN_DOCUMENT_TREE once to choose a directory and after that you have all permissions to save any file under that directory without ask the user. Something like this:
Uri sdCardUri;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
startActivityForResult(intent, 1);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode)
{
case 1:
if (resultCode == Activity.RESULT_OK) {
sdCardUri = data.getData();
getContentResolver().takePersistableUriPermission(sdCardUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
}
createFile();
break;
}
}
private void createFile() {
DocumentFile sdCard=DocumentFile.fromTreeUri(this,sdCardUri);
DocumentFile createFile=sdCard.findFile("teste.txt");
if (createFile==null)
createFile=sdCard.createFile("text","teste.txt");
OutputStream outStream = null;
try {
outStream = getContentResolver().openOutputStream(createFile.getUri());
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
String teste="Isto é um teste";
outStream.write(teste.getBytes());
outStream.flush();
outStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

Android CSV file choose from Storage and get its path

I want to import csv from external storage and then update my database but when i am selecting that csv from downloaded folder FileNotFoundExpception comes. Here is the exception System.err: java.io.FileNotFoundException: /document/primary:Download/GuestCSV.csv: open failed: ENOENT (No such file or directory)
Here is my code. Kindly review my code and help me to find a solution.
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("text/*");
startActivityForResult(Intent.createChooser(intent, "Open CSV"), ACTIVITY_CHOOSE_FILE);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case ACTIVITY_CHOOSE_FILE: {
if (resultCode == RESULT_OK) {
onImport(new File(data.getData().getPath()));
}
}
}
}
public void onImport(File files) {
try {
CSVReader reader = new CSVReader(new FileReader(files));
String[] nextLine;
try {
while ((nextLine = reader.readNext()) != null) {
// nextLine[] is an array of values from the line
String emailID = nextLine[0];
String guestName = nextLine[1];
String guestSource = nextLine[2];
String guestPhone = nextLine[3];
String guestCount = nextLine[4];
String guestCreatedDate = nextLine[5];
String guestModifiedDate = nextLine[6];
GuestDetails guestDetails = new GuestDetails();
guestDetails.setEmail(emailID);
guestDetails.setUsername(guestName);
guestDetails.setPhone(guestPhone);
guestDetails.setSource(guestSource);
guestDetails.setCount(Integer.valueOf(guestCount));
guestDetails.setCreatedDate(guestCreatedDate);
guestDetails.setModifiedDate(guestModifiedDate);
try {
helper.insertGuest(guestDetails);
} catch (SQLiteConstraintException e) {
e.printStackTrace();
}
Toast.makeText(getApplicationContext(), "Data inserted into table...", Toast.LENGTH_SHORT).show();
}
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
i hope this code help you!!
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath()+ "/YourFolder/");
intent.setDataAndType(uri, "text/csv");
startActivity(Intent.createChooser(i, "Open folder"));

Android CSV Path is throwing FileNotFoundExpception

I want to import csv from external storage and then update my database but when I am selecting that csv from downloaded folder FileNotFoundExpception comes. Here is the exception System.err:
java.io.FileNotFoundException: /document/primary:Download/GuestCSV.csv: open failed: ENOENT (No such file or directory)
Here is my code. Kindly review my code and help me to find a solution.
importDatabase.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("text/*");
startActivityForResult(Intent.createChooser(intent, "Open CSV"), ACTIVITY_CHOOSE_FILE);
}
});
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case ACTIVITY_CHOOSE_FILE: {
if (resultCode == RESULT_OK) {
onImport(new File(data.getData().getPath()));
Log.d(TAG, data.getData().getPath());
}
}
}
}
public void onImport(File files) {
try {
String[] nextLine;
try {
CSVReader reader = new CSVReader(new FileReader(files.getAbsolutePath()));
while ((nextLine = reader.readNext()) != null) {
// nextLine[] is an array of values from the line
String emailID = nextLine[0];
String guestName = nextLine[1];
String guestSource = nextLine[2];
String guestPhone = nextLine[3];
String guestCount = nextLine[4];
String guestCreatedDate = nextLine[5];
String guestModifiedDate = nextLine[6];
GuestDetails guestDetails = new GuestDetails();
guestDetails.setEmail(emailID);
guestDetails.setUsername(guestName);
guestDetails.setPhone(guestPhone);
guestDetails.setSource(guestSource);
guestDetails.setCount(Integer.valueOf(guestCount));
guestDetails.setCreatedDate(guestCreatedDate);
guestDetails.setModifiedDate(guestModifiedDate);
try {
helper.insertGuest(guestDetails);
} catch (SQLiteConstraintException e) {
e.printStackTrace();
}
Toast.makeText(getApplicationContext(), "Data inserted into table...", Toast.LENGTH_SHORT).show();
}
} catch (IOException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
There is no guaranty that the URI you are receiving as result is a file (and thus that the path part is an actual filesystem path).
It may be a content: URI, in with case the path only makes sense for the corresponding ContentProvider.
This kind of URI should be read using ContentResolver.openInputStream() or queried via ContentResolver.query().
See A Uri Is Not (Necessarily) a File for more details.

Categories

Resources