0

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?

Will V
  • 142
  • 11
  • `byte[] buffer = new byte[]{(byte) (1024 >>> 8), (byte) 1024};` What is that? Please explain what you are doing. And how many bytes is your buffer? So what is buffer.length? 2? – blackapps May 07 '21 at 16:50
  • `int bytes = bis.read(buffer);` You are reading some bytes but not saving them to outputstream. So all your files will be corrupt and have less bytes than original please check file sizes. – blackapps May 07 '21 at 16:53
  • Have you considered using a more modern HTTP client API, [such as OkHttp](https://stackoverflow.com/a/29012988/115145)? And, have you considered using something other than `AsyncTask`? – CommonsWare May 07 '21 at 17:18
  • @CommonsWare Before posting here I saw an example that you posted on: https://commonsware.com/blog/2019/12/21/scoped-storage-stories-storing-mediastore.html However I needed the code in java, I am not yet able to program Kotlin, do you have that same code in Java please? – Will V May 07 '21 at 18:21
  • Sorry, no, I do not have that in Java. – CommonsWare May 07 '21 at 18:26

0 Answers0