How to download dropbox files into my android application programmatically - android

i am trying to download files using getFileStream() in dropbox api but it returns file
information only,please help me to download file data.
here is code..
FileDownload fd = api.getFileStream("dropbox","/public/myfilename.rtf", null);
BufferedReader br = new BufferedReader(new InputStreamReader(fd.is));
BufferedWriter bw = new BufferedWriter(new FileWriter(newfile));
char[] buffer = new char[4096];
int read;
while (true) {
read = br.read(buffer);
if (read <= 0) {
break
}
bw.write(buffer, 0, read);
}

FileDownload fd = api.getFileStream("dropbox",path, null);
File f=new File("/sdcard/test.pdf");
OutputStream out=new FileOutputStream(f);
byte buf[]=new byte[1024];
int len;
while((len=fd.is.read(buf))>0)
out.write(buf,0,len);
out.close();
fd.is.close();
and mention your path like "/public/myfilename"
Here in my code i want to save it as a PDF so
i am creating one pdf and writing data to that.

Related

PDF file write issue - BluetoothSocket OutputStream.write()

I'm facing an issue writing PDF file to BluetoothSocket OutputStream in my android project. Text file write(print) is working fine but when I'm writing PDF file, it prints out some gibberish text not the data inside the PDF file. Here is my code snippet:
File file = new File("/storage/emulated/0/Download/sample.pdf");
inputStream = new FileInputStream(file);
byte[] bytes = new byte[(int) file.length()];
int nextByte;
while ((nextByte = inputStream.read(bytes, 0, bytes.length)) != -1) {
outputStream.write(bytes, 0, nextByte);
outputStream.flush();
}
inputStream.close();
outputStream.close();
Here outputStream is BluetoothSocket OutputStream.(ex: bluetoothSocket.getOutputStream())

Reading InputStream after writing

I am trying to read InputStream after writing output stream to sdcard. I have downloaded file from HttpURLConnection. File is successfuly written to sdcard. But I am trying to read inputstream from same file but contents are not being read properly. On emulator some contents are shown but on actual device contents are not shown. Can you please help what can be the issue? I am posting downloading, writing and reading code.
fileUrl = new URL(filename);
HttpURLConnection connection = (HttpURLConnection)fileUrl.openConnection();
InputStream is = connection.getInputStream();
/**
* Create file with input stream
*/
File downloadFile = new File("/sdcard/", "myFile3.pdf");
downloadFile.createNewFile();
final FileOutputStream outputStream = new FileOutputStream(downloadFile);
int availbleLength = is.available();
byte[] bytes = new byte[availbleLength];
int len1 = 0;
while ((len1 = is.read(bytes)) > 0) {
outputStream.write(bytes, 0, len1);
}
outputStream.flush();
outputStream.close();
File myFile = new File("/sdcard/myFile3.pdf");
InputStream inputStream = new FileInputStream(myFile);
byte[] buffer = new byte[inputStream.available()];
inputStream.read(buffer);
System.out.println("Byte Lenght: " + buffer.length);
inputStream.available() is only an estimate not actual length of the complete input data.
FileInputStream.available()

sending bmp usng sockets

I'm trying to send bmp image using socket. I have such code on android:
ByteArrayOutputStream stream = new ByteArrayOutputStream();
MainActivity.bmp.compress(Bitmap.CompressFormat.JPEG, 20,
stream);
byte[] byteArray = stream.toByteArray();
OutputStream os = echoSocket.getOutputStream();
os.write(byteArray,0,byteArray.length);
os.flush();
and on PC:
String q = SockIn.readLine();
File file = new File("filename.bmp");
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(q);
in bmp file I only get up to 401 bytes, which of course is corrupt bmp image. what am I doing wrong?
MODIFIED
modified PC side, now the code is:
InputStream in_ = clientSocket.getInputStream();
OutputStream out_ = new FileOutputStream("filename.bmp");
final byte[] buffer = new byte[1024];
int read = -1;
int i = 0;
while ((read = in_.read(buffer)) != -1) {
out_.write(buffer, 0, read);
System.out.println(i);
i++;
}
in_.close();
out_.close();
System.out.println("Done");
It never gets to last line( println("Done") ). when I close android program, it gets to last line and bmp opens succesfully
Your reading logic is completely off. You only use a readLine() once and then write that to file. The data that was written to the socket on the device side was binary. That means that trying to read it as if it were textual (as readLine() does) will return meaningless junk. The reason it's usually 401 bytes long is that readLine() will look for the first newline character combination and return everything up to that as a String. This is not what you want.
What you need is a loop that will read from the socket and write into the file as long as there is data in the socket. A standard copy loop should suffice here.
InputStream in = socket.getInputStream();
OutputStream out = new FileOutputStream(...);
final byte[] buffer = new byte[BUFFER_SIZE];
int read = -1;
while ((read = in.read(buffer)) != -1)
out.write(buffer, 0, read);
in.close();
out.close();
Note that the above code isn't tested but something to that effect should do the trick.
Why are you reading a String if you are sending a byte ?
Try those setp one by one only if the previous did not worked.
1. Read() and don't Readline() what you are writing
If you write a byte, read a byte
Byte obj = SockIn.read();
2. Encode your array before sending
Base64.encodeBase64String(byteArray);

