how do i open a resource file for reading in Android - android

If I have a file in my resources (i.e. in res/raw for example), how do I open it for reading into an input stream? The file might be anything: a text file, a class seriaisation, etc.
On the PC I would use:
MyClass x = null;
FileInputStream fis = null;
ObjectInputStream ois = null;
fis = new FileInputStream("/home/me/Desktop/A.dat");
ois = new ObjectInputStream(fis);
x = (MyClass)ois.readObject();

You can open files from resources in the following way:
BufferedInputStream bis = new BufferedInputStream(getResources().openRawResource(R.raw.A));

Related

How to load a file from assets folder of my android test project?

Is there a way to get the file object of one of the files which are inside the assets folder. I know how to load the inputstream of such a file, but i need to have a file object instead of the inputstream.
This way i load the inputstream
InputStream in2 = getInstrumentation().getContext().getResources().getAssets().open("example.stf2");
But I need the file object, this way the file will not be found
File f = new File("assets/example.stf2");
Found a soltion which works in my case, mabye someone else can use this as well.
Retrieving the file from my android test project to an inputstream
InputStream input = getInstrumentation().getContext().getResources().getAssets().open("example.stf2");
Create a file on the External-Cachedir of the android application under test
File f = new File(getInstrumentation().getTargetContext().getExternalCacheDir() +"/test.txt");
Copy the inputstream to the new file
FileUtils.copyInputStreamToFile(input, f);
Now I can use this file for my further tests
try below code:-
AssetManager am = getAssets();
InputStream inputStream = am.open(file:///android_asset/myfoldername/myfilename);
File file = createFileFromInputStream(inputStream);
private File createFileFromInputStream(InputStream inputStream) {
try{
File f = new File(my_file_name);
OutputStream outputStream = new FileOutputStream(f);
byte buffer[] = new byte[1024];
int length = 0;
while((length=inputStream.read(buffer)) > 0) {
outputStream.write(buffer,0,length);
}
outputStream.close();
inputStream.close();
return f;
}catch (IOException e) {
//Logging exception
}
return null;
}
for more info see below link :-
How to pass a file path which is in assets folder to File(String path)?

Printing PDF with Android

I'm trying to print a PDF from within my android application. But everytime i try to print my printed page contains weird data like:
java.io.FileInputStream#418479b0
I assume that my pdf isn't being rendered in the correct way...
Does anybody now how i can correctly convert my pdf to my outputstream?
I already tried the code from this question(Printing pdf in android), but then i get following output on my printed page:
Filter/FlateDecode/Length 69 >>stream
Can anybody help me? :)
This is my code:
Socket socket = new Socket();
String sIP = "192.168.0.250";
String sPort = "9100";
InetSocketAddress socketAddress = new InetSocketAddress(sIP, Integer.parseInt(sPort));
DataOutputStream outputStream;
try{
//file init
File pdfFile = new File(file);
byte[] byteArray=null;
InputStream inputStream = new FileInputStream(pdfFile);
String inputStreamToString = inputStream.toString();
byteArray = inputStreamToString.getBytes();
inputStream.close();
//Socket init
socket.connect(socketAddress, 3000);
outputStream = new DataOutputStream(socket.getOutputStream());
outputStream.write(byteArray);
outputStream.close();
socket.close();
}catch(Exception e){
e.printStackTrace();
}
You could try something like this to get the byteArray:
//file init
File pdfFile = new File(file);
byte[] byteArray = new byte[(int) pdfFile.length()];
FileInputStream fis = new FileInputStream(pdfFile);
fis.read(byteArray);
fis.close();

Does FileOutputStream is persistent after upgrading my app?

In my app I save some files using FileOutputStream class:
FileOutputStream fos;
fos = openFileOutput("my_file", Context.MODE_PRIVATE);
ObjectOutputStream os = new ObjectOutputStream(fos);
os.writeObject(classToSave);
os.close();
If I upgrade my app anche the I execute:
FileInputStream fis = null;
fis = openFileInput("my_file");
ObjectInputStream is = new ObjectInputStream(fis);
myData = (MyClass) is.readObject();
is.close();
does fis is null or it contains the class that I saved before upgrade?
The file itself will be still there after the upgrade. If you can retrieve the serialised object depends on which changes you made to MyClass in the source code.

Saving a string serialized and buffered android JAVA.IO ObjectInputStream

I am saving out and loading mixed data types. I either have the saving part wrong or the loading part wrong. I am using buffered serial save and load method.
Variable lastFetchDate is defined as a string and initialized as "00/00/00".
It throws an error when reloading the data after it has been saved. What is wrong? I would have thought the opposite to writeBytes would be readBytes for a string.
Saving is as follows:
FileOutputStream fos = new FileOutputStream("userPrefs.dat");
BufferedOutputStream bos = new BufferedOutputStream(fos);
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeBytes(lastFetchDate);
// I close all streams
Loading is as follows:
FileInputStream fis = new FileInputStream("userPrefs.dat");
BufferedInputStream bis = new BufferedInputStream(fis);
ObjectInputStream ois = new ObjectInputStream(bis);
lastFetchDate=(String)ois.readObject(); //<<<<< Error thrown here
// I close all streams
You have written string as byte[] so need to read as byte[]
byte [] bString = new byte[lastFetchDate.length()*2];
ois.readFully(bString, 0, bString.length);
Or if you write as Object using writeObject method then you can read as object,
oos.writeObject(lastFetchDate);

manually read a file in the files directory

Friends, My application has to get current gps position in every seconds and write this data as byte array to a file in files directory. To check the data in that file I tried to open mnually. But i couldn't open. How can i read the file. The file is a text file
private static String readFileAsString(String file) throws IOException {
byte[] bytes = new byte[(int) new File(file).length()];
BufferedInputStream stream = new BufferedInputStream(new FileInputStream(file));
stream.read(bytes);
return new String(bytes);
}

Categories

Resources