2012-02-04 52 views
0

我正在嘗試使用Android的內置圖庫。我能夠獲得畫廊和相冊,但是無論何時我想要顯示圖像,畫廊都會直接將我引導回我的應用程序。儘管已被調用,但我無法查看圖像。Android - 圖像不會從內置圖庫中顯示

這是我的代碼:

public class CameraTab extends Activity implements OnClickListener{ 
private static final int SELECT_PICTURE = 1; 

private String selectedImagePath; 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.camera_tab); 

    ImageButton cameraBtn = (ImageButton)findViewById(R.id.camera_btn); 
    cameraBtn.setOnClickListener(this); 

    ImageButton galleryBtn = (ImageButton)findViewById(R.id.gallery_btn); 
    galleryBtn.setOnClickListener(this); 

} 

public void onClick(View v) { 
    // TODO Auto-generated method stub 

    if (v == this.findViewById(R.id.camera_btn)){ 
    /// some codes here 
    } 

    if (v == this.findViewById(R.id.gallery_btn)){ 
      Intent intent = new Intent(); 
      intent.setType("image/*"); 
      intent.setAction(Intent.ACTION_GET_CONTENT); 
      startActivityForResult(Intent.createChooser(intent, 
       "Select Picture"), SELECT_PICTURE); 
    } 
} 

public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (resultCode == RESULT_OK) { 
     if (requestCode == SELECT_PICTURE) { 
      Uri selectedImageUri = data.getData(); 
      selectedImagePath = getPath(selectedImageUri);   
     } 
    } 
} 

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

}

任何人都可以幫我嗎?任何幫助,將不勝感激!!謝謝!!

回答

0

問題是對Intent.ACTION_GET_CONTENT意圖的誤解。它用於選擇從檔案中的內容(在本例中爲圖片/ *)。

如果要顯示圖像,只需在其佈局中使用ImageView創建一個新的活動。使用setData傳遞圖像URI。

0

我認爲你想使用的動作是Intent.ACTION_VIEW。 試試這個

final Intent intent = new Intent(Intent.ACTION_VIEW); 
final Uri uri = <The URI to your file>; 
// Uri either from file eg. 
final Uri uri = Uri.fromFile(yourImageFileOnSDCard); 
// or from media store like your method getPath() does but with the URI 
// from http://developer.android.com/reference/android/provider/MediaStore.Images.Media.html#EXTERNAL_CONTENT_URI 
intent.setDataAndType(uri, "image/*"); 
startActivity(intent); 

字符串"image/*是要使用的MIME類型。

現在畫廊打開時選定的圖片。要返回到您的應用程序,用戶必須按後退按鈕,像往常一樣;)

0
private Context context; 

public void onCreate(Bundle savedInstanceState) { 
... 
context = this; 
} 

我用下面縮放位圖,因爲在某些設備上的多媒體的圖像可能是被顯示在一個ImageView的過大(我之前有過這個問題)。

@Override  
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
if (resultCode == RESULT_OK) { 
    if (requestCode == SELECT_PICTURE) { 
     Uri selectedImageUri = data.getData(); 
     selectedImagePath = getPath(selectedImageUri); 

     Bitmap b = new BitmapDrawable(context.getResources(), 
       selectedImagePath).getBitmap(); 
     int i = (int) (b.getHeight() * (512.0/b.getWidth())); 
     bitmap = Bitmap.createScaledBitmap(b, 512, i, true); 

     // To display the image, you need to set it to an ImageView here 
     ImageView img = (ImageView) findViewById(R.id.myImageView); 
     img.setImageBitmap(bitmap); 
     } 
    } 
}