How to make a file object of a file in the drawable or raw folder?

I want to bundle an image with my application. I am planing to keep the image in the drawable or the raw folder. I wanted to know how to make a File object of this image?
Something like this:
File file = new File("fileurl");
Thank you.
Can you please try this one ?
try {
File mFile=new File("my file name");
InputStream inputStream = getResources().openRawResource(R.drawable.ic_launcher);
OutputStream out=new FileOutputStream(mFile);
byte buf[]=new byte[1024];
int len;
while((len=inputStream.read(buf))>0)
out.write(buf,0,len);
out.close();
inputStream.close();
}catch (Exception e){
e.printStackTrace();
}
Hope this will help you.
If you put your image resource inside your Raw folder within your workspace, you can access it inside your class by using :
getResources.openRawResources(R.raw.resource_id)
EDIT :
the above code will return an inputStream, to convert it to file, try this one :
inputStream = getResources.openRawResources(R.raw.resource_id)`
// write the inputStream to a FileOutputStream
File file = new File("fileurl");
outputStream = new FileOutputStream(file);
int read = 0;
byte[] bytes = new byte[1024];
while ((read = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
}
outputStream.close();
inputStream.close();

How To Sync SQLite From PC to Android

I have a simple SQLite database on my PC containing one table. I have the same SQLite database in my Android app. The data changes on the PC version of the database from time to time. How do I go about syncing the data from the SQLite database on the PC to the SQLite database on the Android device?
This is a simple solution with sockets:
Server side :
ServerSocket servsock = new ServerSocket(2004);
while (true) {
System.out.println("Waiting...");
Socket sock = servsock.accept();
System.out.println("Accepted connection : " + sock);
BufferedReader input = new BufferedReader(
new InputStreamReader(sock.getInputStream()));
String serverResponse = input.readLine();
// sendfile
File myFile = new File("C://XXXX/XXXX/XXXXX.db");
byte [] mybytearray = new byte [(int)myFile.length()];
FileInputStream fis = new FileInputStream(myFile);
BufferedInputStream bis = new BufferedInputStream(fis);
bis.read(mybytearray,0,mybytearray.length);
OutputStream os = sock.getOutputStream();
System.out.println("Sending...");
os.write(mybytearray,0,mybytearray.length);
os.flush();
sock.close();
}
Client side :
private String serverIpAddress = "xxx.xxx.xxx.xxx";
private static final int REDIRECTED_SERVERPORT = 2004;
InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
socket = new Socket(serverAddr, REDIRECTED_SERVERPORT);
int filesize = 6022386;
int bytesRead;
int current = 0;
byte[] mybytearray = new byte[filesize];
InputStream is = socket.getInputStream();
BufferedReader input = new BufferedReader(new InputStreamReader(is));
FileOutputStream fos = new FileOutputStream(
"/data/data/XXXXX/databases/XXXXX.db");
BufferedOutputStream bos = new BufferedOutputStream(fos);
bytesRead = is.read(mybytearray, 0, mybytearray.length);
current = bytesRead;
do {
bytesRead = is.read(mybytearray, current,
(mybytearray.length - current));
if (bytesRead >= 0)
current += bytesRead;
} while (bytesRead > -1);
bos.write(mybytearray, 0, current);
bos.flush();
bos.close();
socket.close();
This will take quite some work to get it running without problems. Here is how I'd go:
Use a receiver on the Android to detect when USB is plugged. When this is detected, copy your database file over to the SD Card in a folder of your choice (say /data/com.example.package/sync/db.sqlite).
From your PC, detect mass storage devices. Scan the SD Card to check if you find the file at the path above. If not, there is nothing to sync. If yes, then you'll need to perform some DB comparison (google for algorithms and depending on your data you have different possibilities here)
Now you should be having a 3rd database file (the result of the sync between PC and Android databases). This file can be copied to the mass storage (your SD card) at the same place as it was taken.
Use a receiver on the Android to detect when the SDCard is mounted. When this is detected, simply copy the database file from /data/com.example.package/sync/db.sqlite to your application package directory.
We did that for a music application. This is not a trivial task but we managed to get it working quite nicely.

Categories

Resources