2012-01-09 29 views

Trả lời

0

Mã này hoạt động nếu là chèn hướng dẫn này:

intent.putExtra("crop", "true"); 
0
  • Launch một ý định ACTION_GET_CONTENT thay vì một ACTION _PICK
  • Cung cấp một MediaStore.EXTRA_OUTPUT thêm với một URI tới một tệp tạm thời.

Thêm phần này vào hoạt động gọi của bạn:

file yourFile;

Bây giờ sử dụng này code to get Intent:

yourFile = getFileStreamPath("yourTempFile"); 
yourFile.getParentFile().mkdirs(); 
Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT, null); 
galleryIntent .setType("image/*"); 
galleryIntent .putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(yourFile)); 
galleryIntent.putExtra("outputFormat", Bitmap.CompressFormat.PNG.name()); 
startActivityForResult(galleryIntent, GALLERY_PIC_REQUEST); 

CHẮC CHẮN RẰNG yourFile được tạo ra

Cũng trong hoạt động của bạn gọi

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    switch(requestCode){ 
    case GALLERY_PIC_REQUEST: 
     File file = null; 
     Uri imageUri = data.getData(); 
     if (imageUri == null || imageUri.toString().length() == 0) { 
      imageUri = Uri.fromFile(mTempFile); 
      file = mTempFile; 
      //this is the file you need! Check it 
     } 
     //if the file did not work we try alternative method 
     if (file == null) { 
      if (requestCode == 101 && data != null) { 
       Uri selectedImageUri = data.getData(); 
       String selectedImagePath = getPath(selectedImageUri); 
       //check this string to extract picasa id 
      } 
     } 
    break; 
    } 
} 

public String getPath(Uri uri) { 
    String[] projection = { MediaStore.Images.Media.DATA }; 
    Cursor cursor = managedQuery(uri, projection, null, null, null); 
    if(cursor!=null) 
    { 
     int index = cursor 
     .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
     cursor.moveToFirst(); 
     return cursor.getString(index); 
    } 
    else return null; 
} 
+0

File mTempFile là bao giờ trống rỗng. –

+1

điều này không hoạt động nếu hình ảnh ở picasa .. –

5

ACTIVITYRESULT_CHOOSEPICTURE là int bạn sử dụng khi gọi startActivity (inten t, requestCode);

public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if(requestCode == ACTIVITYRESULT_CHOOSEPICTURE) { 
    BitmapFactory.Options options = new BitmapFactory.Options(); 
    final InputStream is = context.getContentResolver().openInputStream(intent.getData()); 
    final Bitmap bitmap = BitmapFactory.decodeStream(is, null, options); 
    is.close(); 
    } 
} 
+0

cảm ơn vì điều này, nó hoạt động tốt, ngay cả khi hình ảnh đến từ picasa. Hãy nhớ làm mã này trên một chuỗi, vì nó có thể tải xuống nội dung từ mạng. –

0

Sử dụng mã này

final Uri tempUri = data.getData(); 
        Uri imageUri = null; 
        final InputStream imageStream; 
        try { 
         imageStream = getActivity().getContentResolver().openInputStream(tempUri); 
         Bitmap selectedImage = BitmapFactory.decodeStream(imageStream); 
         imageUri = getImageUri(getActivity(), selectedImage); 
        } catch (FileNotFoundException e) { 
         e.printStackTrace(); 
        } 


public Uri getImageUri(Context inContext, Bitmap inImage) { 
     ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
     inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes); 
     String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null); 
     return Uri.parse(path); 
    } 
Các vấn đề liên quan