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:
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 >= 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() } } }
Anh chưa thấy em tạo file, em thử thay đoạn:
Thành thế này xem sao:
Code cũ của e đã tạo file trong thẻ nhớ, nhưng trên điện thoại lại không hiện trong thư mục ảnh đó a, phải reset lại điện thoại mới thấy.
Vậy có nghĩa là em đã khắc phục được lỗi rồi nhỉ?
Dạ chưa ạ, mỗi khi e chụp xong, muốn gửi hình qua zalo thì không thấy hình, phải reset lại điện thoại mới được.
Khi em chạy em co thấy lỗi gì bắn ra ở
catch (e: Throwable)
không nhỉ? Nếu không em thử thêm Log.e xem sao nhé, muốn trả lời câu hỏi của em anh sẽ cần tạo project, code, tái hiện nên sẽ không nhanh được 🙁