I can't access a video after download it in android -


I have a code to download a video from my server and save it to an SD card. I use this code:

  string videoURL = "http://www.myapp.com" + key + "/"+key+".avi"; String PATHSdcard = getSDFile (); {// Set Download URL, a URL that points to the file on the Internet // This is the URL url = file downloading new URL (video url); // Create new connection HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection (); // Set up some things on the connection urlConnection.setRequestMethod ("GET"); UrlConnection.setDoOutput (true); // Connect! UrlConnection.connect (); Set // where we want to save the file to // in this case, to save it to the root directory of the // sd card. File SDCardRoot = Environment.getExternalStorageDirectory (); // Create a new file, specifying the path, and the file name // which we want to save the file. File file = new file (PATHSdcard, key + ".avi"); // It will be used to write the downloaded data to the file, in which we have created the FileApplyputTream fileOutput = new file-output stream (file); // It will be used to read data from the internet InputStream inputStream = urlConnection.getInputStream (); // Create a buffer ... byte [] buffer = new byte [1024]; Int buffer lang = 0; // To store a temporary size of buffer / Now, read through the input buffer and add the data while writing the content in the file ((buffer lang = inputstream.read (buffer)) gt; 0) {// File file buffer to output stream (file on SD card file. Output. Written (buffer, 0, buffer lang);} Close the output stream, when the file is output.flash (); fileOptput.Close (); Send Broadcast (new intent (Intent.ACTION_MEDIA_MOUNTED, Uri.parse ("file: //" + Environment.getExternalStorageDirectory ()); // Hold some potential errors ...} hold (malformedULException e) {e.printStackTrace ();} hold (IOException e) {e.printStackTrace ();}  

I download it correctly, but I can not play the video after downloading it.

If I close my application and open again, Remedy works? Thanks in advance

Ensure that you have a right to write in the file Umti is. Include in your manifest file, the line

  & lt; Usage-permission Android: name = "android.permission.WRITE_EXTERNAL_STORAGE" /> Updated: I'm not sure what problem is in your code, but can you try your code, maybe it will work:  
 < Code> public void DownloadFromUrl (string) DownloadUrl, string filename) {try {file root = android.os.Environment.getExternalStorageDirectory ()} File directory = new file (root.etabsolutepoint (+ "/ your_downloads"); if (dir .exists () == incorrect) {dir.mkdirs ();} URL url = new url (download url); // here any link file file = new file (dir, filename) Open a connection at that URL * / URLConnection ucon = url.openConnection (); / * * Define the input streams to read from the URL connection * / inputstream is = ucon.getInputStream (); BufferedInputStream bis = New BufferedInputStream (is); / * * Read buffer up to buffer, as long as there is nothing to read (-1). * / Bytebrand buff = new bytereferver (5000); current = 0; while (( Current = bis.read ())! = -1) {baf.append (byte) on); } / * Convert the bytes read into a string * / FileOutputStream fos = new FileOutputStream (file); Fos.write (baf.toByteArray ()); Fos.flush (); Fos.close (); } Hold (IOException e) {log d. ("Download manager", "error:" + e); }}  

Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -