从服务器下载超过500 MB大小的视频或图像文件到android中的SD卡?

时间:2014-04-25 13:08:59

标签: android download background-process imagedownload

我正在尝试将图像和视频从服务器下载到SD卡以供离线播放。 但问题是图像和视频大小是否超过35 mb而不是视频未下载且应用程序崩溃。

我的图片尺寸高达50mb,视频尺寸为50到500mb,大部分视频尺寸为300mb,因此我如何下载与此尺寸相关的图像和视频。

这里我把我的代码放在一边,以便你可以检查它

  

HomeActivity.java

public class HomeActivity extends Activity 
{   
    ProgressDialog progressdialog;
    ArrayList<HashMap<String,String>> myplaylist = new ArrayList<HashMap<String,String>>();

    String filename="";
    int count=0;

    PowerManager pm;
    WakeLock wl;

    ImageView imageview;
    VideoView videoview;

    int length=0;
    int i=0; 
    boolean video=false;

    private final int TIMEOUT_CONNECTION = 10000;//5sec
    private final int TIMEOUT_SOCKET = 10000;//30sec

    @SuppressLint("Wakelock")
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        imageview=(ImageView)findViewById(R.id.imageview);
        videoview=(VideoView)findViewById(R.id.videoview);

        pm = (PowerManager)getApplicationContext().getSystemService(
                getApplicationContext().POWER_SERVICE);
        wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "WakeLockOn");
        wl.acquire();



        HashMap<String, String> map = new HashMap<String, String>();
        map.put("image","http://pr83.webofficeserver.info/img/sliderimg/original/1387205637Desertkp123-232.jpg");
        map.put("type","video");
        myplaylist.add(map);

        HashMap<String, String> map2 = new HashMap<String, String>();
        map2.put("image","http://pr83.webofficeserver.info/img/thumbnails/video/dummy.mov");
        map2.put("type","image");
        myplaylist.add(map2);

        HashMap<String, String> map3 = new HashMap<String, String>();
        map3.put("image","http://pr83.webofficeserver.info/img/sliderimg/original/1387206137planet-earth-in-space.jpg");
        map3.put("type","video");
        myplaylist.add(map3);

        HashMap<String, String> map4 = new HashMap<String, String>();
        map4.put("image","http://pr83.webofficeserver.info/img/thumbnails/video/BrandHi-240.mp4");
        map4.put("type","video");
        myplaylist.add(map4);


        new DownloadFileFromURL().execute(myplaylist.get(count).get("image").toString());

    }

    // For Download File From Url

    public class DownloadFileFromURL extends AsyncTask<String, String, String> 
    {

        private final int TIMEOUT_CONNECTION = 10000;//5sec
        private final int TIMEOUT_SOCKET = 10000;//30sec

        @Override
        protected void onPreExecute() 
        {
            progressdialog=ProgressDialog.show(HomeActivity.this, "","Please Wait");


        }


        @Override
        protected String doInBackground(String... f_url) {
            int count;
            try {

                String root = Environment.getExternalStorageDirectory().toString();
                new File(root + "/montorwerbung").mkdirs();


                            URL url = new URL(f_url[0]);    
                            Uri u = Uri.parse(f_url[0]);
                            File f1 = new File("" + u);
                            filename=f1.getName();

                            File file = new File(root + "/montorwerbung" , filename);

                            Log.e("File Download Sdcard Filename","--->"+filename);
                            Log.e("File Download Sdcard Path","--->"+file.toString());
                            long startTime = System.currentTimeMillis();

                            //Open a connection to that URL.
                            URLConnection ucon = url.openConnection();

                            //this timeout affects how long it takes for the app to realize there's a connection problem
                             //  ucon.setReadTimeout(TIMEOUT_CONNECTION);
                              // ucon.setConnectTimeout(TIMEOUT_SOCKET);

                            //Define InputStreams to read from the URLConnection.
                            // uses 3KB download buffer
                            InputStream is = ucon.getInputStream();
                            BufferedInputStream inStream = new BufferedInputStream(is, 1024 * 1024);

                            FileOutputStream outStream = new FileOutputStream(file);
                            byte[] buff = new byte[10 * 1024];

                            //Read bytes (and store them) until there is nothing more to read(-1)
                            int len;
                            while ((len = inStream.read(buff)) != -1)
                            {
                                outStream.write(buff,0,len);
                            }

                            //clean up
                            outStream.flush();
                            outStream.close();
                            inStream.close();




            } catch (Exception e) 
            {
                Log.e("Download file frp, server Exception", "Exception : "
                        + e.getMessage(), e); 
                //Log.e("Error: ",+e.printStackTrace());
            }

            return null;
        }


        @Override
        protected void onPostExecute(String file_url) 
        {
            if(progressdialog.isShowing())
            {   
                progressdialog.dismiss();

            }

            Log.e("Count: ","----->"+count);

            count++;
            if(count<myplaylist.size())
            {
                new DownloadFileFromURL2().execute(myplaylist.get(count).get("image").toString());

            }
            else
            {
                wl.release();
                Log.e("No More Downloads","--->");
            }

        }

    }
class DownloadFileFromURL2 extends AsyncTask<String, String, String> 
    {

        @Override
        protected void onPreExecute() 
        {
            progressdialog=ProgressDialog.show(HomeActivity.this, "","Please Wait");
        }


        @Override
        protected String doInBackground(String... f_url) {
            int count;
            try {

                String root = Environment.getExternalStorageDirectory().toString();
                new File(root + "/montorwerbung").mkdirs();

                            URL url = new URL(f_url[0]);    

                            Uri u = Uri.parse(f_url[0]);
                            File f1 = new File("" + u);
                            filename=f1.getName();

                            File file = new File(root + "/montorwerbung" , filename);

                            Log.e("File Download Sdcard Filename","--->"+filename);
                            Log.e("File Download Sdcard Path","--->"+file.toString());
                            long startTime = System.currentTimeMillis();
                             //Open a connection to that URL.
                            URLConnection ucon = url.openConnection();

                            //Define InputStreams to read from the URLConnection.
                            // uses 3KB download buffer
                            InputStream is = ucon.getInputStream();
                            BufferedInputStream inStream = new BufferedInputStream(is, 1024 * 10);

                            FileOutputStream outStream = new FileOutputStream(file);

                            byte[] buff = new byte[10 * 1024];

                            //Read bytes (and store them) until there is nothing more to read(-1)
                            int len;
                            while ((len = inStream.read(buff)) != -1)
                            {
                                outStream.write(buff,0,len);
                            }

                            //clean up
                            outStream.flush();
                            outStream.close();
                            inStream.close();



            } catch (Exception e) 
            {
                Log.e("Download file frp, server Exception", "Exception : "
                        + e.getMessage(), e); 
                //Log.e("Error: ",+e.printStackTrace());
            }

            return null;
        }


        @Override
        protected void onPostExecute(String file_url) 
        {
            if(progressdialog.isShowing())
            {   
                progressdialog.dismiss();

            }

            count++;

            Log.e("Count: ","----->"+count);
            if(count<myplaylist.size())
            {
                new DownloadFileFromURL().execute(myplaylist.get(count).get("image").toString());
                count++;
            }
            else
            {
                wl.release();
                Log.e("No More Downloads 2","--->");
            }

        }

    }

0 个答案:

没有答案