I was implement download apk from my own server, and install that, when i download with wifi its working fine, but when the movile is conected to internet with movile data the transfer is very low, and the apk not donwload, dont give me an error.
The code that i implemented is:
//Download the actual apk
InputStream inputStream = consumirWebService.ventaMovilUpdate("1", direccionUrlBase);
String pathApk = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)+Dictionary.APK_NAME;
OutputStream os = new FileOutputStream(pathApk);
byte[] buffer = new byte[1024];
int bytesRead;
while((bytesRead = inputStream.read(buffer)) !=-1){
os.write(buffer, 0, bytesRead);
}
inputStream.close();
os.flush();
os.close();
//Install the new apk
PackageInfo info = IMAdministrador.this.getPackageManager().getPackageArchiveInfo(pathApk, PackageManager.GET_META_DATA);
File file = new File(pathApk);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.putExtra(Intent.ACTION_PACKAGE_REPLACED, info.packageName);
intent.setDataAndType(Uri.fromFile(file),"application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
if(info.packageName != null){
IMAdministrador.INSTALL_APK_INFO.put(info.packageName, file);
}
I am thinking on download the apk by parts compressed with rest webservices. The weight of apk is 5MB, download each of a parts some thing like 1Mb by part, and when i have the five parts, uncompress and install. But really i dont have idea how can I do that.
Or if you have any best idea, I am very appreciate for your help.
Thanks in advance.
Related
I have an apk to download from a website. It works fine in Android 4, but when I downloaded it with an Android 5.0 device, I got the typical error:
Parse Error
There was a problem while parsing the package
Can anyone shed a bit of light on what is happening?
My server code to get the apk file (I don't want to place the apk file directly to download in website):
InputStream is = ApkDownloadController.class.getResourceAsStream("/apk/fileName.apk");
try {
response.addHeader("Content-Description", "File Transfer");
response.addHeader("Content-Type", " application/vnd.android.package-archive");
response.addHeader("Content-Disposition", "attachment; filename=apkName.apk");
response.addHeader("Content-Transfer-Encoding", "binary");
ServletOutputStream out = response.getOutputStream();
byte[] outputByte = new byte[4096];
// copy binary contect to output stream
while (is.read(outputByte, 0, 4096) != -1) {
out.write(outputByte, 0, 4096);
}
is.close();
out.flush();
out.close();
Thanks all!
I'm writing a process that downloads/ copies a file attached to Gmail on to the SD card that my application can then read.
When a user clicks on the attachment my activity is fired and I use the following code to save to local storage;
InputStream in = getContentResolver().openInputStream( intent.getData() );
String ext = intent.getType().equals("text/xml") ? ".xml" : ".gpkg";
localFile = new File( TILE_DIRECTORY, "tmp/"+intent.getDataString().hashCode()+ext);
// If we haven't already cached the file, go get it..
if (!localFile.exists()) {
localFile.getParentFile().mkdirs();
FileIO.streamCopy(in, new BufferedOutputStream(new FileOutputStream(localFile)) );
}
The FileIO.streamCopy is simply;
public static void streamCopy(InputStream in, OutputStream out) throws IOException{
byte[] b = new byte[BUFFER];
int read;
while ((read = in.read(b)) != -1) {
out.write(b, 0, read);
}
out.close();
in.close();
}
This all works fine on a small file, but with a 6Mb attachment only 12Kb is ever getting written. I'm not even getting an error, the process just runs through very quickly and i'm left with a corrupt file.
This process is run in its own thread and is part of a larger app with a lot of fileIO, so there is no issue with permissions/ directories etc.
Is there something different I should be doing with the stream?
Incidentally, intent.getData() is
content://gmail-ls/mexxx#gmail.com/messages/6847/attachments/0.1/BEST/false
and intent.getType() is
application/octet-stream
Thanks
All work's fine with this code
InputStream in = new BufferedInputStream(
getContentResolver().openInputStream(intent.getData()) );
File dir = getExternalCacheDir();
File file = new File(dir, Utils.md5(uri.getPath()));
OutputStream out = new BufferedOutputStream( new FileOutputStream(file) );
streamCopy(in, out);
Edit*
I have successful on the client server. Now I am doing a file transferring between 2 emulators. The file did transfer between the emulators, but I notice that the file size received is not the same as the original file. For example, A.jpg size is 900KB, but the received file is less than 900KB. I checked the file transfer size, found that there were some data(byte) lost when transferring. How is this happening?
Here's the code:
Client (Send File)
File myFile = new File ("/mnt/sdcard/Pictures/A.jpg");
FileInputStream fis = new FileInputStream(myFile);
OutputStream 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);
//Log display exact the file size
System.out.println("SO sendFile" + bytesRead);
}
os.flush();
os.close();
fis.close();
Log.d("Client", "Client sent message");
socket.close();
Server (Receive File)
FileOutputStream fos = new FileOutputStream("/mnt/sdcard/Pictures/B.jpg");
#SuppressWarnings("resource")
BufferedOutputStream bos = new BufferedOutputStream(fos);
InputStream is = clientSocket.getInputStream();
byte[] aByte = new byte[1024];
int bytesRead;
while ((bytesRead = is.read(aByte)) != -1)
{
bos.write(aByte, 0, bytesRead);
//Log display few parts the file size is less than 1024. I total up, the lost size caused the file received is incomplete
System.out.println("SO sendFile" + bytesRead);
}
clientSocket.close();
*Edit 2
While I surfed around google, I found that .read(buffer) does not guarantee read the full size(byte) of the file. Hence, the received file always lost some bytes (like space, empty character). To solve this, send the file size first to inform the receiver, then only start transfer the file.
NetworkOnMainThreadException occurs because you have to use AsyncTask
NullPointerException occurs because you are trying to use PrintWriter with the result of Sockets. As you have got nothing with Sockets you get this error.
The NetworkOnMainThreadException tells you what you are doing wrong.
You need to put the network stuff into a separate Thread (or AsyncTask or similar).
You can not call any server operation on Main Thread in Android.
In Android O.S 4.0 and above this will directly cause to NetworkOnMainThreadException. You have 2 choices :
1) Either to use AsyncTask to call your every server operation.
2) Or Use User defined Thread for any type of server operation.
I was also struggling with this Exception, only in OS version above 4.0 devices, So you can not ignore these small needs of Android.
So, I have a simple Android app that connects to a Java Server application using Sockets.
Specifically, I want to be able to send a file from the Server application to the Android app and then store that file in internal memory on the device.
The basis of the server code for transferring the file is:
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File(System.getProperty("user.home"), "text.txt")));
BufferedOutputStream bos = new BufferedOutputStream(clientSocket.getOutputStream());
byte buffer[] = new byte[1024];
int read;
while ((read = bis.read(buffer)) != -1) {
bos.write(buffer, 0, read);
}
bos.flush();
bos.close();
and the Client code to receive the file is as follows:
BufferedInputStream bis = new BufferedInputStream(clientSocket.getInputStream());
BufferedOutputStream bos = new BufferedOutputStream(openFileOutput("text.txt", Context.MODE_PRIVATE));
byte buffer[] = new byte[1024];
int read;
while ((read = bis.read(buffer)) != -1) {
bos.write(buffer, 0, read);
}
bos.flush();
bos.close();
The code appears to work fine when the client code is in a standard Java application, that is, the file sends successfully from server to client.
The problem arises when I use this code in an Android app. (Note: I use a standard FileOutputStream in the standard Java app instead of the
openFileOutput("text.txt", Context.MODE_PRIVATE))
line above).
For example purposes, the file I am transferring is a simple UTF-8 text file, which contains a single string
This is a text file.
However, when I pull this file I have copied to the emulator, from the "/data/data//files" folder on the emulator, there are an extra couple of bytes at the top of the file so the content is now
¨ÌThis is a text file.
I have no idea why this is happening and it has me stumped. I think the problem might be related to the line:
BufferedOutputStream bos = new BufferedOutputStream(openFileOutput("text.txt", Context.MODE_PRIVATE));
but I can't quite figure it out.
Any suggestions as to what I am doing wrong would be most helpful.
Thank you in advance
I am planning on writing a simple Android application that is like a small directory of professors. It will have their name, email, phone and a picture of them. I need to manually send an sqlite file from the server to the phone. I have been trying to research how to do this, but it looks like there are so many ways! I'm hoping someone can point me in the best direction!
The easiest way I can think of, is to open an URLConnection to your server, read the response and save it to your app's database directory (or SD card).
For example:
URL url = new URL("http://example.com/file.sqlite");
URLConnection conn = url.openConnection();
BufferedInputStream bin = new BufferedInputStream(conn.getInputStream());
FileOutputStream fos =
new FileOutputStream("/data/data/[your.pkg.name]/databases/file.sqlite");
byte[] buffer = new byte[1024];
int read = 0;
do {
read = bin.read(buffer, 0, buffer.length);
if (read > 0)
fos.write(buffer, 0, read);
} while (read >= 0);
After that you can open your database with a subclass of SQLiteOpenHelper or directly by calling SQLiteDatabase.openDatabase(..):
e.g.:
SQLiteDatabase.openDatabase("/data/data/[your.pkg.name]/databases/file.sqlite",
null, SQLiteDatabase.OPEN_READWRITE);