2012-01-09 46 views

回答

0

代碼工作,如果是插入該指令:

intent.putExtra("crop", "true"); 
0
  • 發動ACTION_GET_CONTENT意圖,而不是一個ACTION _PICK
  • 提供一個額外的MediaStore.EXTRA_OUTPUT與URI到一個臨時文件。

添加到您的通話活動:

文件yourFile;

現在用這個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); 

請確認yourFile在您調用活動創建

而且

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

的mTempFile文件是有史以來空。 – 2012-01-11 14:42:39

+1

這不起作用,如果圖片是在picasa .. – 2012-10-14 12:08:15

5

ACTIVITYRESULT_CHOOSEPICTURE是你打電話時使用INT 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

感謝您的這一點,它工作正常,即使當圖片來自picasa。請記住在一個線程上執行此代碼,因爲它可能會從網上下載內容。 – 2012-10-14 12:40:02

0

使用此代碼

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); 
    } 
相關問題