I am targeting my app for android 11 and I am experiencing a lot of problems when using MediaStore lousy way of working with files!
InputStream inputStream = connection.getInputStream();
BufferedInputStream bis = new BufferedInputStream(inputStream);
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DISPLAY_NAME, fileName);
values.put(MediaStore.MediaColumns.MIME_TYPE, mediaType+"/"+fileExt.replace(".",""));
final ContentResolver resolver = context.getContentResolver();
Uri uri = null;
final Uri contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
ContentValues contentValues = new ContentValues();
String fname = pref.getGalleryName()+File.separator +fileName;
contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, fileName);
if(fileExt.equals(".mp4"))
contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "video/mp4");
else if(fileExt.equals(".mp3"))
contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
else contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "audio/*");
contentValues.put(
MediaStore.Audio.Media.RELATIVE_PATH, Environment.DIRECTORY_DOWNLOADS );
String[] projection = {MediaStore.MediaColumns._ID,
MediaStore.MediaColumns.DISPLAY_NAME,
MediaStore.MediaColumns.RELATIVE_PATH,
MediaStore.MediaColumns.DATE_MODIFIED
};
uri = resolver.insert(MediaStore.Downloads.EXTERNAL_CONTENT_URI, contentValues);
contentValues.put(MediaStore.Audio.Media.IS_PENDING, 0);
}
When downloading the file through the code below I realized that the download is very slow, unviable to use this method
if (uri != null) {
OutputStream outputStream = context.getContentResolver().openOutputStream(uri);
int lenghtOfFile = connection.getContentLength();
if (outputStream != null) {
BufferedOutputStream bos = new BufferedOutputStream(outputStream);
byte[] buffer = new byte[]{(byte) (1024 >>> 8), (byte) 1024};
int bytes = bis.read(buffer);
long total = 0;
int len1 = 0;
while ((len1 = bis.read(buffer)) > 0) {
total += len1;
publishProgress("" + (int)((total*100)/lenghtOfFile));
bos.write(buffer, 0, len1);
}
bos.close();
}
}
bis.close();
Is it possible to download faster? I just need to download 2 media files being an .mp3 and an .mp4
Using android 11 even though it is a media file, is there no other way to download this file in another way?