2010-09-11 7 views
16

मेरे पास एक वेबसाइट पर एक वीडियो फ़ाइल है .एमपी 4 प्रारूप और मैं उपयोगकर्ता को एक लिंक पर क्लिक करके वीडियो को अपने एसडी कार्ड में डाउनलोड करने में सक्षम होना चाहता हूं। क्या इसे करने का कोई आसान तरीका है। मेरे पास वर्तमान में यह कोड है लेकिन यह काम नहीं कर रहा है ... सुनिश्चित नहीं है कि मैं क्या गलत कर रहा हूं। किसी भी मदद के लिए धन्यवाद!एंड्रॉइड: मैं एसडी कार्ड में एक वीडियो फ़ाइल कैसे डाउनलोड करूं?

import java.io.BufferedInputStream; 
import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.net.URL; 
import java.net.URLConnection; 

import org.apache.http.util.ByteArrayBuffer; 

import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 

public class VideoManager extends Activity { 
/** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState);} 



     private final String PATH = "/sdcard/download/"; //put the downloaded file here 


     public void DownloadFromUrl(String VideoURL, String fileName) { //this is the downloader method 
       try { 
         URL url = new URL("http://www.ericmoyer.com/episode1.mp4"); //you can write here any link 
         File file = new File(fileName); 

         long startTime = System.currentTimeMillis(); 
         Log.d("VideoManager", "download begining"); 
         Log.d("VideoManager", "download url:" + url); 
         Log.d("VideoManager", "downloaded file name:" + fileName); 
         /* Open a connection to that URL. */ 
         URLConnection ucon = url.openConnection(); 

         /* 
         * Define InputStreams to read from the URLConnection. 
         */ 
         InputStream is = ucon.getInputStream(); 
         BufferedInputStream bis = new BufferedInputStream(is); 

         /* 
         * Read bytes to the Buffer until there is nothing more to read(-1). 
         */ 
         ByteArrayBuffer baf = new ByteArrayBuffer(50); 
         int current = 0; 
         while ((current = bis.read()) != -1) { 
           baf.append((byte) current); 
         } 

         /* Convert the Bytes read to a String. */ 
         FileOutputStream fos = new FileOutputStream(PATH+file); 
         fos.write(baf.toByteArray()); 
         fos.close(); 
         Log.d("VideoManager", "download ready in" 
             + ((System.currentTimeMillis() - startTime)/1000) 
             + " sec"); 

       } catch (IOException e) { 
         Log.d("VideoManager", "Error: " + e); 
       } 

     } 
} 
+0

क्या आप सुनिश्चित हैं कि पथ/एसडीकार्ड/डाउनलोड/मौजूद है? आप इसे एडीबी खोल –

उत्तर

35

स्मृति से बाहर नहीं चल रहे हैं? मुझे लगता है कि एक वीडियो फ़ाइल बहुत बड़ी है - जिसे आप फ़ाइल में लिखने से पहले बफरिंग कर रहे हैं।

मुझे पता है कि आपका उदाहरण कोड इंटरनेट पर है - लेकिन यह डाउनलोड करने के लिए बीएडी है! इसका उपयोग करें:

private final int TIMEOUT_CONNECTION = 5000;//5sec 
private final int TIMEOUT_SOCKET = 30000;//30sec 


      URL url = new URL(imageURL); 
      long startTime = System.currentTimeMillis(); 
      Log.i(TAG, "image download beginning: "+imageURL); 

      //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 * 5); 
      FileOutputStream outStream = new FileOutputStream(file); 
      byte[] buff = new byte[5 * 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(); 

      Log.i(TAG, "download completed in " 
        + ((System.currentTimeMillis() - startTime)/1000) 
        + " sec");5 
+2

ने 'outStream.flush() जोड़ा;' यह 0 बाइट फाइलों की समस्या हल करता है –

+1

क्या वर्तमान में डाउनलोड करने वाला वीडियो चला सकता है? मतलब यह है कि एक .mp4 वर्तमान में राज्य डाउनलोड करने में है, तो क्या MediaPlayer का उपयोग करते समय इसे डाउनलोड करना संभव है? – Scorpion

+0

@ कुछ और कहीं मैं .swf के प्रारूप में वीडियो कर रहा हूँ? क्या यह आपके कोड का उपयोग कर डाउनलोड किया जाएगा? – Dhasneem

23

कभी भी बाहरी भंडारण के लिए पथ को कड़ी मेहनत न करें। आपका पथ कई उपकरणों पर गलत है। बाहरी भंडारण की रूट प्राप्त करने के लिए Environment.getExternalStoragePath() का उपयोग करें (/sdcard या /mnt/sdcard या कुछ और हो)।

ऑब्जेक्ट का उपयोग करके अपनी उपनिर्देशिका बनाना सुनिश्चित करें, आप Environment.getExternalStoragePath() से वापस आते हैं।

और, अंततः, केवल "यह काम नहीं कर रहा" कहें। हमें नहीं पता कि "लेकिन यह काम नहीं कर रहा है" का मतलब आपके मामले में है। उस जानकारी के बिना, आपकी मदद करना बहुत मुश्किल है।

+0

के माध्यम से बना सकते हैं। मुझे अगली बार याद आएगा। –

संबंधित मुद्दे