val REQUEST_TAKE_PHOTO = 1
val REQUEST_SELECT_PICTURE = 2
private fun dispatchTakePictureIntent()
{
val takePictureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
takePictureIntent.resolveActivity(getPackageManager())?.let {
var photoFile:File? = null
try {
photoFile = createImageFile();
} catch (ex:IOException) {
// Error occurred while creating the File
ex.printStackTrace()
}
// Continue only if the File was successfully created
if (photoFile != null) {
val photoURI: Uri = FileProvider.getUriForFile(this,
"lonepine.jis.playcardforkids",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}
private fun galleryAddPic() {
val mediaScanIntent = Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE)
val f = File(mCurrentPhotoPath)
val contentUri = Uri.fromFile(f)
mediaScanIntent.data = contentUri
this.sendBroadcast(mediaScanIntent)
}
private fun selectImageFromGallery()
{
// 사진 선택
val intent = Intent()
intent.type = "image/*"
// intent.action = Intent.ACTION_GET_CONTENT
intent.action = Intent.ACTION_PICK
startActivityForResult(Intent.createChooser(intent,
"Select Picture"), REQUEST_SELECT_PICTURE)
}
private fun getPath(uri: Uri?): String? {
// uri가 null일경우 null반환
if (uri == null) {
return null
}
// 미디어스토어에서 유저가 선택한 사진의 URI를 받아온다.
val projection = arrayOf(MediaStore.Images.Media.DATA)
val cursor = managedQuery(uri, projection, null, null, null)
if (cursor != null) {
val column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA)
cursor.moveToFirst()
return cursor.getString(column_index)
}
// URI경로를 반환한다.
return uri.path
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == REQUEST_TAKE_PHOTO) {
Log.d("jis","resultCode >> ${resultCode}")
val resultFile = File(mCurrentPhotoPath)
Log.d("jis","resultFile.exists() >> ${resultFile.exists()}")
if (resultCode == Activity.RESULT_OK)
{
galleryAddPic()
mCurrentPhotoPath?.let {
runOnIoScheduler {
cardDao.update(vpa.items.get(vp.currentItem).seq,it)
runOnUiThread {
vpa.notifyDataSetChanged()
}
}
}
}
else
{
if (resultFile.exists())
{
resultFile.delete()
}
}
}
else if (requestCode == REQUEST_SELECT_PICTURE)
{
getPath(data?.getData())?.let {
val resultFile = File(it)
if (resultFile.exists())
{
runOnIoScheduler {
cardDao.update(vpa.items.get(vp.currentItem).seq,it)
runOnUiThread {
vpa.notifyDataSetChanged()
}
}
}
}
}
}
@Throws(IOException::class)
private fun createImageFile(): File {
// Create an image file name
val timeStamp = SimpleDateFormat("yyyyMMdd_HHmmss").format(Date())
val imageFileName = "JPEG_" + timeStamp + "_"
val storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES)
val image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
)
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = image.getAbsolutePath()
return image
}