2015-01-12 47 views
0

我们有一个要求从谷歌+/picasa下载视频并将其存储到SD卡。 你能请任何人帮我解决这个问题吗? 谷歌+/Picasa中视频从谷歌+/picasa下载android

enter image description here

将从URIbyte[],然后byte[]保存到文件:

InputStream videoStream = getActivity().getContentResolver().openInputStream(videoUri); 
byte bytes[] = ByteStreams.toByteArray(videoStream); 
videoFile = new File("abcd.mp4"); 
FileOutputStream out = new FileOutputStream(videoFile); 
out.write(bytes); 
out.close(); 
+0

请告诉我们你到目前为止所尝试过的。 – Vishwanath

+0

请检查编辑后的代码。现在视频存储在SD卡中,但没有播放...... :( – Krishna

回答

0

你可以尝试一个:

public String DownloadFromUrl(String DownloadUrl, String fileName) { 

File SDCardRoot = null; 
try { 
    SDCardRoot = Environment.getExternalStorageDirectory(); 
    File files = new File(SDCardRoot+fileName); 
    int sizeoffile; 
    if(!files.exists()) 
    { 
     File root = android.os.Environment.getExternalStorageDirectory();    

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

     URL url = new URL(DownloadUrl); 
     File file = new File(dir, fileName); 

     /* Open a connection to that URL. */ 
     URLConnection ucon = url.openConnection(); 
     sizeoffile = ucon.getContentLength(); 
     Log.d("SIZEOFFILE: ", sizeoffile+" BYTE"); 
     /* 
     * 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) { 
    e.getMessage(); 
} 

return SDCardRoot+fileName; } 
0
Finally i found the solution. 



Uri videoUri = data.getData(); 
        File videoFile = null; 
        final InputStream imageStream; 
        try { 
         imageStream = getActivity().getContentResolver().openInputStream(videoUri); 
         byte bytes[] = ByteStreams.toByteArray(imageStream);//IStoByteArray(imageStream); 
         videoFile = new File(Environment.getExternalStorageDirectory()+ "/"+System.currentTimeMillis()+".mp4"); 
         videoFile.createNewFile(); 
         FileOutputStream out = new FileOutputStream(videoFile); 
         out.write(bytes); 
         out.close(); 
        } catch (FileNotFoundException e) { 
         e.printStackTrace(); 
        }catch (Exception ee){ 
         ee.printStackTrace(); 
        } 
0

我有最近遇到这个。

我第一次发现我收到的是图片而不是视频。

但我不明白为什么Facebook成功播放通过(Google +)照片分享的在线视频。

然后,我偶尔发现他们目前给出的文件是GIF,其原始扩展名为contentUri的MediaStore.Images.Media.DISPLAY_NAME部分。

Eeek!