Created
November 11, 2018 05:42
-
-
Save taktamur/4486f124f8a766bd4153ca50956c3cfe to your computer and use it in GitHub Desktop.
Android 画像一覧表示(遅い)
This file contains hidden or 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 jp.paming.listsample | |
import android.Manifest | |
import android.content.ContentResolver | |
import android.content.ContentUris | |
import android.content.Context | |
import android.content.pm.PackageManager | |
import android.databinding.DataBindingUtil | |
import android.net.Uri | |
import android.support.v7.app.AppCompatActivity | |
import android.os.Bundle | |
import android.provider.MediaStore | |
import android.support.v4.app.ActivityCompat | |
import android.support.v4.content.ContextCompat | |
import android.support.v7.widget.LinearLayoutManager | |
import android.support.v7.widget.RecyclerView | |
import android.util.Log | |
import android.view.LayoutInflater | |
import android.view.ViewGroup | |
import android.widget.Toast | |
import jp.paming.listsample.databinding.RawPhotoBinding | |
import kotlinx.android.synthetic.main.activity_recycler.* | |
import java.util.* | |
import android.graphics.Bitmap | |
class PhotoListActivity : AppCompatActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_photo_list) | |
// 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 expanation 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. | |
Toast.makeText(this, "パーミッションがOFFになっています。", Toast.LENGTH_SHORT).show(); | |
} else { | |
// No explanation needed, we can request the permission. | |
val permission = Manifest.permission.READ_EXTERNAL_STORAGE | |
ActivityCompat.requestPermissions(this, | |
arrayOf(permission), | |
12345) | |
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an | |
// app-defined int constant. The callback method gets the | |
// result of the request. | |
} | |
}else { | |
val photoDataList = read() | |
Log.d("photoDataList","${photoDataList}") | |
recycleView.layoutManager = LinearLayoutManager(this) | |
recycleView.adapter = PhotoListActivity.MyRecycleAdapter(this, | |
contentResolver, | |
photoDataList) | |
} | |
} | |
// RecyclerView.ViewHolderを継承した自作ViewHolder | |
// 親クラスの初期化にはBinding.rootで親Viewを渡し、 | |
// 子クラスはbindingを保持する | |
class PhotoDataViewHolder(val binding: RawPhotoBinding) : RecyclerView.ViewHolder(binding.root) | |
class MyRecycleAdapter(context: Context,val contentResolver:ContentResolver, val list:List<PhotoData>) : RecyclerView.Adapter<PhotoDataViewHolder>() { | |
private val layoutInflater = LayoutInflater.from(context) | |
override fun getItemCount(): Int { | |
return list.size | |
} | |
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PhotoDataViewHolder { | |
val binding: RawPhotoBinding = DataBindingUtil.inflate(layoutInflater,R.layout.raw_photo, parent, false) | |
return PhotoDataViewHolder(binding) | |
} | |
override fun onBindViewHolder(holder: PhotoDataViewHolder, position: Int) { | |
// ここではモデルに値をセットしている | |
// →DataBindingにより、自動でViewに反映される | |
holder.binding.date = list[position].dateString | |
val bmp = MediaStore.Images.Media | |
.getBitmap(contentResolver, list[position].uri) | |
holder.binding.imageView.setImageBitmap(bmp) | |
} | |
} | |
private fun read(): List<PhotoData> { | |
//レコードの取得 | |
// var cursor = managedQuery( | |
// MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null, | |
// null); | |
val proj = arrayOf( | |
MediaStore.Images.Media._ID, | |
MediaStore.Images.Media.TITLE, | |
MediaStore.Images.Media.DATA | |
) | |
val cursor = getContentResolver().query( | |
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, | |
// MediaStore.Files.getContentUri("external"), | |
// proj, | |
null, | |
null, | |
null, | |
null | |
) ?: return emptyList() | |
// cursor.moveToFirst() | |
Log.d("column","${cursor.columnCount}") | |
for( i in (0 until cursor.columnCount) ){ | |
Log.d("column_name","${cursor.getColumnName(i)}") | |
} | |
Log.d("count", "${cursor.count}") | |
val ret = ArrayList<PhotoData>() | |
if (cursor.moveToFirst()){ | |
do { | |
val id = cursor.getLong(cursor.getColumnIndex(MediaStore.Images.Media._ID)) | |
val added = cursor.getLong(cursor.getColumnIndex(MediaStore.Images.Media.DATE_ADDED)) | |
val date = Date(added) | |
val bmpUri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id); | |
val photoData = PhotoData(bmpUri,date) | |
ret.add(photoData) | |
} while (cursor.moveToNext()) | |
} | |
cursor.close() | |
return ret | |
} | |
} | |
class PhotoData(val uri:Uri, val date: Date){ | |
var dateString:String = "" | |
get() = date.toString() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment