File permissions with glass gdk - java

I can create a JSON file in DCIM directory of Glass but the file is not visible when I access it on glass.
Here is the code:
File jsonFile = new File(Environment.getExternalStorageDirectory()
+ File.separator + "DCIM/test/file.json");
//file address
String json = "{\"id\":1}";
//text to be written on the file
FileWriter fw = new FileWriter(jsonFile);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(json);
bw.close();
fw.close();
//writing the file
I can only access the file in DDMS view in Eclipse.
I have added the following permission:
android.permission.WRITE_EXTERNAL_STORAGE

You can view it with DDMS that means the file was created. I think there is no problem with your app.
If you want to open the file, you can pull the file out from your Glass using adb:
adb pull /sdcard/DCIM/test/file.json
And view it with whatever application you want.
Also see http://developer.android.com/tools/help/adb.html
To copy a file or directory (and its sub-directories) from the
emulator or device, use
adb pull <remote> <local>

Why would you create a JSON file in the Digital Camera IMages (DCIM) folder in the first place? Smells like you need to rethink your architecture.

Related

Changing file path to a more common folder

I currently have a simple code in which i save a CSV file in a folder, according to my android studio monitor i am successfully saving that file in a folder i can't access which is "/storage/emulated/0/Download/myfolder/552.csv".
File path =new
File(Environment.getExternalStoragePublicDirectory(Environment. +
DIRECTORY_DOWNLOADS),"myfolder");
System.out.println(path);
if (!path.exists()){
path.mkdir();
}
File mypath=new File(path,(editSesion.getText().toString()+".csv"));
FileWriter mFile = new FileWriter(mypath,true);
CSVWriter writer = new CSVWriter(new FileWriter(mypath));
System.out.println(mypath);
Is there any way to obtain the path to a more commonly accesable folder?
/storage/emulated/0/Download/ is the Downloads folder of your phone storage.
How much common directory are you expecting than this?
Please add / (Forward slash) like this myfolder/
File(Environment.getExternalStoragePublicDirectory(Environment. +
DIRECTORY_DOWNLOADS),"myfolder");
change into
File(Environment.getExternalStoragePublicDirectory(Environment. +
DIRECTORY_DOWNLOADS),"myfolder/");

jsp how to creat a log.txt file in openshift

I need to save something for every request in a log.txt file.
index.jsp in the below do that (which runs in localhost) correctly.
StringBuilder html = new StringBuilder();
ServletContext context = request.getServletContext();
String file = context.getRealPath("/");
html.append(html);
file += "log.txt";
System.out.println(file);
html.append("<br/>\n____________________________________<br/>\n");//file path
html.append(file.toString());//file path
html.append("<br/>\n____________________________________<br/>\n");//file path
FileWriter filewriter = new FileWriter(file, true);
filewriter.write(html.toString());
filewriter.close();
But when i push my project in Openshift it return null as file path:
I create log.txt manually and set 777 for its permission near index.jsp but it is always empty!!
According to the developer center [1] you should use the data folder if you want to have persistent storage of your uploaded/dynamically created files. Or else they will be wiped every time you perform a git push.

Error when writing to Android sdcard

I am trying to open an output stream and write it into my device.
Whenever I try to open an output stream with the path I have on the sd card, I get a
java.io.FileNotFoundException.
But I am able to open the same file path when I try to open it with the device internal storage.
I have given the permission in manifest file to write into sd card.
Below are two file paths
In Device internal storage
/data/data/test.android/cache/9b77fb19-0c1a-441a-8a00-a762898a6648,workspace:/SpacesStore/c3c5f8f8-2dab-443d-8914-5dbfe499054e;1.0
In SD card
/storage/sdcard0/Android/data/test.android/cache/9b77fb19-0c1a-441a-8a00-a762898a6648,workspace:/SpacesStore/c3c5f8f8-2dab-443d-8914-5dbfe499054e;1.0
As you can see, everything after 'cache/' is the same.
Below is the file writing code
String path = downloadedDirectory + File.separator + document.getRepositoryId() + FILENAME_SEPARATOR+ document.getId();
//downloaded directoy is the directory what user selects either sd card or app internal storage. Document repository id and document id i get from my backend.
//should not be of any issue here.
File file= new File(path);
randomAccessFile = new RandomAccessFile(file, "rw");
I am getting exception while trying to create the random access file.
cheers,
Saurav

Editing existing file in Android

I am trying to edit existing file on the device by-First- select the file using selector that retrieve the path of the file like this "mnt/sdcard/file.png". Then I pass it to reader to read the existing file then modify it by by shifting the Ascii of every char. Then Overwrite it again to replace the old one.
I have tested the code on a desktop app on PC files and it Works perfect, but does not work as an Android app. It worked on my device once but did not work again
About what I did:
1)Add writing on External source Permission in the Mainafest file
2)Select the file right and retrieve it path
3)Read the file content true
File file = f;
FileInputStream fin;
fin = new FileInputStream(file);
byte fileContent[] = new byte[(int)file.length()];
fin.read(fileContent);
4)Modify the file bytes
5)Write back (Overwrite) in the original file
FileOutputStream fos = new FileOutputStream(f.getAbsolutePath());
fos.write(enc_msg);
fos.write((byte)seed);
fin.close();
fos.close();
6)Set the file to null again
7)Call finish() in the onClickListner
Thanks in Advance
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://" + Environment.getExternalStorageDirectory())));
However, the device must be disconnected from USB. Otherwise you need to unplug and replug the device to see the changes.
After a lot of work, I have reached the final solution by:
1)Using Common-io library
http://commons.apache.org/proper/commons-io/download_io.cgi
2)Writing this simple line only after import common-io
FileUtils.writeByteArrayToFile(new File(file.getAbsolutePath()), myByteArray, false);
the last attribute (Fasle) for overriding the file .. Append flag

What is my directory when writing files in Android?

FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_WORLD_READABLE);
fos.write(string.getBytes());
fos.close();
When trying to delete one of those files, this is what I use, but it's returning false.
String tag = v.getTag().toString();
File file = new File(System.getProperty("user.dir")+"/"+tag);
String s = new Boolean (file.exists()).toString();
Toast.makeText(getApplicationContext(), s, 1500).show();
file.delete();
How can I overcome this problem?
Use getFileStreamPath(FILENAME) to find your file. From the docs:
Returns the absolute path on the filesystem where a file created with openFileOutput(String, int) is stored.
Your current working directory.
To help diagnose the problem, use file.getAbsolutePath() to see the full path.
It could also be a permissions problem, if you're trying to delete from another application. If so, you may need to change to MODE_WORLD_WRITEABLE (insecure), or restructure your code so the create and delete are called by the same app.
EDIT: That was mostly incorrect. I didn't realize that openFileOutput didn't use the current working directory.
Use same contents as 'FILENAME' variable in your first snippet in the second snippet while trying to delete.
String RootDir = Environment.getExternalStorageDirectory()
+ File.separator + "Video";
File RootFile = new File(RootDir);
RootFile.mkdir();
FileOutputStream f = new FileOutputStream(new File(RootFile, "Sample.mp4"));
i used this code to save the video files to non-default location. Hope this will be useful to you.By default it is storing in sd card
For each application the Android system creates a "data/data/package of the application" directory.
Files are saved in the "files" folder under this directory
to change the default directory the above code will be used
the default working directory can be displayed using fileobject.getAbsolutePath()

Categories