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
Related
As the title points out, I'm having trouble writing files to the external storage. My debug device is a Nexus 5. The thing is, I'm able to read files perfectly from the device (I've been trying with the ones in the Download Folder) but cannot write them. I am aware that I must do this while the device isn't connected to the computer. But it doesn't work either.
In fact, I've tried reading the state of the SD card prior to writing to it (which didn't work, of course). The state showed as "mounted" either when the device was connected to my PC or not. And I compared the state to Environment.MEDIA_MOUNTED_READ_ONLY and Environment.MEDIA_MOUNTED without any success. My device is in none of these states.
One thing which you must know is that my phone doesn't have an external SD card, as it's an internal one. This results in my device having a "/storage/emulated/0/..." directory for the external storage.
I must also point out that I have implemented the following tags in my Android Manifest:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="ANDROID.PERMISSION.WRITE_EXTERNAL_STORAGE"/>
I don't have any clue to what might be happening. Another thing which might help is that I've tried managing files with winrar (for Android) and I've been able to remove files with the device connected to my PC as well as without having it connected. So I don't know what to do.
The code which I'm using to write a file is the following. Bear in mind that it should read an image file (which it does), convert it into a string, convert it back into an image and then save it to the Downloads Folder:
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath() + "/base_image.jpg");
// Reading a Image file from file system
FileInputStream imageInFile = new FileInputStream(file);
byte imageData[] = new byte[(int) file.length()];
imageInFile.read(imageData);
// Converting Image byte array into Base64 String
String imageDataString = encodeImage(imageData);
// Converting a Base64 String into Image byte array
byte[] imageByteArray = decodeImage(imageDataString);
File newFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "converted_image.jpg");
//Write a image byte array into file system
FileOutputStream imageOutFile = new FileOutputStream(newFile);
imageOutFile.write(imageByteArray);
imageInFile.close();
imageOutFile.close();
What should I do?
Just fix ANDROID.PERMISSION.WRITE_EXTERNAL_STORAGE to android.permission.WRITE_EXTERNAL_STORAGE in your uses-permission.
I've encounterd this problem, UPPERCASE in permission is not useful.
FileOutputStream does NOT automatically create a file if it's not exist.
So, you need to check and create if your file doesn't exist.
if(!newFile.exists()) {
newFile.createNewFile();
}
Hope this help!
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.
I am trying to access WhatsApp database folder exists in Environment.getExternalStorageDirectory().getAbsolutePath() + /WhatsApp/Databases location in un-rooted device.
My aim is to collect these files and decrypt them and show each messages to Web portal. Here is the code to collect these files:
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + location;
f = new File(path);
final File file[]=f.listFiles();
final int size = file.length;
Whenever i am running on rooted device, It is working but on some unrooted devices it is throwing nullpointer because it is not able to get the list of all database files.
My question is: Is it possible to ready WhatsApp Database files when device is not root?
or is there any way to do it?
I am not sure the root effects. Have you get the PERMISSION for your app ? if not, you can try to put uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE on your manifest and then test it.
you are able to read database file try below code its worked if device is not rooted.
i have tried these in Samsung Grand 2, HTC
String path = Environment.getExternalStorageDirectory()
+ "/WhatsApp/Databases";
File file = new File(path);
if (file.exists()) {
File[] filenames = file.listFiles();
for (File tmpf : filenames) {
// Do something with the files
if (tmpf.isFile()) {
Log.e("tmpf-------->", "not add---->" + tmpf.getPath());
}
}
} else {
Log.e("folder not found", "folder not found");
}
don't forget to add permission
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
No, You can't get SQLite databse file from an Android device if it's not rooted, since you have no permission to access data folder.
But yes you can get database if you run app on emulator.
I don't know how to retrieve file system of SD card on android. How to do it? It shows like directories.
final String state = Environment.getExternalStorageState();//shows the String name
final File primaryExternalStorage = Environment.getExternalStorageDirectory();//shows directory
The very core of question:
I have an sd card. I don't know file system. How to know it?
I want to get a file which i saved in a specific directory on my phone.
How can I find and get a ref to it so I can do with it something different like uploading to a server?
Thanks in advance.
Shiran
Well where do you store your file?
Store the file in a specific area such as
Environment.getExternalStorageDirectory()
Then when you want to read it, just use the file name used and the path obtained from the above method.
File f = new File("location");
Remember, if you're trying to access the sd card you need to set permissions. Then just handle the "file" in whatever way it is you're trying to do something.
If you're looking to use the SDCard check this post:
Permission to write to the SD card
[Edit - Example]
String filenameToWrite = "Test.jpg"
try{
File f = new File(Environment.getExternalStorageDirectory() + "/Photos/");
if (f.exists()){
File fileToWrite = new File(Environment.getExternalStorageDirectory() + "/Photos/" + filenameToWrite;
// Do something to write file, save bitmap, save byte stream, etc.
}
} catch(IOException e){
Log.e("File", "Could not save file, error occured: " + e.toString());
}
Here's a few getting started tutorials to help you understand how to read and write files in java. This (apart from the 'path') is a java question, not an Android one.
Tutorials:
http://beginwithjava.blogspot.com/2011/04/java-file-save-and-file-load-objects.html
http://www.roseindia.net/java/beginners/java-write-to-file.shtml