Android - 图像无法从内置库中显示

时间:2012-02-04 21:46:04

标签: android image android-intent gallery

我正在尝试使用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);
}

}

任何人都可以帮助我吗?任何帮助,将不胜感激!!谢谢!!

3 个答案:

答案 0 :(得分:0)

问题是对Intent.ACTION_GET_CONTENT意图的误解。它的目的是用来从档案中选择内容(在本例中为image / *)。

如果要显示图像,只需在其布局中创建一个带有ImageView的新活动。使用setData传递图片URI。

答案 1 :(得分: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类型。

现在打开图库并选择给定的图片。要返回您的应用程序,用户必须像往常一样按后退按钮;)

答案 2 :(得分: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);
        }
    }
}