Ad
Unable To Get URI Of A Text File
I am trying to make an app in which I can take text input from the user store it in a text file and then upload that file to firebase so that I can retrieve it later.
The issue is that I am unable to get the correct URI of the file. Please help me get the URI.
Here is the code
public class TextUpload extends AppCompatActivity {
private void writeToFile(String data, Context context) throws FileNotFoundException {
mediaFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
if (!mediaFile.exists()) {
mediaFile.mkdir();
}
byte[] data2 = data.getBytes();
FileOutputStream f = new FileOutputStream(new File(mediaFile, "textfile.txt"));
try {
f.write(data2);
f.flush();
f.close();
} catch (IOException e) {
e.printStackTrace();
}
fileUri = Uri.fromFile(mediaFile);
Log.d("TAHH" , "URI = "+fileUri);
}
}
This is the value I'm getting stored at fileUri
URI = file:///storage/emulated/0
[
Please help me get the URI of the highlighted file.
Ad
Answer
Main problem is that you are reading the path of mediaFile
which is a directory (not the file itself). mediaFile
is the parent directory of the file that you want.
So, change to this:
mediaFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
if (!mediaFile.exists()) {
mediaFile.mkdir();
}
byte[] data2 = data.getBytes();
// Hold the reference to the file that you are writting
File txtFile = new File(mediaFile, "textfile.txt")
FileOutputStream f = new FileOutputStream(txtFile );
try {
f.write(data2);
f.flush();
f.close();
} catch (IOException e) {
e.printStackTrace();
}
// Here, use txtFile instead of mediaFile
fileUri = Uri.fromFile(txtFile);
Log.d("TAHH" , "URI = "+fileUri);
Ad
source: stackoverflow.com
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
Ad