How to access specific raw data on SD card? - java

I am doing an Android project. In this project i need to read the data of SD card sector by sector.I tried in some ways , Like
RandomAccessFile file = new RandomAccessFile("\\\\.\\PhysicalDrive0","r");
But Filenotfound Exception is coming. I did a similar kind of project in MFC(VC++). There by using Handle, CreateFile nad ReadFile functions i am reading the data sector wise. Any functions are there in java to read the data sector by sector ?

To read the SD card sector by sector, you'll need to run as root. I'd suggest something like the following:
Runtime.getRuntime().exec("su -c dd if=/dev/mmcblk1 of=/dev/stdout");
This should return a Process object whose standard output stream contains the data. The device name of the SD card may vary between different systems, and AFAIK there is no standardized way of finding out what it is, so you will have to experiment a little.
Good luck. :)

Jules is right if you read a block device,
however the internal storage in the new systems is mounted(exec mount on the device to check) on /mnt/shell/emulated(or smth like this) from /dev/fuse(using fuse daemon), which is not a block device but a character device( character special file), meaning that you cannot use random access to get data.
check out this, it might help.

You want to read data from sd card.So do something like this..
File dir = Environment.getExternalStorageDirectory();
File yourFile = new File(dir, "path_of_your_file");
And then you can read the file like..
fileInputStream = new FileInputStream(yourFile);
BufferedReader br = new BufferedReader(new InputStreamReader(fileInputStream));
String readString = new String();
while((readString = br.readLine())!= null){
//readString has your data
}

Related

Writing files to external storage in Android failing

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!

creating text files out of a text file by extracting lines between two character lines

I'm going to read a corpus that is segmented by "##################" and put each segment in a separated text file. My corpus is like below:
#####4327 (Judicial System)
ofobcbyfwquote A right
sd A uh actually i lived over in europe for a couple of years
sd A i lived in germany
sd A and in germany they dont have the jury system
#####4423 (living right)
sv B i think what they need to do is they need to somehow lipsmack take the money out of it
sd B i mean when you have a man thats signed a a a statement saying hes guilty
sd B we have a a family called all day family
I need two text files out of it that named 4327 and 4423 and contain text in between. I know the standard model of reading text files and used it a lot:
FileInputStream fis = new FileInputStream ("C:\\Users\\Desktop\\Train.txt");
BufferedReader br = new BufferedReader (new InputStreamReader(fis,"UTF-8"));
String line="";
while ((br.readLine().startsWith("#")))
{
But do not know how to command it to create text files out of each segment?
You can use regual expression, you can use ApacheCommon for reading files to String:http://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/FileUtils.html#readFileToString(java.io.File).
The code will be something like this:
File fileToParse = new File("/path/to/file");
String complete = FileUtils.readFiletoString(fileToParse);
String[] fragments = complete.split("\\#+[0-9]+");
May be you need to improve regex and save every fragment, you also can use FileUtils.
I hope it help you.

Storing a file as part of android manifest for deployment and then opening it?

Okay I know this should be dead simple but I guess I'm not phrasing my question correctly in my Google & stackoverflow searches.
I have a substantial amount of static data (6 megs) I need to load into my database upon install. Right now I'm fetching a json data file from my web server on first run and populating my database but that can be slow and something could go wrong. I'd prefer to just include the data file in the manifest and then load it on install or first run.
So, where do I put the file, make it so that it ends up on the target device, and then open it?
I've tried putting it in /res/files/ and then doing:
InputStream inputStream = new FileInputStream("/res/files/foo.json");
but of course I'd have been shocked if that had worked.
While I'm at it I should probably use CSV format instead as that would cut down the size but that's another story, I don't seem to have a way to parse it but I do know how to parse JSON data. Sorry I'm a bit new at this. Thanks!
You could store it either in assets or in res\raw.
How to open it from the assets folder:
InputStream is = getAssets().open("foo.json");
How to open it from the res\raw folder:
getResources().openRawResource(R.raw.foo);
I would advise you to use SQLite and/or XML. What #Gabriel suggested will most likely work fine, but loading and processing 6MBs may take some time -a time window of 1 to 5 secs to my experience. Since you downloaded from your webserver I believe your data has some form of structure and in your app you won't need all of the data at once.
Here are some guides/tutorials about SQLite in android, keep in mind that XML is also viable and some will probably advocate XML over SQLite in this case.
http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html
http://www.vogella.com/tutorials/AndroidSQLite/article.html
You can put your JSON file in the raw folder (res/raw) and load it with this code :
InputStream inputStream = getResources().openRawResource(R.raw.foo);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
inputStream.close();
JSONArray jArray = new JSONArray(sb.toString());
Then you can use your knowledge to parse the JSONArray.

Creating a .txt file from scratch

I'm working on a microcontroller and I'm trying to write some data from some sensors into a .txt file on the SDcard and later on place the sd card in a card reader and read the data on the PC.
Does anyone know how to write a .txt file from scratch for a FAT32 file system? I don't have any predefined code/methods/functions to call, I'll need to create the code from nothin.
It's not a question for a specific programming language, that is why I tagged more than one. I can later on convert the code from C or Java to my programming language of choice. But I can't seem to find such low level methods/functions in any type of language :)
Any ideas?
FatFs is quite good, and highly portable. It has support for FAT12, FAT16 and FAT32, long filenames, seeking, reading and writing (most of these things can be switched on and off to change the memory footprint).
If you're really tight on memory there's also Petit FatFs, but it doesn't have write support by default and adding it would take some work.
After mounting the drive you'd simply open a file to create it. For example:
FATFS fatFs;
FIL newFile;
// The drive number may differ
if (f_mount(0, &fatFs) != FR_OK) {
// Something went wrong
}
if (f_open(&newFile, "/test.txt", FA_WRITE | FA_OPEN_ALWAYS) != FR_OK) {
// Something went wrong
}
If you really need to create the file using only your own code you'll have to traverse the FAT, looking for empty space and then creating new LFN entries (where you store the filename) and DIRENTs (which specify the clusters on the disk that will hold the file data).I can't see any reason for doing this except if this is some kind of homework / lab exercise. In any case you should do some reading about the FAT structure first and return with some more specific questions once you've got started.
In JAVA you can do like this
Writer output = null;
String text = "This is test message";
File file = new File("write.txt");
output = new BufferedWriter(new FileWriter(file));
output.write(text);
output.close();
System.out.println("Your file has been written");

Creating and reading a one time created file

I want to store a list of strings in a file.
I need to create it just one time, and after that i will read and write on it programmaticlly.
My question is where in the file system should i create the file (manually) so that it will best for reading and writing ?
Thanks.
You can create your file in your app's directory so no one can access it but your app
getApplicationContext().getFilesDir().getAbsolutePath();
or on sd card
File externalStorage = Environment.getExternalStorageDirectory();
if you want others to access it and, maybe, if your file is very big
If you intent to create your file manually then I think SD card is the only option unless you have a rooted phone or working with the emulator.
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()))
{
//SDcard is there
File f=new File("/sdcard/YOURFILE.txt");
if (!f.exists())
{
//File created only for first time
f.createNewFile();
//create inputstream and write it to your file
OutputStream out=new FileOutputStream(f);
byte buf[]=new byte[1024];
int len;
while((len=inputStream.read(buf))>0)
out.write(buf,0,len);
out.close();
inputStream.close();
System.out.println("\nData Written");
}
else { // read/ write SECOND TIME }
}
It really depends.
The problem with creating the file on the SDCard is that a special permission is required in order to access it. If the app is only for yourself, that's cool. If you want to distribute it through Google MarketPlay (or whatever it is called these days), please know that some people (myself included) tend to look at the permissions and ask "why would an app doing X require permission to do Y?", and sometimes not install the app because of it.
If the manual part is done by the app's user, by all means, store it on the sdcard. It's the only place a standard, none-root user even has access to.
Generally speaking, however, a better place to store data is in /data/data/packagename. See Android's data storage for more details.
Shachar
Add file in assets folder, then it will be clearly after new install

Categories