PackageManager ; import android. Bitmap ; import android. Toast ; import android. Drawable ; import android. Uri ; import android. Environment ; import android. MediaStore ; import android.
ActivityCompat ; import android. ContextCompat ; import android. Others only request them where they are actually needed. If your app has a specific place where file downloads occur, you can just request permission there.
First, we need functions to check the permissions and the request result. Note that only devices with Marshmallow API 23 or later need permissions; earlier devices had them by default.
If permission needs to be requested, our return should set the click listener when permission is granted. Either you can force the download to occur where you want with the name you provide, or you can let the user decide the name and location. I use the latter case here; the click will start an intent for creating a document with some defaults set.
Downloading the file opens the output stream to the URI given and dispatches the download file coroutine. The download itself is handled on the IO thread, but the emitter results are handled on the Main thread. This allows for the correct asynchronous updates in this case, updating the progress bar. The viewFile function takes the same URI given and opens it in an intent, if able. If there are multiple applications where the file can be viewed, it presents a chooser.
The code for this can be found on my Github repo: kotlin-file-downloading. You will also need to include a dependency on the support library. Note: Avoid using aar in your dependencies whenever possible.
If you use Maven you can add a dependency on Glide as well. Again, you will also need to include a dependency on the support library. That said, most applications either load images on the device in DCIM, Pictures or elsewhere on the SD card or load images from the internet. Active 2 years, 1 month ago. Viewed 10k times. Ezio Ezio 2, 2 2 gold badges 23 23 silver badges 35 35 bronze badges. The code seems fine, have you tried other urls? Add a comment. Active Oldest Votes.
GVillani82 GVillani82 I believe that approach will work, the image is encoded into a Base64 string, you need to convert it to a byte array then to a bitmap. Thank you it worked, can you please explain what was the real issue and how you fixed it — Ezio.
You are welcome Ezio I don't know if there is a way for loading directly a data-Uri. So, I would suggest to use that approach: converting your image into a byte array and then using the method load byte[] model. Sdghasemi Sdghasemi 4, 1 1 gold badge 29 29 silver badges 35 35 bronze badges.
0コメント