사진을 찍던, 갤러리에 보관이 되던 썸네일은 항상 internal 에 보관이 되어있습니다.
리스트에서 사용을 앱의 성능 향상을 위해서는 썸네일을 쓰는 것이 좋습니다.
코드 사용
xml 코드
<Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="갤러리 호출" tools:layout_editor_absoluteX="16dp" tools:layout_editor_absoluteY="16dp" /> <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" />
java 코드
public class MainActivity extends AppCompatActivity { private static final int PICK_FROM_ALBUM = 1; ImageView imageView; //권한 추가 private static final int REQUEST_PERMISSIONS = 1; private static final String[] MY_PERMISSIONS = { Manifest.permission.READ_EXTERNAL_STORAGE }; public boolean hasAllPermissionsGranted() { for (String permission : MY_PERMISSIONS) { if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, MY_PERMISSIONS, REQUEST_PERMISSIONS); return false; } } return true; } @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { if (requestCode == REQUEST_PERMISSIONS) { int index = 0; for (String permission : permissions) { if(permission.equalsIgnoreCase(Manifest.permission.READ_EXTERNAL_STORAGE) && grantResults[index] == PackageManager.PERMISSION_GRANTED) { getGalley(); break; } index++; } return; } super.onRequestPermissionsResult(requestCode, permissions, grantResults); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageView = (ImageView) findViewById(R.id.imageView); Button button = (Button) findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { boolean permission = hasAllPermissionsGranted(); Log.e("test","permission : "+permission); if(!permission) return; } getGalley(); } }); } private void getGalley() { Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); intent.setType(MediaStore.Images.Media.CONTENT_TYPE); startActivityForResult(intent, PICK_FROM_ALBUM); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == PICK_FROM_ALBUM) { if (resultCode == Activity.RESULT_OK) { Log.d("test","data : "+data); if(data != null) { Uri returnImg = data.getData(); if("com.google.android.apps.photos.contentprovider".equals(returnImg.getAuthority())) { for(int i=0;i<returnImg.getPathSegments().size();i++) { String temp = returnImg.getPathSegments().get(i); if(temp.startsWith("content://")) { returnImg = Uri.parse(temp); break; } } } // 썸네일 가져오기 Bitmap bm = getThumbNail(returnImg); imageView.setImageBitmap(bm); } } } } private Bitmap getThumbNail(Uri uri) { Log.d("test","from uri : "+uri); String[] filePathColumn = {MediaStore.Images.Media._ID, MediaStore.Images.Media.DATA, MediaStore.Images.Media.TITLE/*, MediaStore.Images.Media.ORIENTATION*/}; ContentResolver cor = getContentResolver(); //content 프로토콜로 리턴되기 때문에 실제 파일의 위치로 변환한다. Cursor cursor = cor.query(uri, filePathColumn, null, null, null); Bitmap thumbnail = null; if(cursor != null) { cursor.moveToFirst(); int columnIndex = cursor.getColumnIndex(filePathColumn[0]); long ImageId = cursor.getLong(columnIndex); if(ImageId != 0) { BitmapFactory.Options bmOptions = new BitmapFactory.Options(); thumbnail = MediaStore.Images.Thumbnails.getThumbnail( getContentResolver(), ImageId, MediaStore.Images.Thumbnails.MINI_KIND, bmOptions); } else { Toast.makeText(this, "불러올수 없는 이미지 입니다.", Toast.LENGTH_LONG).show(); } cursor.close(); } return thumbnail; } }
작업 전
썸네일 불러오기 후
'Android' 카테고리의 다른 글
[안드로이드] Assets의 Excel data 불러오기 (0) | 2017.04.12 |
---|---|
[안드로이드] Bitmap 모서리 round 처리하기 (0) | 2017.04.12 |
[안드로이드] ActionBar 좌우에 여백 생기는 오류 (0) | 2017.04.12 |
[안드로이드] Hash key 얻기 (java code) (0) | 2017.04.12 |
[안드로이드] ImageView 안에 있는 resource 컬러값 변경 (0) | 2017.04.12 |