3

I have a Galaxy S10 and recently I got an update for Android 10 and suddenly I got some errors in my app with filepaths.

The code below works for all versions except Android 10

This is the way I download stuff:

                            int res = context.checkCallingOrSelfPermission(permission);
                            if (res == PackageManager.PERMISSION_GRANTED) {

                                Toast.makeText(con, "Download is starting", Toast.LENGTH_SHORT).show();
                                DownloadManager downloadmanager = (DownloadManager) con.getSystemService(Context.DOWNLOAD_SERVICE);
                                DownloadManager.Request request = new DownloadManager.Request(Uri.parse(lin));
                                titl.replace("#", "");
                                titl.replace("-", "");
                                titl = titl.substring(0, Math.min(titl.length(), 20));

                                request.setTitle(titl);
                                request.setDescription("Downloading");
                                request.setNotificationVisibility(1);
                                request.setVisibleInDownloadsUi(false);
                                File fileo = Environment.getExternalStorageDirectory();

                                File Dir = new File(fileo.getAbsolutePath() + "/Test");

                                if (!Dir.exists()) {
                                    Dir.mkdir();

                                }
                                StringBuilder sb = new StringBuilder();
                                sb.append("file:///");
                                sb.append(fileo);
                                sb.append("/Test/");
                                sb.append(titl);
                                sb.append(".mp4");
                                request.setDestinationUri(Uri.parse(sb.toString()));
                                downloadmanager.enqueue(request);
                                System.out.println("Downloader");
                            } else {
                                Toast.makeText(con, "Please go to Settings and allow permission to download", Toast.LENGTH_LONG).show();
                            }

How I read the Downloads

if (checkPermission() == true) {

            File fileo = Environment.getExternalStorageDirectory();
            String path = fileo.toString() + "/Test";
            System.out.println(path);
            File directory = new File(path);
            System.out.println(directory.toString());

            File[] files = directory.listFiles();

            System.out.println(Arrays.toString(files));

            Bitmap bitmap;
            if(files.length > 0 ) {
                for (int i = 0; i < files.length; i++) {
                    Log.d("Files", "FileName:" + files[i].getName());
                    Bitmap bMap = ThumbnailUtils.createVideoThumbnail(files[i].getAbsolutePath(), MediaStore.Video.Thumbnails.MINI_KIND);
                    rowList.add(new String[]{files[i].getAbsolutePath(), BitMapToString(bMap), files[i].getName()});
                    System.out.println(files[i].getAbsolutePath());
                }
            }
            RelativeLayout relativeLayout = (RelativeLayout) getActivity().findViewById(R.id.relative);
            gridview.setAdapter(new CustomAdapter("download", context, rowList, getActivity().getWindowManager(), getActivity().getWindow(), getActivity().getActionBar(), relativeLayout));
            context = getActivity();
        }else{
            Toast.makeText(context, "Please go to Settings to enable Downloads!", Toast.LENGTH_SHORT).show();
        }

Errors:

java.lang.SecurityException: Unsupported path /storage/emulated/0/Test/test.mp4

java.lang.NullPointerException: Attempt to get length of null array ( if(files.length > 0 ) {....} )

Timur Ugurlu
  • 183
  • 1
  • 2
  • 13

0 Answers0