在android下载后,我无法访问视频

时间:2014-10-28 12:46:41

标签: android android-mediaplayer

我有一个代码可以从我的服务器下载视频并将其保存在SD卡中。我使用这段代码:

String videoURL = "http://www.myapp.com" + key + "/"+key+".avi";
    String PATHSdcard = getSDFile();

    try {
        //set the download URL, a url that points to a file on the internet
        //this is the file to be downloaded
        URL url = new URL(videoURL);

        //create the new connection
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

        //set up some things on the connection
        urlConnection.setRequestMethod("GET");
        urlConnection.setDoOutput(true);

        //and connect!
        urlConnection.connect();

        //set the path where we want to save the file
        //in this case, going to save it on the root directory of the
        //sd card.
        File SDCardRoot = Environment.getExternalStorageDirectory();
        //create a new file, specifying the path, and the filename
        //which we want to save the file as.
        File file = new File(PATHSdcard,key+".avi");

        //this will be used to write the downloaded data into the file we created
        FileOutputStream fileOutput = new FileOutputStream(file);

        //this will be used in reading the data from the internet
        InputStream inputStream = urlConnection.getInputStream();

        //create a buffer...
        byte[] buffer = new byte[1024];
        int bufferLength = 0; //used to store a temporary size of the buffer

        //now, read through the input buffer and write the contents to the file
        while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
                //add the data in the buffer to the file in the file output stream (the file on the sd card
                fileOutput.write(buffer, 0, bufferLength);
        }
        //close the output stream when done
        fileOutput.flush();
        fileOutput.close();
        sendBroadcast (
                new Intent(Intent.ACTION_MEDIA_MOUNTED,
                    Uri.parse("file://" + Environment.getExternalStorageDirectory()))
            );

    //catch some possible errors...
    } catch (MalformedURLException e) {
            e.printStackTrace();
    } catch (IOException e) {
            e.printStackTrace();
    }

我正确下载,但下载后无法播放视频。

如果我关闭我的应用程序并再次打开,视频工作正常。有解决方案吗 提前致谢

1 个答案:

答案 0 :(得分:0)

确保您具有写入文件的正确权限。在清单文件中,包含此行

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />.

更新: 我不确定您的代码中有什么问题,但是您可以尝试我的代码,也许它会起作用:

public void DownloadFromUrl(String DownloadUrl, String fileName) {

try {
           File root = android.os.Environment.getExternalStorageDirectory();               

           File dir = new File (root.getAbsolutePath() + "/your_downloads");
           if(dir.exists()==false) {
                dir.mkdirs();
           }

           URL url = new URL(DownloadUrl); //you can write here any link
           File file = new File(dir, 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(5000);
           int current = 0;
           while ((current = bis.read()) != -1) {
              baf.append((byte) current);
           }


           /* Convert the Bytes read to a String. */
           FileOutputStream fos = new FileOutputStream(file);
           fos.write(baf.toByteArray());
           fos.flush();
           fos.close();

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

}