sdcard 画像パス サムネイル取得 ContentResolver MediaStore.Images.Media
uri file path 変換
/////////uri to path
String path = uri.getPath();
/////////path to file
File file = new File(path);
/////////file to uri
Uri uri2 = Uri.fromFile(file);
MediaStore.Images.Mediaからパスの取り方。
DATE_MODIFIEDは使えない。LINEから画像保存、コピーしてきたものはNULL
DATE_ADDEDはちゃんと日付がはいってる。
Uri image_uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
Cursor cursor = koviewActivity.getContentResolver().query(image_uri,
null,
"date_added >= ?" + " and " + "date_added < ?",
new String{str_stadate, str_enddate},
"date_added DESC");
cursor.moveToFirst();
int numrow = cursor.getCount();
Calendar cal=Calendar.getInstance();
String wk_path
=currsor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
String w = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media. DATE_MODIFIED ));
String w = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media. DATE_ADDED ));
//////////////////////////////////ファイル名指定してサムネイルget
/////////////////////galleryから画像を選ばして、表示するときなど。。。。
if (resultCode == Activity.RESULT_OK) {
try {
ContentResolver cr = getContentResolver();
String columns = {MediaStore.Images.Media.DATA };
Cursor c = cr.query(data.getData(), columns, null, null, null);
c.moveToFirst();
File fl = new File(c.getString(0)); //fairu パス名称
ip_path = fl +""; //画像ファイルのパス名称
c.close();
// サムネイルの取得********************************************
ContentResolver resolver = getContentResolver();
Cursor c2 = null;
c2= resolver.query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,// データの種類
null, // 項目(null 全項目)
MediaStore.Images.ImageColumns.DATA + " = ?",// フィルタ条件(null フィルタなし)
new String{ip_path},// フィルタ用パラメータ
null// ソート
);
c2.moveToFirst();
// サムネイルの取得
long id = c2.getLong(c2.getColumnIndex("_id"));
Bitmap bmp = MediaStore.Images.Thumbnails.getThumbnail(
resolver,
id,
MediaStore.Images.Thumbnails.MINI_KIND,//MICRO_KIND96*96 MINI_KIND 512×384
null);
bitmap_dsp(iv1, bmp,getdegree(ip_path));
c2.close();
//最終更新日付取得表示 ファイル名表示
Calendar cal=Calendar.getInstance();
cal.setTimeInMillis( fl.lastModified() );
int w_yyyy = cal.get(Calendar.YEAR);
int w_mm = cal.get(Calendar.MONTH);
int w_dd = cal.get(Calendar.DATE);
String str_yyyy = w_yyyy+"";
String str_mm = string_change(w_mm);
String str_dd = string_change(w_dd);
TextView tv2 = (TextView)findViewById(R.id.textView2);
TextView tv3 = (TextView)findViewById(R.id.textView3);
tv2.setText(str_yyyy+"/"+str_mm+"/"+str_mm);
tv3.setText(ip_path);
} catch (Exception e) {
}
}
private int getdegree(String path) {
// TODO 自動生成されたメソッド・スタブ
int wdegree =0;
try {
ExifInterface exifInterface = new ExifInterface(path);
int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
wdegree = 90;
} else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
wdegree = 180;
} else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
wdegree = 270;
}
if (wdegree != 0) {//これバグ!!!!!!!!
// exifInterface.setAttribute(ExifInterface.TAG_ORIENTATION, "0");
// exifInterface.saveAttributes();
}
} catch (IOException e) {
wdegree = -1;
}
return wdegree;
}////////////////////////////////getdegree
private void bitmap_dsp(ImageView iv1, Bitmap bmp, int get6_degree) {
// TODO 自動生成されたメソッド・スタブ
Matrix matrix = new Matrix();
int width = bmp.getWidth();
int height = bmp.getHeight();
if (get6_degree != -1){
matrix.postRotate( get6_degree); // 回転させる角度を指定
bmp = Bitmap.createBitmap( bmp, 0, 0, width, height, matrix, true);
}else{
}
iv1.setImageBitmap( bmp);
}/////////////////////////////////bitmapdsp
private String string_change(int input_dt) {
// TODO 自動生成されたメソッド・スタブ
DecimalFormat df = new DecimalFormat("00");
String return_data = df.format(input_dt);
return return_data;
}////////////////////////////////////////string_change
////////////////////////////////////画像gridVIEW 作成
File file = Environment.getExternalStorageDirectory();//SDカードにアクセス
Uri image_uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
Cursor cursor = getContentResolver().query(image_uri, null, null, null, null);
ContentResolver cr = getContentResolver();
cursor.moveToFirst();
int numrow = cursor.getCount();
// String path = new String[cursor.getCount()];
for (int i = 0; i < numrow; i++) {///////////////////////////////////////////////////for start
// path[i] = cursor.getString(1);
String path = cursor.getString(1);
File fl = new File(path);
if(path.endsWith("jpg") || path.endsWith("jpeg") ){
long id = cursor.getLong(cursor.getColumnIndexOrThrow("_id"));
Bitmap bmp = MediaStore.Images.Thumbnails.getThumbnail(
cr, id, MediaStore.Images.Thumbnails.MICRO_KIND, null);
list_data.add(new Book(
0,
"",
"",
"",
fl,bmp));
}else{
}//////////////////////////if else end
cursor.moveToNext();
}///////////////////////////////////////////////////////for end
cursor.close();
gv= (GridView) findViewById(R.id.gridView1);
adapter = new BookAdapter(
this,
R.layout.seiri_cell, // 1行分のレイアウトファイル
0, // 上記レイアウト内のテキスト表示箇所のId
list_data // 表示対象のデータ
);
gv.setAdapter(adapter);
cursor.getString(引数) の引数を変えると色々なデータが取ってこれます。
引数 | データ内容 | 実データ(例) |
---|---|---|
0 | インデックス | 0 |
1 | ファイルパス | /sdcard/DCIM/Camera/test.jpg |
2 | ファイルサイズ | 337243 |
3 | ファイル名(拡張子付き) | test.jpg |
4 | MIMEタイプ | image/jpeg |
5 | ファイル名(拡張子なし) | test |
6 | 謎データ*1 | 1256906217 |
7 | 謎データ*2 |
1249145692
|