I am trying to upload a video file and then play it in the application, if I put the video file in the res folder, I can play it without problems, however, when I try to download the video, play it I get VideoView Error: 1, -2147483648.
This is the code I use to download the video:
try
{
URL url = new URL(videoURL);
URLConnection conexion = url.openConnection();
conexion.connect();
int lengthOfFile = conexion.getContentLength();
Log.d("ANDRO_ASYNC", "Length of file: " + lengthOfFile);
InputStream input = new BufferedInputStream(url.openStream());
final File dir = new File(this.getFilesDir() + "/videos");
dir.mkdirs();
final File file = new File(dir, "my_video.mp4");
Log.d("writePath", file.getPath());
FileOutputStream output = new FileOutputStream(file);
byte data[] = new byte[1024];
long total = 0;
while((count = input.read(data)) != -1)
{
total += count;
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
Log.d("download", "finished");
}
catch(Exception e)
{
Log.d("Exception", e.getMessage());
}
This seems to succeed, but when I try to play the file with the following code, I get an error.
videoView = (VideoView)findViewById(R.id.surface_view);
File f = new File("/data/data/com.companyname.appname/files/videos/videoFilename.mp4");
if(f.exists())
{
long len = f.length();
String length = String.valueOf(len);
Log.d("file", "exists");
Log.d("length", (String)length);
}
else
{
Log.d("file", "does not exist");
}
videoView.setMediaController(new MediaController(this));
videoView.setVideoPath(f.getPath());
videoView.start();
As can be seen from the code, before playing back I check if the file exists, as well as the size (the same size as the downloaded file), but it will not play, I assume that the file is damaged, but I donβt know why.
I have the following permissions set in the manifest
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_INTERAL_STORAGE" />