0

Now I have to download a file whose url has known. I need to save it to SD card when download action finished. The problem is I should know whether the file is existed before downloading. So I plan to save the file with a identified filename which is generated from url. So when I get the url I can calculate his corresponding filename. Which algorithm should I use?

BTW, JAVA is what I'm using.

Maybe, I have not told my requirement clearly. Fetch the filename "abc.png" from url "www.yahoo.com/abc.png" is not what I need. Because "www.google.com/abc.png" results the same filename. I need to generate a unique filename from url.

xing.zhang
  • 359
  • 1
  • 2
  • 13

1 Answers1

0

full example working ...i tried myself some days ago.. im sure it will help..

package com.imagedownloader;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;

public class ImageDownloaderActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Bitmap bitmap=DownloadImage("http://www.allindiaflorist.com/imgs/arrangemen4.jpg");
        ImageView img =(ImageView)findViewById(R.id.imageView1);
        img.setImageBitmap(bitmap);

    }


    private Bitmap DownloadImage(String URL) {
        // TODO Auto-generated method stub
        Bitmap bitmap=null;
        InputStream in=null;
        try {

        in=OpenHttpConnection(URL);
        bitmap=BitmapFactory.decodeStream(in);

            in.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return bitmap;
    }

    private InputStream OpenHttpConnection(String stingurl) throws IOException {
        // TODO Auto-generated method stub
        InputStream in=null;
        int response=-1;

            URL url = new URL(stingurl);
            URLConnection conn=url.openConnection();



            if(!(conn instanceof HttpURLConnection))
                    throw new IOException("not and http exception");

            try{

                HttpURLConnection httpconn=(HttpURLConnection)conn;
                httpconn.setAllowUserInteraction(false);
                httpconn.setInstanceFollowRedirects(true);
                httpconn.setRequestMethod("GET");
                httpconn.connect();

                response=httpconn.getResponseCode();
                if(response==HttpURLConnection.HTTP_OK)
                {
                    in=httpconn.getInputStream();

                }

            }
            catch(Exception ex)
            {throw new IOException("Error connecting");   }
        return in;
    }
}
ashish.n
  • 1,234
  • 14
  • 30
  • I think you have misunderstood. I know how to download a file from url. What a need is how to give a identified filename when download finished. – xing.zhang May 15 '12 at 11:26
  • there is no such way...you need to know the file name you are downloading from the url... in such case you can directly apply that name to the file.. – ashish.n May 15 '12 at 11:37