I'm getting data from an api and I want to write/save some file with that data. This is my code
try
{
HttpResponse response = httpClient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/incubate_files");
if (!myDir.exists()) myDir.mkdirs();
File file = new File(Environment.getExternalStorageDirectory().getPath()+File.separator+"/incubate_files/", "messageId_"+messageId+"."+ext);
FileOutputStream output = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int bufferLength = 0;
while((bufferLength = content.read(buffer)) != -1)
output.write(buffer, 0, bufferLength);
output.close();
output.flush();
content.close();
}
catch (Exception e)
{
e.printStackTrace();
}
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
There is no exception, only a empty file
Thanks!
UPDATE
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null)
{
builder.append(line);
}
Log.d(app.TAG,"Cadena: "+builder.toString());
InputStream is = new ByteArrayInputStream(builder.toString().getBytes());
I change my InputStream white the content of the api. The api returns a lot of characters. The image actually exists in the server and I can see it.
Now the file is with some bytes but I cant see in my phone
The api reponse is in binary
You need to make use of getInputStream method from connection object and save the data into a File. For example:
InputStream input = connection.getInputStream();
File file = new File("download_directory_path", "file_name");
FileOutputStream output = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int bufferLength = 0;
while((bufferLength = input.read(buffer)) != -1)
output.write(buffer, 0, bufferLength);
and then finally close() your output and input streams.
Once, writing is complete, the file points to the downloaded file.
Related
I'm trying to download a file in my app, but the download times are inconsistently too long.
Sometimes it just downloading it in normal time, but sometimes it just stuck for like 30 seconds or more until it will just fail due to time out error.
Why would that be?
private void Download(String url, String destFileName) throws IOException{
//TODO remove that
// File file = new File(destFileName);
// if(file.exists())
// return;
if(BuildConfig.DEBUG)
Log.d("DownloadFile", "Downloading url: " + url + ", dest: " + destFileName);
HttpGet httppost = null;
AndroidHttpClient client = AndroidHttpClient.newInstance("TvinciAndroid");
FileOutputStream fos = new FileOutputStream(destFileName);
try {
httppost = new HttpGet(url);
HttpResponse res = client.execute(httppost);
if (res.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
Header[] headers = res.getHeaders("Location");
if(headers != null && headers.length != 0) {
url = headers[headers.length - 1].getValue();
Download(url, destFileName);
}
}
HttpEntity responseEntity = res.getEntity();
if (responseEntity != null && responseEntity.getContentLength() > 0) {
InputStream is = AndroidHttpClient.getUngzippedContent(responseEntity);
BufferedReader reader = new BufferedReader(new InputStreamReader(is,Charset.forName("UTF-8")));
StringBuilder bld = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
line += "\n";
fos.write(line.getBytes());
bld.append(line);
}
reader.close();
if(BuildConfig.DEBUG)
Log.d("file content", bld.toString());
bld = null;
}
}
catch(IOException ex){
throw ex;
}
finally {
client.close();
fos.close();
}
}
Any help will be much appreciated
Try specifying the buffer to 8192.
//input stream to read file - with 8k buffer
InputStream input = new BufferedInputStream(url.openStream(), 8192);
I have a sample working code here that can download a file via URL It is different from your implementation, but this might help you.
try {
URL url = new URL(f_url[0]);
URLConnection conection = url.openConnection();
// getting file length
int lengthOfFile = conection.getContentLength();
// input stream to read file - with 8k buffer
InputStream input = new BufferedInputStream(url.openStream(), 8192);
// Output stream to write file
OutputStream output = new FileOutputStream(filePath);
byte data[] = new byte[1024];
long total = 0;
pDialog.setMax(lengthOfFile);
NOTIFICATION_ID = 1+lengthOfFile;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
// After this onProgressUpdate will be called
//publishProgress(""+(int)((total*100)/lenghtOfFile));
publishProgress(""+(int)(total));
notifBuilder.setProgress(lengthOfFile, (int)(total), false)
.setContentText("Download in progress... "+total+"/"+lengthOfFile);
nm.notify(NOTIFICATION_ID, notifBuilder.build());
// writing data to file
output.write(data, 0, count);
}
// flushing output
output.flush();
// closing streams
output.close();
input.close();
} catch (IOException e) {
e.printStackTrace();
Log.e("Error: ", e.getMessage());
return e.getMessage()+" download failed!";
}
I Hope this helps.
So, my app receives a large xml from a soap server. I wish to save this in a file, for later use. I managed to do this, and to read the file. BUT the result (after reading) is a garbled xml! A large portion of text (412 characters) from the latter part of the xml is copied and pasted at the end of my xml, and I can't figure out why this is happening.
I have tried 2 ways to write the file and 2 ways to read the file, no dice! (will post code below) Note: xml is large 5000-20000 characters, so I used methods to keep eclipse from returning out of memory error.
BOTTOM LINE:
-input xml file is correct
-output xml file is incorrect
-tried 2 save methods
-tried 2 read methods
-wtf?!
save code 1:
InputStream is = new ByteArrayInputStream(string.getBytes());
FileOutputStream fos = ctx.openFileOutput(filename, Context.MODE_PRIVATE);
byte[] buffer = new byte[1024];
int length;
while ((length = is.read(buffer))>0){
fos.write(buffer, 0, length);
}
fos.flush();
fos.close();
is.close();
save code 2 :
InputStream is = new ByteArrayInputStream(string.getBytes());
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
FileOutputStream fos = ctx.openFileOutput(filename, Context.MODE_PRIVATE);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
Log.e("stuff is good", "line: "+line);
sb.append(line);
if (sb.toString().length() > 10000) {
fos.write(sb.toString().getBytes());
fos.flush();
sb = new StringBuilder();
}
}
fos.write(sb.toString().getBytes());
fos.flush();
is.close();
fos.close();
read code 1:
FileInputStream fis = openFileInput("caca");
int c;
StringBuilder fileContent = new StringBuilder();
while((c=fis.read())!=-1)
{
fileContent.append((char)c);
}
fis.close();
Log.e("TEST TEST", "XML length = "
+String.valueOf(fileContent.length()) );
Log.e("TEST TEST", "XML = "
+fileContent );
read code 2 :
FileInputStream fis;
fis = openFileInput("caca");
StringBuffer fileContent = new StringBuffer("");
byte[] buffer = new byte[1024];
int i =1;
while (fis.read(buffer) != -1) {
fileContent.append(new String(buffer));
Log.v("TEST"+ String.valueOf(i), new String(buffer) );
i++;
}
Log.e("TEST TEST", "XML length = "
+String.valueOf(fileContent.length()) );
Log.e("TEST TEST", "XML = "
+fileContent );
save to file code :
File myFile = new File("/sdcard/mysdfile.txt");
myFile.createNewFile();
FileOutputStream fOut = new FileOutputStream(myFile);
OutputStreamWriter myOutWriter =
new OutputStreamWriter(fOut);
myOutWriter.append(fileContent);
myOutWriter.close();
fOut.close();
Toast.makeText(getBaseContext(),
"Done writing SD 'mysdfile.txt'",
Toast.LENGTH_SHORT).show();
Sorry for the long post, but after 3 days, I'm at my wits end. Any input would be nice, thank you!
I prefer to use Apache Commons IO for this:
DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url + id);
try {
HttpResponse response = client.execute(httpGet);
InputStream content = response.getEntity().getContent();
StringWriter writer = new StringWriter();
IOUtils.copy(content, writer, "utf-8");
return writer.toString();
} catch (ClientProtocolException e) {
Log.e(tag, "client problem:" + e.getMessage());
throw new RuntimeException("client problem",e);
} catch (IOException e) {
Log.e(tag, "IO problem:" + e.getMessage());
throw new RuntimeException("IO problem",e);
}
Then just write out the string as usual.
ok.... I fixed it , I have no idea why it works.
save code:
public static void Save(String filename, String string,
Context ctx) {
Log.e("stuff is good", "xml length b4 save= "+String.valueOf(string.length()));
try {
FileOutputStream fOut = ctx.openFileOutput(filename, Context.MODE_PRIVATE);
OutputStreamWriter myOutWriter =
new OutputStreamWriter(fOut);
myOutWriter.append(Login.messagesXmlDump);
myOutWriter.close();
fOut.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
read code:
Save("LOL", messagesXmlDump, getApplicationContext());
try {
FileInputStream fis = openFileInput("LOL");
int c;
StringBuilder fileContent = new StringBuilder();
while((c=fis.read())!=-1)
{
fileContent.append((char)c);
}
fis.close();
Managed to write/read a 70k characters long xml. Maybe that recursive method of saving it wasn't the best idea. Think I over-complicated a simple matter.
Sorry for wasting your time :(
I am trying to share files between two Android phones using Socket programming. The problem is right now I have to hard code the file extension on the receiving end. Is there a way that I can automatically determine the extension of the file being received?
Here's my code.
Client Side
socket = new Socket(IP,4445);
File myFile = new File ("/mnt/sdcard/Pictures/A.jpg");
FileInputStream fis = null;
fis = new FileInputStream(myFile);
OutputStream os = null;
os = socket.getOutputStream();
int filesize = (int) myFile.length();
byte [] buffer = new byte [filesize];
int bytesRead =0;
while ((bytesRead = fis.read(buffer)) > 0) {
os.write(buffer, 0, bytesRead);
System.out.println("SO sendFile" + bytesRead);
}
os.flush();
os.close();
fis.close();
socket.close();
}
And the Server side
FileOutputStream fos = null;
File root = Environment.getExternalStorageDirectory();
fos = new FileOutputStream(new File(root,"B.jpg")); //Here I have to hardcode B.jpg with jpg extension.
BufferedOutputStream bos = new BufferedOutputStream(fos);
ServerS = new ServerSocket(4445);
clientSocket = ServerS.accept();
InputStream is = null;
is = clientSocket.getInputStream();
int bytesRead = 0;
int current = 0;
byte [] mybytearray = new byte [329];
do {
bos.write(mybytearray,0,bytesRead);
bytesRead = is.read(mybytearray, 0, mybytearray.length);
} while(bytesRead > -1);
bos.flush();
bos.close();
clientSocket.close();
}
You can find the file extension pretty easily by doing this:
String extension = filename.substring(filename.lastIndexOf('.'));
I am loading a image from the web to the local android phone. The code that I have for writing to a file is as follows
BufferedInputStream bisMBImage=null;
InputStream isImage = null;
URL urlImage = null;
URLConnection urlImageCon = null;
try
{
urlImage = new URL(imageURL); //you can write here any link
urlImageCon = urlImage.openConnection();
isImage = urlImageCon.getInputStream();
bisMBImage = new BufferedInputStream(isImage);
int dotPos = imageURL.lastIndexOf(".");
if (dotPos > 0 )
{
imageExt = imageURL.substring(dotPos,imageURL.length());
}
imageFileName = PATH + "t1" + imageExt;
File file = new File(imageFileName);
if (file.exists())
{
file.delete();
Log.d("FD",imageFileName + " deleted");
}
ByteArrayBuffer baf = new ByteArrayBuffer(255);
Log.d("IMAGEWRITE", "Start to write image to Disk");
int current = 0;
try
{
while ((current = bisMBImage.read()) != -1)
{
baf.append((byte) current);
}
FileOutputStream fos = new FileOutputStream(file);
fos.write(baf.toByteArray());
fos.close();
Log.d("IMAGEWRITE", "Image write to Disk done");
}
catch (IOException e)
{
e.printStackTrace();
}
isImage.close();
}
catch (IOException e)
{
Log.d("DownloadImage", "Error: " + e);
}
finally
{
isImage = null;
urlImageCon = null;
urlImage = null;
}
For some reason the whole writing to a file takes 1 minute. Is there a way I can optimize this ?
Your buffer is very small: 255 bytes. You could make it 1024 times bigger (255 kilobytes). This is an acceptable size and this would certainly speed up the thing.
Also, this is very slow as it reads the bytes one by one:
while ((current = bisMBImage.read()) != -1) {
baf.append((byte) current);
}
You should try using the array version of read() instead: read(byte[] buffer, int offset, int byteCount) with an array as large as what I have described above.
You should use the Android HttpClient for file fetching over the java URL Connection. Also your Buffer is very small.
Try this snipped:
FileOutputStream f = new FileOutputStream(new File(root,"yourfile.dat"));
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet request = new HttpGet(urlString);
HttpResponse response = httpClient.execute(request);
InputStream is = response.getEntity().getContent();
byte[] buffer = new byte[1024];
int len1 = 0;
while ( (len1 = is.read(buffer)) > 0 ) {
f.write(buffer,0, len1);
}
f.close();
I'm porting the application from Symbian/iPhone to Android, part of which is saving some data into file. I used the FileOutputStream to save the file into private folder /data/data/package_name/files:
FileOutputStream fos = iContext.openFileOutput( IDS_LIST_FILE_NAME, Context.MODE_PRIVATE );
fos.write( data.getBytes() );
fos.close();
Now I am looking for a way how to load them. I am using the FileInputStream, but it allows me to read the file byte by byte, which is pretty inefficient:
int ch;
StringBuffer fileContent = new StringBuffer("");
FileInputStream fis = iContext.openFileInput( IDS_LIST_FILE_NAME );
while( (ch = fis.read()) != -1)
fileContent.append((char)ch);
String data = new String(fileContent);
So my question is how to read the file using better way?
Using FileInputStream.read(byte[]) you can read much more efficiently.
In general you don't want to be reading arbitrary-sized files into memory.
Most parsers will take an InputStream. Perhaps you could let us know how you're using the file and we could suggest a better fit.
Here is how you use the byte buffer version of read():
byte[] buffer = new byte[1024];
int length;
while ((length = fis.read(buffer)) != -1) {
fileContent.append(new String(buffer));
}
This isn't really Android-specific but more Java oriented.
If you prefer line-oriented reading instead, you could wrap the FileInputStream in an InputStreamReader which you can then pass to a BufferedReader. The BufferedReader instance has a readLine() method you can use to read line by line.
InputStreamReader in = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(in);
String data = br.readLine()
Alternatively, if you use the Google Guava library you can use the convenience function in ByteStreams:
String data = new String(ByteStreams.toByteArray(fis));
//to write
String data = "Hello World";
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(openFileOutput(FILENAME,
Context.MODE_PRIVATE));
outputStreamWriter.write(data);
outputStreamWriter.close();
//to read
String ret = "";
try {
InputStream inputStream = openFileInput(FILENAME);
if ( inputStream != null ) {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String receiveString = "";
StringBuilder stringBuilder = new StringBuilder();
while ( (receiveString = bufferedReader.readLine()) != null ) {
stringBuilder.append(receiveString);
}
inputStream.close();
ret = stringBuilder.toString();
}
}
catch (FileNotFoundException e) {
Log.e(TAG, "File not found: " + e.toString());
} catch (IOException e) {
Log.e(TAG, "Can not read file: " + e.toString());
}
return ret;
}
context.getFilesDir() returns File object of the directory where context.openFileOutput() did the file writing.