URL からファイルをダウンロードしようとすると、android 4.0 以降で問題が発生します。android Galaxy Y 2.3 / 2.2 では問題なく動作しますが、Galaxy S3(4.1.2) で同じコードを使用すると、ダウンロードは始まるが終わらない。
コード:
public void update() {
Log.d(TAG, "Método para fazer a atualização iniciado.");
try {
Log.d(TAG, "Conectando com a internet...");
URL url = new URL(
url);
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
Log.d(TAG, "Iniciando arquivo...");
String PATH = Environment.getExternalStorageDirectory()
+ "/test/Update/";
File file = new File(PATH);
file.mkdirs();
File outputFile = new File(file, "test.apk");
FileOutputStream fos = new FileOutputStream(outputFile);
Log.d(TAG, "Iniciando Stream...");
InputStream is = c.getInputStream();
Log.d(TAG, "Iniciando o download...");
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = is.read(buffer)) != -1) {
fos.write(buffer, 0, len1);
}
fos.close();
is.close();
Log.d(TAG, "Iniciando instalador...");
final Intent promptInstall = new Intent(Intent.ACTION_VIEW);
promptInstall.setDataAndType(Uri.fromFile(new File(PATH + "test.apk")),"application/vnd.android.package-archive");
promptInstall.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(promptInstall);
Log.d(TAG, "Instalador iniciado com sucesso!");
} catch (MalformedURLException e) {
Log.e(TAG, "Malformed URL. Mensagem: " + e.getMessage()
+ " Causa: " + e.getCause());
} catch (IOException e) {
Log.e(TAG, "IOException. Mensagem: " + e.getMessage() + " Causa: "
+ e.getCause());
} catch (Exception e) {
Log.e(TAG, "Exception. Mensagem: " + e.getMessage() + " Causa: "
+ e.getCause());
}
}
Obs:エラーはありません。ダウンロードを開始して停止するだけです。ファイルサイズ : 126,188 バイト、Galaxy S3 では 6,403 バイト