Created
June 2, 2020 18:51
-
-
Save yrezgui/8da567729ba0337e989ed1a029b51569 to your computer and use it in GitHub Desktop.
FileProvider for Meghan
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.android.samples.fileprovidertest | |
import android.os.Bundle | |
import androidx.activity.viewModels | |
import androidx.appcompat.app.AppCompatActivity | |
import com.android.samples.fileprovidertest.databinding.ActivityMainBinding | |
import kotlinx.coroutines.CoroutineScope | |
import kotlinx.coroutines.Dispatchers | |
import kotlinx.coroutines.launch | |
import okhttp3.OkHttpClient | |
const val SAMPLE_IMAGE_URL = "https://upload.wikimedia.org/wikipedia/en/a/a9/Example.jpg" | |
class MainActivity : AppCompatActivity() { | |
private lateinit var binding: ActivityMainBinding | |
private val viewModel: MainViewModel by viewModels() | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
binding = ActivityMainBinding.inflate(layoutInflater) | |
setContentView(binding.root) | |
binding.addImage.setOnClickListener { | |
CoroutineScope(Dispatchers.Main).launch { | |
viewModel.addImage() | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.android.samples.fileprovidertest | |
import android.app.Application | |
import androidx.lifecycle.AndroidViewModel | |
import androidx.lifecycle.viewModelScope | |
import kotlinx.coroutines.Dispatchers | |
import kotlinx.coroutines.launch | |
import kotlinx.coroutines.withContext | |
import okhttp3.OkHttpClient | |
import okhttp3.Request | |
class MainViewModel(application: Application) : AndroidViewModel(application) { | |
private val httpClient by lazy { OkHttpClient() } | |
private var counter = 1 | |
fun addImage() { | |
val contentResolver = getApplication<Application>().contentResolver | |
val newImageUri = insertImage(contentResolver, counter) | |
?: return println("Error couldn't insert new image entry") | |
val outputStream = contentResolver.openOutputStream(newImageUri, "w") | |
?: return println("Error couldn't open output stream") | |
viewModelScope.launch { | |
val imageContent = downloadImage() ?: return@launch println("Error downloading image") | |
outputStream.write(imageContent) | |
} | |
finishEditingImage(contentResolver, newImageUri) | |
} | |
private suspend fun downloadImage(): ByteArray? { | |
val request = Request.Builder().url(SAMPLE_IMAGE_URL).build() | |
return withContext(Dispatchers.IO) { | |
val response = httpClient.newCall(request).execute() | |
return@withContext response.body?.bytes() | |
} | |
// if (!response.isSuccessful) { | |
// throw IOException("Failed to download file: $response") | |
// } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.android.samples.fileprovidertest | |
import android.content.ContentResolver | |
import android.content.ContentValues | |
import android.net.Uri | |
import android.os.Build | |
import android.provider.MediaStore | |
fun insertImage(contentResolver: ContentResolver, counter: Int): Uri? { | |
val newImageDetails = ContentValues().apply { | |
put(MediaStore.Images.Media.DISPLAY_NAME, "nice_pic_$counter.jpg") | |
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { | |
put(MediaStore.Images.Media.IS_PENDING, 1) | |
} | |
} | |
return contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, newImageDetails) | |
} | |
fun finishEditingImage(contentResolver: ContentResolver, imageUri: Uri) { | |
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { | |
val updatedDetails = ContentValues().apply { | |
put(MediaStore.Images.Media.IS_PENDING, 0) | |
} | |
contentResolver.update(imageUri, updatedDetails, null, null) | |
} | |
} | |
//private fun editImageApi21(contentResolver: ContentResolver) { | |
// val options = BitmapFactory.Options() | |
// options.inMutable = true | |
// val bmp = BitmapFactory.decodeByteArray(data, 0, data.length, options) | |
//} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment