2011-07-04 48 views

Trả lời

11

Thực hiện luân chuyển của hình ảnh bạn có thể có đoạn mã sau:

Bitmap bMap = BitmapFactory.decodeResource(getResources(),R.drawable.test); 
Matrix mat = new Matrix(); 
mat.postRotate(90); 
Bitmap bMapRotate = Bitmap.createBitmap(bMap, 0, 0, 
          bMap.getWidth(), bMap.getHeight(), mat, true); 
BitmapDrawable bmd = new BitmapDrawable(bMapRotate); 
image.setImageBitmap(bMapRotate); 
image.setImageDrawable(bmd); 

và cho hình ảnh cắt xén lấy từ bộ sưu tập sử dụng đoạn mã sau đây:

Intent viewMediaIntent = new Intent(); 
    viewMediaIntent.setAction(android.content.Intent.ACTION_VIEW); 
    File file = new File("/image/*");  
    viewMediaIntent.setDataAndType(Uri.fromFile(file), "image/*"); 
    viewMediaIntent.putExtra("crop","true"); 
    viewMediaIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
    startActivityForResult(viewMediaIntent,1); 

Hope , điều này sẽ hữu ích cho bạn.

+0

câu trả lời của bạn đã giúp tôi quá ,, đã chiến đấu để xoay hình ảnh từ một thời gian qua .. cảm ơn Nikki – AAnkit

+0

Điều này cũng giúp tôi rất nhiều. Cảm ơn. –

+0

ERROR/(5824): không thể mở '/ image/*' tại sao? Bạn còn ý kiến ​​nào không? – DuyguK

1

Hãy thử đoạn mã sau để cắt hình ảnh đã chọn khỏi giá trị.

private static final String TEMP_PHOTO_FILE = "temporary_holder.jpg"; 

Intent photoPickerIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
photoPickerIntent.setType("image/*"); 
photoPickerIntent.putExtra("crop", "true"); 
photoPickerIntent.putExtra(MediaStore.EXTRA_OUTPUT, getTempUri()); 
photoPickerIntent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()); 
startActivityForResult(photoPickerIntent, REQ_CODE_PICK_IMAGE); 


private Uri getTempUri() { 
return Uri.fromFile(getTempFile()); 
} 

private File getTempFile() { 
if (isSDCARDMounted()) { 

File f = new File(Environment.getExternalStorageDirectory(),TEMP_PHOTO_FILE); 
try { 
f.createNewFile(); 
} catch (IOException e) { 

} 
return f; 
} else { 
return null; 
} 
} 

private boolean isSDCARDMounted(){ 
String status = Environment.getExternalStorageState(); 
if (status.equals(Environment.MEDIA_MOUNTED)) 
return true; 
return false; 
} 




protected void onActivityResult(int requestCode, int resultCode, 
    Intent imageReturnedIntent) { 
super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

switch (requestCode) { 
case REQ_CODE_PICK_IMAGE: 
    if (resultCode == RESULT_OK) { 
     if (imageReturnedIntent!=null){ 



      File tempFile = getTempFile(); 

      String filePath= Environment.getExternalStorageDirectory() 
     + "/temporary_holder.jpg"; 
      System.out.println("path "+filePath); 


Bitmap selectedImage = BitmapFactory.decodeFile(filePath); 
_image = (ImageView) findViewById(R.id.image); 
_image.setImageBitmap(selectedImage); 

} 
} 
} 

và cũng có một cái nhìn tại Tutorial1 này, tutorial2

+0

mã này mở thư viện đầu tiên, tôi đã triển khai nó thông qua tutorial2, Tôi muốn mở hình ảnh cụ thể cho cây trồng, không chọn từ thư viện, ảnh mặc định tồn tại ... bạn sẽ giúp tôi ? –

Các vấn đề liên quan