2013-12-23 3 views
0

사용자 셀의 라이브러리에서 사진을 가져 오는 코드를 구현했지만 사용자가 Android 기본 자르기 UI를 사용하여 이미지자를 수 있도록 코드 우물을 사용하고 있습니다.자른 이미지 가져 오기

Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
    intent.putExtra("crop", "true"); 
    intent.putExtra("aspectX", 1); 
    intent.putExtra("aspectY", 1); 
    intent.putExtra("outputX", 280); 
    intent.putExtra("outputY", 280); 
    intent.putExtra("scale", true); 
    startActivityForResult(intent , RESULT_CODE_PICK_FROM_LIBRARY); 

다시 나는하여 onActivityResult에 코드를 사용하고 이미지를 얻을 수 있습니다 :

Uri selectedImage = data.getData(); 
String tempPath = getPath(selectedImage); 
Bitmap pickedImage = BitmapFactory.decodeFile(tempPath); 

getPath() :

private String getPath(Uri uri) { 
    String[] projection = { MediaStore.Images.Media.DATA }; // MediaColumns.DATA // MediaStore.Images.Media.DATA 
    Cursor cursor = getContentResolver().query(uri, projection, null, null, null); 
    if (cursor != null) { 
     cursor.moveToFirst(); 
     int columnIndex = cursor.getColumnIndex(projection[0]); 
     String filePath = cursor.getString(columnIndex); 
     cursor.close(); 
     return filePath; 
    } else { 
     return null; 
    } 
} 

하지만 줄에 널 포인터 예외가 나타납니다.

Cursor cursor = getContentResolver().query(uri, projection, null, null, null); 

아무도 이것에 관해 어떤 clew도 가지고 있습니까? 코드가 사진을 촬영을 자른 후 간단한 이미지를 검색 할 수있는 옵션이 제시 될 것입니다 무슨 또한 는 .. .. 원본 파일에 대한 필요가 없습니다

감사합니다, 뉴턴

답변

1

이를 Tutorial 당신이 즐길 것을 적극 추천 :

이미지 자르기를 의도 픽업 동작 후 enter image description here

+0

제안에 감사드립니다.하지만이 튜토리얼은 촬영 한 사진을 파일로 저장하기 때문에 필자는 작은 크기의 사진 만 필요합니다 ... 또한 문서화되지 않은/개인 코드 (com.android)를 사용합니다. camera.action.CROP) 이것은 곧 지원을 느슨하게 할 수 있으며 모든 장치/osVersions에서도 작동하지 않습니다! –

1

를 사용하여 작물 비트 맵에이 기능을

public static Bitmap getCroppedBitmap(Bitmap bmp, int radius) { 
    Bitmap sbmp; 
    if(bmp.getWidth() != radius || bmp.getHeight() != radius) 
     sbmp = Bitmap.createScaledBitmap(bmp, radius, radius, false); 
    else 
     sbmp = bmp; 
    Bitmap output = Bitmap.createBitmap(sbmp.getWidth(), 
      sbmp.getHeight(), Config.ARGB_8888); 
    Canvas canvas = new Canvas(output); 

    final int color = 0xffa19774; 
    final Paint paint = new Paint(); 
    final Rect rect = new Rect(0, 0, sbmp.getWidth(), sbmp.getHeight()); 

    paint.setAntiAlias(true); 
    paint.setFilterBitmap(true); 
    paint.setDither(true); 
    canvas.drawARGB(0, 0, 0, 0); 
    paint.setColor(Color.parseColor("#BAB399")); 
// canvas.drawCircle(sbmp.getWidth()/2+0.7f, sbmp.getHeight()/2+0.7f, 
//   sbmp.getWidth()/2+0.1f, paint); 
    canvas.drawCircle(sbmp.getWidth()/2, sbmp.getHeight()/2, 
      (sbmp.getWidth()/2), paint); 
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); 
    canvas.drawBitmap(sbmp, rect, rect, paint); 


      return output; 
} 
+0

이미지 자르기 코드가 있지만 이미지가 정사각형이 아닌 경우 가로 세로 비율이 낮아집니다. 그래서 안드로이드 기본 자르기 UI를 구현하고 싶습니다. –