Avatar
0
Cường Ngô Beginner
Cường Ngô Beginner
Refresh gallery
Mọi người cho e hỏi vấn đề này với, e muốn chụp ảnh cái textview lại, nhưng khi code chụp xong rồi thì vào thu mục ảnh lại không thấy hiện ảnh lên ạ. Code: <p> </p> <pre> import android.content.ContentValues import android.content.Context import android.content.Intent import android.graphics.Bitmap import android.net.Uri import android.os.Build import android.os.Environment import android.provider.MediaStore import android.text.format.DateFormat import android.widget.Toast import java.io.File import java.io.FileOutputStream import java.io.OutputStream import java.util.* data class TakeSS(val bitmap: Bitmap, val context: Context, val img_name: String) { fun savescrolltoGallery() { val date = Date() val now = DateFormat.format("yyyyMMddhhmmss", date) val fos: OutputStream try { if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.Q) { val resolver = context.contentResolver val contentValue = ContentValues() contentValue.put(MediaStore.MediaColumns.DISPLAY_NAME, "$img_name.jpg") contentValue.put(MediaStore.MediaColumns.MIME_TYPE, "image/jpg") contentValue.put( MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_PICTURES + File.separator + "Huyền Học" ) val imageUri = resolver?.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValue) fos = resolver?.openOutputStream(Objects.requireNonNull(imageUri)!!)!! bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos) Toast.makeText(context, "$img_name.jpg", Toast.LENGTH_LONG).show() } else{ TakeSS(bitmap) } } catch (e: Exception) { Toast.makeText(context, e.toString(), Toast.LENGTH_LONG).show() } } fun TakeSS(contrain_content: Bitmap) { val date = Date() val now = DateFormat.format("yyyyMMddhhmmss", date) try { // image naming and path to include sd card appending name you choose for file val mPath = Environment.getExternalStorageDirectory() .toString() + "/Huyền Học/$img_name.jpg" val imageFile = File(mPath) imageFile.parentFile!!.mkdirs() val outputStream = FileOutputStream(imageFile) val quality = 100 contrain_content.compress( Bitmap.CompressFormat.JPEG, quality, outputStream ) outputStream.flush() outputStream.close() val uri = Uri.fromFile(imageFile) Toast.makeText(context, img_name, Toast.LENGTH_SHORT).show() } catch (e: Throwable) { // Several error may come out with file handling or DOM e.printStackTrace() } } } </pre>
Answer