라이브러리에서 사진 선택하기
Intent libraryIntent = new Intent(); libraryIntent.setType("image/*"); libraryIntent.setAction(Intent.ACTION_PICK); // 아래방법의 경우는 연계하는 Activity가 여러개 나타날 수 있다. // 갤러리 + 파일익스플로러등등. //libraryIntent.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(libraryIntent, 2);사진 선택은 다른 Activity 가 실행되므로 결과는 아래와 같이 받는다.
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode != RESULT_OK) { Toast.makeText(this, "사진 선택에 실패했습니다.", Toast.LENGTH_SHORT).show(); return; } // 1개의 Activity에 여러개의 결과가 돌아올때는 requestCode로 구분한다. if (requestCode == 2) { try { InputStream is = getContentResolver().openInputStream(data.getData()); bitmap = BitmapFactory.decodeStream(is); is.close(); if (bitmap != null) { imageView.setImageBitmap(bitmap); } } catch (Exception e) { e.printStackTrace(); } } }
카메라 기동하기
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // 아래와 같이 해당 파일로써 저장할 수도 있는데 이경우는 onActivityResult의 Bundle에 들어가지 않는다. //cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, new Uri("file:///mnt/sdcard/xxx")); // requestCode=1로 설정한후에 onActivityResult에서 판단한다. startActivityForResult(cameraIntent, 1);사진 촬영은 다른 Activity 가 실행되므로 결과는 아래와 같이 받는다.
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode != RESULT_OK) { Toast.makeText(this, "카메라 촬영에 실패했습니다.", Toast.LENGTH_SHORT).show(); return; } // 1개의 Activity에 여러개의 결과가 돌아올때는 requestCode로 구분한다. if (requestCode == 1) { Bitmap bitmap = null; if ( data.getExtras() != null ) { bitmap = (Bitmap) data.getExtras().get("data"); } else { // Android 2.3 의 경우 try { InputStream is = getContentResolver().openInputStream(data.getData()); bitmap = BitmapFactory.decodeStream(is); is.close(); } catch (Exception e) { e.printStackTrace(); } } if (bitmap != null) { imageView.setImageBitmap(bitmap); } } }
SDcard 가 마운트 되었는지?
if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { // 마운트되지 않았을때 처리 }사진이 저장되는 디렉토리
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);Bitmap 리사이즈 하기
private Bitmap resizeImage(Bitmap srcBitmap, int resizedWidth, int resizedHeight, int quality) { // 원본 이미지의 정보 int srcWidth = srcBitmap.getWidth(); int srcHeight = srcBitmap.getHeight(); Log.d("JO", "source image size => " + srcWidth + "x" + srcHeight); float ratioX = resizedWidth / (float)srcWidth; float ratioY = resizedHeight / (float)srcHeight; Log.d("JO", "ratioX => " + ratioX); Log.d("JO", "ratioY => " + ratioY); int dstWidth = Math.round(srcWidth * ratioX); int dstHeight = Math.round(srcHeight * ratioY); Log.d("JO", "resized image size => " + dstWidth + "x" + dstHeight); // 파일 리사이즈 Bitmap output = Bitmap.createScaledBitmap(srcBitmap, dstWidth, dstHeight, false); try { output.compress(CompressFormat.JPEG, quality, new FileOutputStream("저장할 파일")); } catch (Exception e) { e.printStackTrace(); } return output; }
Bitmap을 byte[]로 변환
ByteArrayOutputStream os = new ByteArrayOutputStream(); bitmap.compress(CompressFormat.JPEG, 100, os); byte[] bin = os.toByteArray();
byte[]를 Bitmap으로 변환
BitmapFactory.Options options = new BitmapFactory.Options(); Bitmap bitmap = BitmapFactory.decodeByteArray(bin, 0, bin.length, options);
리소스에서 Drawble 가져오기
getResources().getDrawable(R.drawable.icon)
Drawble을 Bitmap으로 변환
BitmapFactory.decodeResource(getActivity().getResources(), R.drawable.icon)
ImageView에 설정한 Bitmap 가져오기
imageView.buildDrawingCache(); Bitmap bitmap = imageView.getDrawingCache();]]>