Can some body explain me what am i doing wrong, I'm working on an app that need to download a video an save the file on the device...and then play de video.. I'm testing the app with images and videos, I'm able to donwload images and save them on the device, i can also download the video and then play the video.....the problem is that if i close the app and then i try to play the video and get the error "No Content provider" but the images are there, so it look like im able to save the images on the device but not the video?
Any ideas why is this happening.
public class MainActivity extends AppCompatActivity {
private String TAG = MainActivity.class.getSimpleName();
// String image_url = "https://st1.uvnimg.com/dims4/default/48609b9/2147483647/resize/1093x820%3E/quality/75/?url=https%3A%2F%2Fuvn-brightspot.s3.amazonaws.com%2F95%2F69%2F0a7817d84e678beb6ffbc7c14f3e%2Fresizes%2F1500%2Fgettyimages-1019549260.jpg";
// String image_url = "https://st1.uvnimg.com/dims4/default/2cf8c08/2147483647/resize/935x645%3E/quality/75/?url=https%3A%2F%2Fuvn-brightspot.s3.amazonaws.com%2Ffd%2F77%2F07f78fb84b35aed6eda04734d014%2Fresizes%2F1500%2F20190509-3586.jpg";
String image_url = "https://mywebsize.com/testvido.mp4";
Button button;
Button button2;
Button button3;
Button button4;
VideoView videoView;
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button4 = (Button) findViewById(R.id.button4);
imageView = (ImageView) findViewById(R.id.image_view);
videoView = (VideoView) findViewById(R.id.videoview);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DownloadTask downloadTask = new DownloadTask();
downloadTask.execute(image_url);
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String path = "sdcard/photoalbum/image.jpg";
Log.e(TAG, "PATh:" + path);
imageView.setImageDrawable(Drawable.createFromPath(path));
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String path = "sdcard/photoalbum/image2.jpg";
Log.e(TAG, "PATh:" + path);
imageView.setImageDrawable(Drawable.createFromPath(path));
}
});
button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String path = "sdcard/photoalbum/video.mp4";
Log.e(TAG, "PATh:" + path);
videoView.setVideoURI(Uri.parse(path));
}
});
}
class DownloadTask extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
String path = params[0];
int file_length = 0;
try {
URL url = new URL(path);
URLConnection urlConnection = url.openConnection();
urlConnection.connect();
file_length = urlConnection.getContentLength();
File new_folder = new File("sdcard/photoalbum");
Log.e(TAG, "PATh:" + new_folder);
if(!new_folder.exists()) {
new_folder.mkdir();
}
File input_file = new File(new_folder, "video.mp4");
Log.e(TAG, "PATh:" + input_file);
InputStream inputStream = new BufferedInputStream(url.openStream(), 8192);
byte[] data = new byte[1027];
int total = 0;
int count = 0;
OutputStream outputStream = new FileOutputStream(input_file);
while ((count=inputStream.read(data)) != -1) {
total += count;
outputStream.write(data, 0, count);
Log.e(TAG, "Descargando: "+ total);
}
inputStream.close();
outputStream.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "Download complete";
}
@Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(String result) {
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
String path = "sdcard/photoalbum/video.mp4";
Log.e(TAG, "PATh:" + path);
// imageView.setImageDrawable(Drawable.createFromPath(path));
videoView.setVideoURI(Uri.parse(path));
videoView.start();
}
}
}