How to create a file from google glass application? - android

I have an application that runs on glass. I want my application to create files at run time and be able to write/read data to/from those files. Can anyone show me a way to do this? Does Android's openFileOutput() work in glass?

(In case if anyone else has the same question)
Okay I figured out a way to do this. It looks like Java i/o libraries works fine with android and also for glass. The following works fine in glass.
String filename = "sensorData";
FileOutputStream outputStream;
try {
outputStream = openFileOutput(filename, Context.MODE_APPEND);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter (outputStream);
outputStreamWriter.write(content);
outputStreamWriter.close();
}
catch (Exception e) {
e.printStackTrace();
}
//Printing the file in logcat just to verify the contents
FileInputStream inputStream;
try
{
inputStream = openFileInput(filename);
InputStreamReader inputStreamReader = new InputStreamReader (inputStream);
char[] buffer = new char[content.length()];
inputStreamReader.read(buffer);
String input = buffer.toString();
Log.i(input);
}
catch (Exception e)
{
e.printStackTrace();
}

Related

Android, Why Can't I Read the File I Just Wrote?

I am using the first snippet to write a file.
String fileName = "Test6.txt";
String outputString="Text for File";
try {
FileOutputStream outputStream = openFileOutput(fileName, Context.MODE_PRIVATE);
outputStream.write(outputString.getBytes());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
And the second to read it.
try{
FileInputStream InputStream = openFileInput("Text6.txt");
InputStreamReader inputStreamReader = new InputStreamReader(InputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String lineData = bufferedReader.readLine();
}catch(FileNotFoundException ex)
{
Log.d(TAG, ex.getMessage());
}
catch(IOException ex) {
Log.d(TAG, ex.getMessage());
}
But I can't read it, I get:
java.io.FileNotFoundException: /data/user/0/com.example.android.buildingmarque2/files/Text6.txt (No such file or directory)
I can also get a list of the files and Test6.txt is in the list.
Also, Android Studio Device File Explorer shows it.
It could be a problem with the path.
Device Explorer, "Copy Path" gives me
/data/data/com.example.android.buildingmarque2/files/Test6.txt
But the Log says:
/data/user/0/com.example.android.buildingmarque2/files/Text6.txt
I'm confused?
Typo. One is "Text6" the other is "Test6". Use a constant for both names to avoid this in the future

Why does OutputStream returned by DriveContents.getOutputStream() not act as expected?

I have been playing with Google Drive API for Android and I have stumbled across a bit of a problem. In particula I am interested in App Folder fro saving and synchronizing app data across devices.
I can query if a file with certain filename exists in app folder.
I can get file via
driveFile = metadata.getDriveId().asDriveFile();
I can open file via
driveFile.open( mGoogleApiClient,
DriveFile.MODE_WRITE_ONLY,
new DownloadProgressListener() {...} )
.setResultCallback(...);
And in the later callback I can get OutputStream with:
OutputStream outputStream = dcr.getDriveContents().getOutputStream();
But the problem is that if I try to write to that OutpustStream nothing is written to file. I have used code like that:
OutputStream outputStream = result.getDriveContents().getOutputStream();
OutputStreamWriter writer = new OutputStreamWriter(outputStream);
try {
writer.write("Just some strinh I want to save to Google Drive.");
} catch (IOException e) {
throw new RuntimeException(e);
}
Status status = dcr.getDriveContents().commit(mGoogleApiClient, null).await();
Using this OutputStream nothing is ever written to GoogleDrive. But if I use code from https://developers.google.com/drive/android/files#making_modifications it works as expected.
A copied snippet of that code from google for reference:
try {
ParcelFileDescriptor parcelFileDescriptor = contents.getParcelFileDescriptor();
FileOutputStream fileOutputStream = new FileOutputStream(parcelFileDescriptor
.getFileDescriptor());
Writer writer = new OutputStreamWriter(fileOutputStream);
writer.write("hello world");
} catch (IOException e) {
e.printStackTrace();
}
Why does OutputStream accesible by DriveContents.getOutputStream() not work as expected? Why is it even provided? Or am I missing something?
Version of Google Play Services Library is r29.
Close the OutputStreamWriter - writer.close() to commit the output.

FTPClient in android convert image to bitmap

Help!! i have installed ApacheCommons.net FTPClient. As i need to download a PNG file via ftp. The stream downloads but iam having trouble converting it into a Bitmap so i can save to local storage.
mFTPClient.connect("path");
mFTPClient.login("anonymous","nobody");
mFTPClient.enterLocalPassiveMode();
mFTPClient.changeWorkingDirectory("/fax");
InputStream inStream = mFTPClient.retrieveFileStream("nweimage.PNG");
InputStreamReader isr = new InputStreamReader(inStream, "UTF8");
Been trying to use BitmapFactory to convert it, but just keep getting null return.
Any pointers
Cheers
This is how i got around it. Thanks greenapps for making me think down a different route
mFTPClient.connect("path");
mFTPClient.login("anonymous","nobody");
mFTPClient.enterLocalPassiveMode();
mFTPClient.changeWorkingDirectory("/fax");
mFTPClient.setControlEncoding("UTF-8");
mFTPClient.setFileType(FTPClient.BINARY_FILE_TYPE);
try{
FileOutputStream out = new FileOutputStream(file);
boolean status = mFTPClient.retrieveFile("image.PNG", out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}

Write data continuously to file in external storage - Android App

I am writing the data to the file in an external storage (SD card) on my android. The issue that I am facing is that it just makes one entry and does not go beyond that. I have looked up a number of Q/As here. Could someone please point me in the write direction? TIA!
FileOutputStream outputstream;
try {
file1 = new File(Environment.getExternalStorageDirectory(), "MyData.txt");
outputstream = new FileOutputStream(file1);
OutputStreamWriter oswriter = new OutputStreamWriter(outputstream);
BufferedWriter bwriter = new BufferedWriter (oswriter);
bwriter.append(entry);
bwriter.newLine();
bwriter.close();
outputstream.close();
} catch (Exception e) {
e.printStackTrace();
}
You have to tell FileOutputStream to append the data. By default it just overwrites all contents there. For this you only need to use a different constructor FileOutputStream(File, boolean):
outputstream = new FileOutputStream(file1, true);

write and read file order nature android

developer data-storage-files
String filename = "myfile";
String string = "Hello world!";
FileOutputStream outputStream;
try {
outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
outputStream.write(string.getBytes());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
private String readfile(){
String content="";
try{
InputStream inputStream=openFileInput(myfile);
if(inputStream!=null){
InputStreamReader inputStreamReader=new InputStreamReader(inputStream);
BufferedReader bufferedReader=new BufferedReader(inputStreamReader);
String receiveString="";
StringBuilder stringBuilder=new StringBuilder();
while((receiveString=bufferedReader.readLine())!=null){
stringBuilder.append(receiveString);
}
inputStream.close() ;
content=stringBuilder.toString();
}
}
catch (FileNotFoundException e){
Log.e(FILENAME1, "File Not Found"+e.toString());
}catch (IOException e){
Log.e(FILENAME1,"Cannot read file"+e.toString());
}
return content;
}
I try to write a txt file according to this website, say i want to save the data like this way: ever day i save one number after the date
line 1 2014-12-23 3
line 2 2014-12-24 6
line 3 2014-12-25 10
.
.
.
.
But for every time I write data into this file, it seems that the file is overwritten and every time i read the file, this returns me the most update number. I want to save the date and get the data on one specific line. Any suggestions? Thanks a lot!!
Try changing this
outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
to
outputStream = openFileOutput(filename, Context.MODE_APPEND);
Also you might have to check if files exists. If it doesn't exist use your original statement and if it does exist then use the statement I have mentioned.

Categories

Resources