fix #6 downlaod image
This commit is contained in:
@@ -2,6 +2,8 @@
|
|||||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
xmlns:tools="http://schemas.android.com/tools">
|
xmlns:tools="http://schemas.android.com/tools">
|
||||||
<uses-permission android:name="android.permission.INTERNET"/>
|
<uses-permission android:name="android.permission.INTERNET"/>
|
||||||
|
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
|
||||||
|
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
|
||||||
<application
|
<application
|
||||||
android:allowBackup="true"
|
android:allowBackup="true"
|
||||||
android:dataExtractionRules="@xml/data_extraction_rules"
|
android:dataExtractionRules="@xml/data_extraction_rules"
|
||||||
|
|||||||
@@ -1,13 +1,19 @@
|
|||||||
package com.dreamteam.timelapse
|
package com.dreamteam.timelapse
|
||||||
|
|
||||||
|
import android.content.ContentValues
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
|
import android.content.Intent
|
||||||
|
import android.content.Intent.createChooser
|
||||||
import android.graphics.Bitmap
|
import android.graphics.Bitmap
|
||||||
|
import android.os.Environment
|
||||||
|
import android.provider.MediaStore
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import android.view.LayoutInflater
|
import android.view.LayoutInflater
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
import android.widget.ImageView
|
import android.widget.ImageView
|
||||||
import android.widget.Toast
|
import android.widget.Toast
|
||||||
|
import androidx.core.content.ContextCompat.startActivity
|
||||||
import androidx.recyclerview.widget.RecyclerView
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
import com.bumptech.glide.Glide
|
import com.bumptech.glide.Glide
|
||||||
import com.bumptech.glide.request.target.SimpleTarget
|
import com.bumptech.glide.request.target.SimpleTarget
|
||||||
@@ -15,7 +21,7 @@ import java.io.File
|
|||||||
import java.io.FileOutputStream
|
import java.io.FileOutputStream
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
import com.bumptech.glide.request.transition.Transition
|
import com.bumptech.glide.request.transition.Transition
|
||||||
|
import java.io.OutputStream
|
||||||
|
|
||||||
|
|
||||||
class ImageAdapter(private val imageUrls: List<String>) : RecyclerView.Adapter<ImageAdapter.ImageViewHolder>() {
|
class ImageAdapter(private val imageUrls: List<String>) : RecyclerView.Adapter<ImageAdapter.ImageViewHolder>() {
|
||||||
@@ -36,26 +42,50 @@ class ImageAdapter(private val imageUrls: List<String>) : RecyclerView.Adapter<I
|
|||||||
Glide.with(holder.itemView.context)
|
Glide.with(holder.itemView.context)
|
||||||
.load(imageUrl) // Charge l'image via Glide
|
.load(imageUrl) // Charge l'image via Glide
|
||||||
.into(holder.imageView) // Affiche l'image dans l'ImageView
|
.into(holder.imageView) // Affiche l'image dans l'ImageView
|
||||||
|
holder.itemView.setOnClickListener { view ->
|
||||||
|
downloadImage(imageUrl, holder.itemView.context)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
private fun downloadImage(url: String, context: Context) {
|
private fun downloadImage(url: String, context: Context) {
|
||||||
|
Toast.makeText(context, "Downloading...", Toast.LENGTH_SHORT).show()
|
||||||
|
|
||||||
Glide.with(context)
|
Glide.with(context)
|
||||||
.asBitmap()
|
.asBitmap()
|
||||||
.load(url)
|
.load(url)
|
||||||
.into(object : SimpleTarget<Bitmap>() {
|
.into(object : SimpleTarget<Bitmap>() {
|
||||||
override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
|
override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
|
||||||
// Save the image to the app's internal storage
|
// Save the image to the Downloads folder using MediaStore
|
||||||
val file = File(context.filesDir, "downloaded_image.jpg")
|
val contentValues = ContentValues().apply {
|
||||||
|
put(MediaStore.Images.Media.DISPLAY_NAME, "downloaded_image.jpg") // File name
|
||||||
|
put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg")
|
||||||
|
put(MediaStore.Images.Media.RELATIVE_PATH, "Pictures/") // Save to the Downloads folder
|
||||||
|
}
|
||||||
|
|
||||||
|
// Insert the image into MediaStore
|
||||||
|
val uri = context.contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues)
|
||||||
|
|
||||||
|
uri?.let { imageUri ->
|
||||||
try {
|
try {
|
||||||
val fileOutputStream = FileOutputStream(file)
|
// Open an output stream to write the image to the Downloads folder
|
||||||
resource.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream)
|
val outputStream: OutputStream? = context.contentResolver.openOutputStream(imageUri)
|
||||||
fileOutputStream.flush()
|
outputStream?.use {
|
||||||
fileOutputStream.close()
|
resource.compress(Bitmap.CompressFormat.JPEG, 100, it) // Save the image as JPEG
|
||||||
Toast.makeText(context, "Image downloaded successfully", Toast.LENGTH_SHORT).show()
|
Toast.makeText(context, "Image downloaded successfully", Toast.LENGTH_SHORT).show()
|
||||||
} catch (e: IOException) {
|
val intent = Intent(Intent.ACTION_VIEW).apply {
|
||||||
|
// Use the Uri where the image was saved
|
||||||
|
setDataAndType(uri, "image/jpeg") // MIME type for images
|
||||||
|
flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
|
||||||
|
}
|
||||||
|
//intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||||
|
context.startActivity(createChooser(intent, "Open image with"))
|
||||||
|
|
||||||
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
e.printStackTrace()
|
e.printStackTrace()
|
||||||
Toast.makeText(context, "Error downloading image", Toast.LENGTH_SHORT).show()
|
Toast.makeText(context, "Error downloading image", Toast.LENGTH_SHORT).show()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
override fun getItemCount(): Int = imageUrls.size
|
override fun getItemCount(): Int = imageUrls.size
|
||||||
|
|||||||
Reference in New Issue
Block a user