I need to download 100MB of images, so i decided that the best way is to make Service wich download it, and will show results for each file in activity. But this works like theres no service, the Activity fzreees, and unfreezes only after download all files.
Heres the code of Activity:
public class DownloadActivity extends Activity
{
String hist;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.download_activity);
startService(new Intent(this, DownloadService.class));
registerReceiver(broadcastReceiver,
new IntentFilter(DownloadService.BROADCAST_ACTION));
}
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent _intent)
{
updateUI(_intent);
}
};
private void updateUI(Intent intent)
{
if (intent.getBooleanExtra("exists", false))
hist = hist + "Item " +
intent.getIntExtra("item", -1) + ", image " +
intent.getIntExtra("obraz", -1) + " - DOWNLOADED\n";
else
hist = hist + "Item " +
intent.getIntExtra("item", -1) + ", image " +
intent.getIntExtra("obraz", -1) + " - ALREADY EXISTS\n";
((TextView) findViewById(R.id.dtitle)).setText("Item " +
intent.getIntExtra("item", -1) + ", image " +
intent.getIntExtra("image", -1) + ".");
((TextView) findViewById(R.id.ddetails)).setText(hist);
}
}
Code of Service:
public class DownloadService extends Service
{
public static final String BROADCAST_ACTION = "emis.katalog.grzybow.publishprogress";
Intent intent;
int counter = 0;
String postString;
@Override
public IBinder onBind(Intent arg0)
{
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate()
{
super.onCreate();
intent = new Intent(BROADCAST_ACTION);
}
@Override
public void onStart(Intent intent, int startId)
{
SQLiteDatabase db = new BazaGrzybowHelper(DownloadService.this).getReadableDatabase();
Cursor kursor = db.rawQuery("SELECT * FROM table", null);
InputStream in = null;
OutputStream out = null;
URL ulrn;
int nn = 1;
int pos = 1;
//out:
while(kursor.moveToNext())
{
while(kursor.getString(kursor.getColumnIndex("i_url_" + nn)) != "" ||
kursor.getString(kursor.getColumnIndex("i_url_" + nn)) != null)
{
String filename = "thg_" + pos + "_" + (nn+2) + ".jpg";
if (new File(Environment.getExternalStorageDirectory(),
"emis/katalog.grzybow/zapis_na_stale/"+filename).exists())
publishProgress(pos, nn, true);
else
{
publishProgress(pos, nn, false);
File destDir = new File(Environment.getExternalStorageDirectory(),
"emis/katalog.grzybow/zapis_na_stale");
if (!destDir.exists())
destDir.mkdirs();
destDir = null;
try
{
ulrn = new URL(kursor.getString(kursor.getColumnIndex("i_url_" + nn)));
HttpURLConnection con = (HttpURLConnection)ulrn.openConnection();
in = con.getInputStream();
out = new FileOutputStream(Environment.getExternalStorageDirectory().
getPath() + "/emis/katalog.grzybow/zapis_na_stale/" + filename);
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
}
catch(Exception e)
{
e.printStackTrace();
}
}
nn++;
if (nn > 10 || kursor.getString(kursor.getColumnIndex("i_url_" + nn)) == "" ||
kursor.getString(kursor.getColumnIndex("i_url_" + nn)) == null)
{
nn = 1;
break;
}
/*if (anuluj)
break out;*/
}
pos++;
}
db.close();
}
private void publishProgress(int item, int image, boolean exists)
{
intent.putExtra("item", item);
intent.putExtra("image", image);
intent.putExtra("exists", exists);
sendBroadcast(intent);
}
private void copyFile(InputStream in, OutputStream out) throws IOException
{
byte[] buffer = new byte[1024];
int read;
while((read = in.read(buffer)) != -1)
{
out.write(buffer, 0, read);
}
}
}
What am I doing wrong?