Created
November 6, 2017 07:33
-
-
Save mockiemockiz/44e6b8395b6f98ce0b5d3d4b4b3f88f1 to your computer and use it in GitHub Desktop.
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 tigaer.inventory | |
import android.Manifest | |
import android.annotation.SuppressLint | |
import android.app.Activity | |
import android.content.Context | |
import android.content.DialogInterface | |
import android.content.Intent | |
import android.content.pm.PackageManager | |
import android.database.Cursor | |
import android.graphics.Bitmap | |
import android.os.AsyncTask | |
import android.os.Build | |
import android.os.Bundle | |
import android.provider.MediaStore | |
import android.support.v4.app.ActivityCompat | |
import android.support.v4.content.ContextCompat | |
import android.support.v7.app.AlertDialog | |
import android.support.v7.app.AppCompatActivity | |
import android.util.Log | |
import android.view.LayoutInflater | |
import android.view.View | |
import android.view.ViewGroup | |
import android.widget.BaseAdapter | |
import android.widget.Button | |
import android.widget.CheckBox | |
import android.widget.GridView | |
import android.widget.ImageView | |
import kotlinx.android.synthetic.main.custom_gallery.* | |
/** | |
* Created by User on 06/11/2017. | |
*/ | |
class CustomPhotoGalleryActivity : AppCompatActivity() { | |
private var grdImages: GridView? = null | |
private var btnSelect: Button? = null | |
private val imageAdapter: ImageAdapter? = null | |
private var arrPath: Array<Any> = emptyArray() | |
private val thumbnailsselection: BooleanArray? = null | |
private val ids: IntArray? = null | |
private var count: Int = 0 | |
private val MY_PERMISSIONS_REQUEST_READ_CONTACTS = 1 | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.custom_gallery) | |
// Here, thisActivity is the current activity | |
if (ContextCompat.checkSelfPermission(this, | |
Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { | |
// Should we show an explanation? | |
if (ActivityCompat.shouldShowRequestPermissionRationale(this, | |
Manifest.permission.READ_EXTERNAL_STORAGE)) { | |
// Show an explanation to the user *asynchronously* -- don't block | |
// this thread waiting for the user's response! After the user | |
// sees the explanation, try again to request the permission. | |
} else { | |
// No explanation needed, we can request the permission. | |
ActivityCompat.requestPermissions(this, | |
arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE), | |
MY_PERMISSIONS_REQUEST_READ_CONTACTS) | |
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an | |
// app-defined int constant. The callback method gets the | |
// result of the request. | |
val builder: AlertDialog.Builder | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { | |
builder = AlertDialog.Builder(this, android.R.style.Theme_Material_Dialog_Alert) | |
} else { | |
builder = AlertDialog.Builder(this) | |
} | |
} | |
} | |
grdImages = findViewById<View>(R.id.grdImages) as GridView | |
btnSelect = findViewById<View>(R.id.btnSelect) as Button | |
val columns = arrayOf(MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID) | |
val orderBy = MediaStore.Images.Media._ID | |
val imagecursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null, null, orderBy) | |
val image_column_index = imagecursor.getColumnIndex(MediaStore.Images.Media._ID) | |
this.count = imagecursor.getCount() | |
// arrPath = Array<Any>?(this.count) | |
val ids = IntArray(count) | |
// thumbnailsselection = BooleanArray(this.count) | |
for (i in 0 until this.count) { | |
imagecursor.moveToPosition(i) | |
ids[i] = imagecursor.getInt(image_column_index) | |
val dataColumnIndex = imagecursor.getColumnIndex(MediaStore.Images.Media.DATA) | |
arrPath[i] = imagecursor.getString(dataColumnIndex) | |
} | |
val imageAdapter = ImageAdapter() | |
grdImages!!.adapter = imageAdapter | |
imagecursor.close() | |
} | |
/** | |
* This method used to set bitmap. | |
* | |
* @param iv represented ImageView | |
* @param id represented id | |
*/ | |
private fun setBitmap(iv: ImageView?, id: Int) { | |
object : AsyncTask<Void, Void, Bitmap>() { | |
override fun doInBackground(vararg params: Void): Bitmap { | |
return MediaStore.Images.Thumbnails.getThumbnail(applicationContext.contentResolver, id.toLong(), MediaStore.Images.Thumbnails.MICRO_KIND, null) | |
} | |
override fun onPostExecute(result: Bitmap) { | |
super.onPostExecute(result) | |
iv!!.setImageBitmap(result) | |
} | |
}.execute() | |
} | |
inner class ImageAdapter : BaseAdapter() { | |
private val mInflater: LayoutInflater | |
init { | |
mInflater = getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater | |
} | |
override fun getCount(): Int { | |
return count | |
} | |
override fun getItem(position: Int): Any { | |
return position | |
} | |
override fun getItemId(position: Int): Long { | |
return position.toLong() | |
} | |
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View { | |
var convertView = convertView | |
val holder: ViewHolder | |
if (convertView == null) { | |
holder = ViewHolder() | |
convertView = mInflater.inflate(R.layout.custom_gallery_item, null) | |
holder.imgThumb = convertView!!.findViewById<View>(R.id.imgThumb) as ImageView | |
holder.chkImage = convertView.findViewById<View>(R.id.chkImage) as CheckBox | |
convertView.tag = holder | |
} else { | |
holder = convertView.tag as ViewHolder | |
} | |
holder.chkImage!!.id = position | |
holder.imgThumb!!.id = position | |
holder.chkImage!!.setOnClickListener { v -> | |
val cb = v as CheckBox | |
val id = cb.id | |
if (thumbnailsselection!![id]) { | |
cb.isChecked = false | |
thumbnailsselection[id] = false | |
} else { | |
cb.isChecked = true | |
thumbnailsselection[id] = true | |
} | |
} | |
holder.imgThumb!!.setOnClickListener { | |
val id = holder.chkImage!!.id | |
if (thumbnailsselection!![id]) { | |
holder.chkImage!!.isChecked = false | |
thumbnailsselection[id] = false | |
} else { | |
holder.chkImage!!.isChecked = true | |
thumbnailsselection[id] = true | |
} | |
} | |
try { | |
setBitmap(holder.imgThumb, ids!![position]) | |
} catch (e: Throwable) { | |
} | |
holder.chkImage!!.isChecked = thumbnailsselection!![position] | |
holder.id = position | |
return convertView | |
} | |
} | |
/** | |
* Inner class | |
* | |
* @author tasol | |
*/ | |
internal inner class ViewHolder { | |
var imgThumb: ImageView? = null | |
var chkImage: CheckBox? = null | |
var id: Int = 0 | |
} | |
companion object { | |
private val MY_PERMISSIONS_REQUEST_READ_CONTACTS = 1 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment