简体   繁体   English

Android-无法从内置图片库显示图片

[英]Android - Image does not display from built-in gallery

I am trying to use Android's built-in gallery. 我正在尝试使用Android的内置库。 I am able to get the gallery and the albums, but whenever I want to display the image, the gallery straightaway directs me back to my app. 我可以获得画廊和专辑,但是每当我要显示图像时,画廊都会立即将我引导回我的应用程序。 I am unable to view the image despite it has been called. 尽管已调用该图像,但仍无法查看。

This is my code: 这是我的代码:

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);
}

} }

Can anyone please help me? 谁能帮帮我吗? Any help would be appreciated!! 任何帮助,将不胜感激!! Thanks!! 谢谢!!

I think the Action you want to use is Intent.ACTION_VIEW . 我认为您要使用的Action是Intent.ACTION_VIEW try this 尝试这个

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);

The string "image/* is the mime type to be used. 字符串"image/*是要使用的mime类型。

Now the gallery is opened with the given picture selected. 现在,打开画廊并选择给定的图片。 To return to your app the user has to press the back button, as usual ;) 要返回到您的应用,用户必须像往常一样按返回按钮;)

private Context context;

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

I used a scaled Bitmap below because on some devices the images in the Gallery might be too large for being displayed on an ImageView (I had this problem before). 我在下面使用了可缩放的位图,因为在某些设备上,图库中的图像可能太大,无法显示在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);
        }
    }
}

The problem is a misunderstanding of Intent.ACTION_GET_CONTENT intent. 问题是对Intent.ACTION_GET_CONTENT意图的误解。 It's intented to be used to choose a content (in this case image/*) from the archive. 它旨在用于从存档中选择内容(在本例中为image / *)。

If you want to show an image, just create a new activity with an ImageView in its layout. 如果要显示图像,只需使用布局中的ImageView创建一个新活动。 Pass the image URI using setData . 使用setData传递图像URI。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM