How to read an image from drawable to File object in java? - android

I have an image in the res folder. I want it to be available in the File(java.io.File) object.
I am using:
File f = new File(new URI("drawable/small");
where small is the name of the image.

InputStream ins = getResources().openRawResource(R.drawable.icon);
BufferedReader br = new BufferedReader(new InputStreamReader(ins));
StringBuffer sb;
String line;
while((line = br.readLine()) != null){
sb.append(line);
}
File f = new File(sb.toString());
You can use openRawResource to copy a binary across from your raw resource folder to the device or in this case the File

Is it something like?
File f = new File(Uri.parse("android.resource://com.package.AppName/res/drawable/resource_name"));

Related

How to read from a file in my application on android?

I created a file on my pc, and I want my app to read from it.
How do I read from that file in my app?
Thanks
Put the file in assets/ (e.g., app/src/main/assets/ in a typical Android Studio project). Then use open() on an AssetManager to get an InputStream on that content. You can get an AssetManager from your Activity or other Context by calling getAssets().
Move your file to card and add path instead of "file.txt"
File sdcard = Environment.getExternalStorageDirectory();
//Get the text file
File file = new File(sdcard,"file.txt");
//Read text from file
StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
text.append(line);
text.append('\n');
}
br.close();
}catch (IOException e) {
//You'll need to add proper error handling here
}
//Find the view by its id
TextView tv = (TextView)findViewById(R.id.text_view);
//Set the text
tv.setText(text);
it example best if you want to read text

How do I insert new line in EditText field while reading the text from a file

I am trying to create an android app in which it will take the contents of the file which is in assets, copy the contents to the file in filestorage. Then while displaying, it will read the file in filestorage and will append the lines in edittext.
The problem i am facing is empty lines are not being read.
Following is the code snippet in which i am reading the contents of asset file and copying it in file "current.txt" which will be stored in filestorage.
reader = new BufferedReader(new InputStreamReader(getAssets().open("pravachan.txt")));
ContextWrapper contextWrapper=new ContextWrapper(getApplicationContext());
File directory= contextWrapper.getDir("FileStorage",Context.MODE_PRIVATE);
File myInternalFile=new File(directory,"current.txt");
FileWriter filewriter = new FileWriter(myInternalFile);
BufferedWriter out = new BufferedWriter(filewriter);
String mLine = reader.readLine();
while (mLine != null)
{
//process line
if(mLine.isEmpty())
{
out.write(" ");
String str = System.getProperty("line.separator");
out.write(str);
}
else
out.write(mLine);
mLine = reader.readLine();
}
out.close();
Following is the code snippet in which i am appending the edittext with contents of "current.txt" file
ContextWrapper contextWrapper=new ContextWrapper(getApplicationContext());
File directory= contextWrapper.getDir("FileStorage", Context.MODE_PRIVATE);
File myInternalFile=new File(directory,"current.txt");
FileInputStream fis=new FileInputStream(myInternalFile);
BufferedReader reader=new BufferedReader(new InputStreamReader(fis));
String mLine = reader.readLine();
TextView tv = (TextView)findViewById(R.id.editText);
while(mLine != null)
{
if(mLine.isEmpty()) {
tv.append(" ");
String str = System.getProperty("line.separator");
tv.append(str);
}
else
tv.append(mLine);
mLine = reader.readLine();
}
I don't want empty lines to be skipped.I want the file as it is to be displayed in Edittext. Please Help. Thanks in advance!
I got the answer!
Instead of doing out.write(str) in which str is the line separator, I did out.newline(). It worked!

Android context

I 'm reading on how to write to internal storage, and found the following code methods on the Android Developer forum.
In both the the cases, I do not know how to get/invoke the "context"'s method.
I don't understand what the context variable is and how I can create one.
Say I want app read a file on start up.
What is context and how can I use it to achieve reading from storage.
File file = new File(context.getFilesDir(), filename);
FileInputStream fis = context.openFileInput("Data.dat",
Context.MODE_PRIVATE);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader bufferedReader = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
sb.append(line);
}
Android Developer site's Documentation about File reading from internal storage says...
To read a file from internal storage:
Call openFileInput() and pass it the name of the file to read. This returns a FileInputStream.
Read bytes from the file with read().
Then close the stream with close().
So, your code to read the file named Data.dat should be as below.
FileInputStream fis = context.openFileInput("Data.dat");
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader bufferedReader = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
sb.append(line);
}

Android: How to get a file path from R.raw in res folder?

I have a text file called words.txt in folder raw within android res folder. I don't know how to get its path to make a new file with the given path. I use the code below, but seems it just doesn't work:
File file = new File("res/raw/words.txt").getAbsoluteFile();
String filePath = file.getAbsolutePath();
File wordFile = new File(filePath);
You can read the raw/words.txt as follows:
// The InputStream opens the resourceId and sends it to the buffer
InputStream is = this.getResources().openRawResource(R.raw.words);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String readLine = null;
try {
// While the BufferedReader readLine is not null
while ((readLine = br.readLine()) != null) {
Log.d("TEXT", readLine);
}
// Close the InputStream and BufferedReader
is.close();
br.close();
} catch (IOException e) {
e.printStackTrace();
}
Note: this code must be inside an Activity class, as this.getResources() refers to Context.getResources()
You can't write to your own app's res folder. It is not modifiable.
You can create a file here though:
http://developer.android.com/reference/android/content/Context.html#getFilesDir%28%29
If you want to use words.txt as a baseline, you can read from it and copy it to a modifiable file at runtime.

Problems reading text from file

I'm trying to read some text from a .txt file, here's my code:
String filePath = bundle.getString("filepath");
StringBuilder st = new StringBuilder();
try {
File sd = Environment.getExternalStorageDirectory();
File f = new File(sd, filePath);
FileInputStream fileis = new FileInputStream(f);
BufferedReader buf = new BufferedReader(new InputStreamReader(
fileis));
String line = new String();
while ((line = buf.readLine()) != null) {
st.append(line);
st.append('\n');
}
Log.i("egor", "reading finished, line is " + line);
} catch (FileNotFoundException e) {
Log.i("egor", "file not found");
} catch (IOException e) {
Log.i("egor", "io exception");
}
reader.setText(st.toString());
The text looks like this:
This is a sample text to test
The .txt file is created in Windows notepad.
And here's what I'm getting:
What's wrong with my code? Thanks in advance.
Is the file in utf-8 (unicode) format? For some reason, Notepad always adds a byte-order mark to unicode files, even when the byte-order is irrelevant. When interpreted as ASCII or ANSI, the BOM will be seen as several characters. It's possible this is what's causing your problem.
If so, the solution is to use a more competent text editor than Notepad, or write code that checks for a BOM first in all unicode files.
If none of this makes sense to you, try googling 'unicode' and 'byte-order mark'.
Wrap a FileReader object in the BufferedReader object instead.
http://download.oracle.com/javase/1.4.2/docs/api/java/io/FileReader.html
File sd = Environment.getExternalStorageDirectory();
File file = new File(sd, filePath);
BufferedReader br = new BufferedReader(new FileReader(file));
String line = "";
while ((line = br.readLine()) != null) {
st.append(line);
st.append("\n");
}
br.close();
Try with the folowing code
File f = new File(str);
FileInputStream fis = new FileInputStream(f);
byte[] mydata1 = new byte[(int) f.length()];
fis.read(mydata1);
System.out.println("...data present in 11file..."+new String(mydata1));

Categories

Resources