以下のようにAsyncTaskで画像のダウンロードを非同期の並列で行っているのですが
画像の数が多いので仕様として、裏で画像をダウンロードし続けるのですが、
この処理が完了していないうちにstartActivityで別の画面に遷移しようとすると
数秒間遷移しません。遷移する端末もあるので何らかの機種依存だと思いますが
何か考えられる問題はあるでしょうか
AsyncTask処理
private void imageDownload(final List<String> urls, final String filename) { new AsyncTask<Void, Void, Boolean>() { @Override protected Boolean doInBackground(Void... params) { int i=0; for (String url : urls) { try { downloadUtil.getDownloadFile(url, filename+i); }catch (Exception e){ Log.e("image download error",e.toString()); } } return null; } }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); }
DownLoad処理
public class DownloadUtil { private Context mContext; public DownloadUtil(Context context) { mContext = context; } public String getDownloadFile(String downloadUrl,String filename) throws IOException { URL url = new URL(downloadUrl); // テンポラリファイルの設定 Log.d("download_url",downloadUrl); // HTTP接続開始 HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.connect(); int response = connection.getResponseCode(); if( response != HttpURLConnection.HTTP_OK){ //取得コードエラー処理 Log.v("取得コードエラー処理","NG"); return null; } //入力ストリーム(from HTTP) int contentsLength = connection.getContentLength(); InputStream is = connection.getInputStream(); BufferedInputStream bis = new BufferedInputStream(is); DataInputStream dis = new DataInputStream(bis); FileOutputStream fo = mContext.openFileOutput(filename, Activity.MODE_PRIVATE); DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(fo)); //データ読み byte[] b = new byte[1024]; int readbyte = 0, totalbyte = 0; while( ( readbyte = dis.read(b)) != -1 ){ dos.write(b,0,readbyte); totalbyte += readbyte; } //ストリームクローズ dis.close(); dos.close(); return filename; } }
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。