2013-12-17 20 views
0

在我的應用中,用戶單擊一個按鈕打開Choser來選擇圖片。在我的活動結果代碼是來自OnActivityResult的帶有圖像視圖的URI

public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (resultCode == RESULT_OK) { 
      if (requestCode == SELECT_PICTURE_REQUEST_CODE) { 
       Uri selectedImageUri = data.getData(); 
       if(selectedImageUri!=null){ 
        imageviewTest.setImageURI(selectedImageUri) 

       }else{ 

       } 

      } 
     } 
} 

圖像不顯示。我想知道爲什麼?我注意到這個論壇上的一些人將這個值傳遞給contentresolver,爲什麼?不應該有足夠的路徑嗎?

感謝

回答

0
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (resultCode == RESULT_OK) { 
      if (requestCode == SELECT_PICTURE_REQUEST_CODE) { 
       BitmapFactory.Options bfOptions =new BitmapFactory.Options(); 
       Uri selectedImage = data.getData(); 
       if(selectedImage!=null){ 
        String galleryImatePath = getRealPathFromURI(selectedImage); 
        try{ 

          bfOptions.inDither=false;   //Disable Dithering mode 
          bfOptions.inPurgeable=true;  //Tell to gc that whether it needs free memory, the Bitmap can be cleared 
          bfOptions.inInputShareable=true; //Which kind of reference will be used to recover the Bitmap data after being clear, when it will be used in the future 
          bfOptions.inSampleSize=5; 
          bfOptions.inTempStorage=new byte[32 * 1024]; 
          InputStream stream = getContentResolver().openInputStream(selectedImage); 
          final Bitmap myImage = BitmapFactory.decodeStream(stream, null , bfOptions); 
          ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
          myImage.compress(Bitmap.CompressFormat.JPEG, 100, bos); 
          bitmapdata = bos.toByteArray(); 
          imageviewTest.setImageBitmap(myImage) 

       }else{ 

       } 

      } 
     } 
} 

public String getRealPathFromURI(Uri contentUri) { 
    String[] proj = { MediaStore.Images.Media.DATA }; 
    Cursor cursor = managedQuery(contentUri, proj, null, null, null); 
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
    cursor.moveToFirst(); 
    return cursor.getString(column_index); 
} 
+0

謝謝!這樣可行。當顯示圖片時,我應該使用壓縮方法嗎?還是抽樣方法? – Snake

+0

使用壓縮方法 –

相關問題