简体   繁体   English

Android从图库中选择图像

[英]Android pick an image from gallery

In fragment I am calling gallery intent like this: 在片段中我正在调用gallery这样的意图:

         // Create the Intent for Image Gallery.
        Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(i, MainActivity.LOAD_IMAGE_RESULTS);

And in main activity I am handling result with following code: 在主要活动中,我使用以下代码处理结果:

public static int LOAD_IMAGE_RESULTS = 1;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Log.w("reqestCode:",""+requestCode);
    if(requestCode == LOAD_IMAGE_RESULTS && data != null && data.getData() != null) {
        Uri _uri = data.getData();

        //User had pick an image.
        Cursor cursor = getContentResolver().query(_uri, new String[] { android.provider.MediaStore.Images.ImageColumns.DATA }, null, null, null);
        cursor.moveToFirst();

        //Link to the image
        final String imageFilePath = cursor.getString(0);
        Log.w("ImageFile",imageFilePath);
        cursor.close();
    }
}

But in onActivityResult requestCode is returning 196609 value.So my code is not working.How can I resolve it ? 但是在onActivityResult requestCode返回196609值。所以我的代码不能正常工作。我该如何解决呢?

Looks issue is with your Intent that you created. 看起来问题与您创建的Intent有关。 Try something like this: 尝试这样的事情:

                pictureActionIntent = new Intent(
                        Intent.ACTION_GET_CONTENT, null);
                pictureActionIntent.setType("image/*");
                pictureActionIntent.putExtra("return-data", true);
                startActivityForResult(pictureActionIntent,
                        GALLERY_PICTURE);

For more information check this answer. 有关更多信息,请查看答案。

You said you're calling this from the Fragment . 你说你是从Fragment调用它的。 Have you tried listening to the onActivityResult in both places? 您是否尝试过在两个地方都听取onActivityResult The Activity as well as the Fragment ? Activity以及Fragment It's worth a shot. 这值得一试。 :) :)

try this code it will works // start the activity 尝试这个代码它将起作用//开始活动

Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(
                Intent.createChooser(intent, "Select Picture"),
                GALLERY_INTENT_CALLED);

//in onactivity result get the bitmap //在onactivity结果中获取位图

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == GALLERY_INTENT_CALLED) {
            if (null == data)
                return;
            String selectedImagePath;
            Uri selectedImageUri = data.getData();
            // MEDIA GALLERY
             Bitmap bitmap = BitmapFactory.decodeFile(ImageFilePath.getPath(
                    getApplicationContext(), selectedImageUri));
            ivProfileImage.setImageBitmap(bitmap);

        }
    }
}

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

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