0

I want to download an image from server, save it on the SD card, and show it. I wrote that code, but it doesn't work - there are no bugs, but I see only a black screen instead of the image.

public class Main extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    MyTask mt = new MyTask();
    mt.execute();

    Intent intent = new Intent();  
    intent.setAction(android.content.Intent.ACTION_VIEW);  
    File file = new File("/sdcard/askeroid/logos/1_mobile.png");  
    intent.setDataAndType(Uri.fromFile(file), "image/*");  
    startActivity(intent); 
}  

}
class MyTask extends AsyncTask {

@Override
protected Void doInBackground(Void... params) {
    try{
        URL url = new URL("http://ed.sadko.mobi/logo/logo_1mobile.png");
        URLConnection connection = url.openConnection();
        connection.connect();

        InputStream input = new BufferedInputStream(url.openStream());
        OutputStream output = new FileOutputStream("/sdcard/askeroid/logos/1_mobile.png");

        output.flush();
        output.close();
        input.close();

    } catch(Exception e){e.printStackTrace();}
  return null;
}

}

Stas0n
  • 127
  • 1
  • 10

2 Answers2

1

You don't wait for the end of the download before moving to next activity.

I suggest you use AsyncTask - download the image using in doInBackground and start next activity in onPostExecute.

Something like:

AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
    @Override protected Long doInBackground(Void ... urls) {
        // download the image
    }

    @Override protected void onPostExecute(Void result) {
        // start new activity...
    }
};
task.execute();

Also, please note that the path to the SD card may vary between different devices. Take a look here to see how to access it properly.

Community
  • 1
  • 1
MByD
  • 135,866
  • 28
  • 264
  • 277
  • Also, the linked documentation to AsyncTask has a full code sample. – digitaljoel Oct 10 '12 at 21:53
  • @Binyamin, i've changed my code, but it still doesn't wirk - i edited my question, there is new code – Stas0n Oct 10 '12 at 22:36
  • Sorry for the late response. First, as digitaljoel wrote, you don't write to the output stream from the input stream, you only create them, so you don't write anything. Second, you need to start the new activity from `onPostExecute()`, not right after calling `execute` – MByD Oct 11 '12 at 21:32
1

+1 for The answer to use AsyncTask, it makes threading on android super easy. Another problem is that you open the InputStream and the OutputStream but you never actually read anything from the input nor do you write anything to the output, so the file on your sdcard is going to be empty.

digitaljoel
  • 26,265
  • 15
  • 89
  • 115
  • +1 Someone told me a few days ago that if someone always uses a hammer, everything look like nails to him. I really overlooked this tiny-tiny issue :P – MByD Oct 10 '12 at 21:49
  • @BinyaminSharet You should add my info to your answer so the best answer is all in one place and the submitter can accept your more complete answer. – digitaljoel Oct 10 '12 at 21:52
  • like @BinyaminSharet said, you should start the next intent in the onPostExecute of the AsyncTask implementation. You are still starting the next intent immediately after starting the thread so the image download doesn't complete. You also are still not writing the image to the file system because you never use the streams you open. – digitaljoel Oct 11 '12 at 00:46