Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] java – how to check whether file exists or not using content uri

Posted on November 14, 2022

Solution 1 :

Method 1 :-
Ok, Since Android team decided to deprecate getExternalStorageDirectory for security reason. Lot of things are not properly addressed by them.

for the question I don’t find any proper answer. Anyhow here is a workaround by using deprecated method.

        var uri = Uri.parse("content://com.mi.android.globalFileexplorer.myprovider/external_files/KannadaGeeta/11CHAPTER10.mp3")
        var vidPath = uri.path
        vidPath = vidPath?.replace("external_files",Environment.getExternalStorageDirectory().toString())
        val file = File(vidPath)

        if (file.exists()) {
            println("log yes $file")
        } else {
            println("log no $file")
        }

This will work fine, but using this is left to you because of deprecated method.

Method 2 :-
one more way is to use ACTION_OPEN_DOCUMENT
then in onActivityResult it must add

val contentResolver = applicationContext.contentResolver
val takeFlags: Int = Intent.FLAG_GRANT_READ_URI_PERMISSION or       
                     Intent.FLAG_GRANT_WRITE_URI_PERMISSION
contentResolver.takePersistableUriPermission(selectedVideoUri!!, takeFlags)

Then, finally with the content uri which is not expired, one can check for file existance.

DocumentFile.fromSingleUri(Uri.parse(contentUriStringfromdatabase)).exists()

Solution 2 :

 File file = new File(URI.create("your uri").getPath());
 if (file.exists()) {
  //Do something`enter code here`
 }

Also you need the permission:

<uses-permission android_name="android.permission.WRITE_EXTERNAL_STORAGE" />

Problem :

I have a content uri of an audio file, which I got it from chooser intent.

content://com.mi.android.globalFileexplorer.myprovider/external_files/KannadaGeeta/11CHAPTER10.mp3

I just saved the above uri on database.

Now, when user opens my app again. I need to inform him whether the last mp3 file which he selected exists or not. If exists then only I must allow him to continue else I need to inform to download the file again.

but I stuck here completely, I’m not getting how to check a file exist or not with its content uri.

Kindly, help me to solve my issue.

Comments

Comment posted by Vikas Acharya

this will not working. because when I print the

Comment posted by Ilja Tomilovych

try to replace content: with file:

Comment posted by Vikas Acharya

i didn’t understood, what are you saying. give example.

READ  [ANSWERED] android - error: cannot access RemoteMessage when injecting on FirebaseMessagingService
Powered by Inline Related Posts

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress