1

Right now I'm currently trying to make a file downloaded from a Uri, but I wanna make it so that if the file has been downloaded it's wont be able to download it anymore. So i decided to use file.exists() to do that, But right now the file.exists() method isn't working for some reason.

Uri uri = Uri.parse("http://kmmc.in/wp-content/uploads/2014/01/lesson2.pdf");

Uri path = Uri.parse("file:///storage/emulated/0/Download/");
String test = uri.getPath();
File file = new File(path.getPath());
File check = new File(path.getPath()+uri.getLastPathSegment());
if (!check.exists()||!check.isFile()) {
  DownloadManager.Request request = new DownloadManager.Request(uri);
  request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
  request.setDestinationInExternalPublicDir(String.valueOf(file), uri.getLastPathSegment());
  Long reference = downloadManager.enqueue(request);
}
Intent intent = new Intent(MainActivity.this,ViewActivity.class);
intent.putExtra("book",""+uri.getLastPathSegment()+"");
startActivity(intent);

I try to do a file check and see if file exist or not, but I always get file doesn't exist even though the file is already downloaded and on the right place. Can someone help me with this?

UkFLSUI
  • 5,509
  • 6
  • 32
  • 47
Adha Fajri
  • 41
  • 2
  • 8
  • 1
    `isFile` will return false itself if it doesn't exist – OneCricketeer Jun 23 '18 at 20:41
  • @cricket_007 I misunderstood the question...deleted my answer – UkFLSUI Jun 23 '18 at 20:45
  • Duplicate of https://stackoverflow.com/questions/21579468/android-file-exists-returns-false-for-existing-file-for-anything-different ? – Alexander Egger Jun 23 '18 at 21:02
  • @alexander.egger Tried folowing that one and still doesn't work – Adha Fajri Jun 23 '18 at 21:24
  • 1
    Aside from the `exists()` question, your approach is silly. The fact that file 'x.foo' exists does NOT mean I downloaded it, nor I downloaded it completely nor I downloaded it correctly. It just means there's file with that name and this is completely different thing and you cannot draw any conclusion from that fact about file origin or content. – Marcin Orlowski Jun 24 '18 at 05:59
  • @MarcinOrlowski Sorry if it's like that but do you have any other approach you'd like to tell me? Please do tell – Adha Fajri Jun 25 '18 at 13:20
  • do NOT prevent re-download. If user wants to, let him do that. You may restrict the IP or say IP class x.x.x.* but do not block the feature. – Marcin Orlowski Jun 25 '18 at 13:37

2 Answers2

1

if (!check.exists()||!check.isFile()) - is not good, as, in theory, it may trigger on a folder that has a name lessons2.pdf. So, isFile() alone should suffice.

Make sure you have all the permissions in Manifest

android.permission.WRITE_EXTERNAL_STORAGE

and that you have asked for read/write permissions runtime, too.

and access the downloads folder through

String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString();

because it may be emulated/0/Downloads or it maybe different, depending on whether the phone is used in a guest mode.

and name

String name = Uri.parse("http://kmmc.in/wp-content/uploads/2014/01/lesson2.pdf").getLastPathSegment();`

then do your check with

File check = new File(path, name);
Eugene Kartoyev
  • 501
  • 4
  • 11
0

Instead of :

request.setDestinationInExternalPublicDir(String.valueOf(file), uri.getLastPathSegment());

Use:

 request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, uri
                    .getLastPathSegment());

Reason:

setDestinationInExternalPublicDir is expecting dirType as your first parameter:

But when set it to String.valueOf(file) it will store the file in /storage/emulated/0/storage/emulated/0/Download/lesson2.pdf instead of storage/emulated/0/Download/. As a result your condition always fails.

Sagar
  • 23,903
  • 4
  • 62
  • 62