简体   繁体   English

无法从Fragment中选择图库图像

[英]Unable to pick gallery image from Fragment

what's up? 这是怎么回事?

I'm trying to pick a photo from gallery and then set that photo as the image of my ImageButton. 我正在尝试从图库中选择一张照片,然后将该照片设置为我的ImageButton图像。 That should be pretty straight forward but somehow I'm screwing it up. 这应该是非常直接的,但不知怎的,我搞砸了。 This is how I'm trying: I have a fragment in which I set up my tabs and my ImageButton: 这就是我的尝试:我有一个片段,我在其中设置我的标签和我的ImageButton:

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    TabHost host = (TabHost)getView().findViewById(R.id.tabHost);
    host.setup();

    //Tab 1
    TabHost.TabSpec spec = host.newTabSpec("Foto");
    spec.setContent(R.id.tab1);
    spec.setIndicator("Foto");
    host.addTab(spec);

    //Tab 2
    spec = host.newTabSpec("Sentimento");
    spec.setContent(R.id.tab2);
    spec.setIndicator("sentimento");
    host.addTab(spec);

    //Tab 3
    spec = host.newTabSpec("Medição");
    spec.setContent(R.id.tab3);
    spec.setIndicator("Medição");
    host.addTab(spec);

    cancelBtn = (ImageButton)getView().findViewById(R.id.cancel_post_btn);
    saveBtn = (ImageButton)getView().findViewById(R.id.save_post_btn);
    insertPostText = (EditText)getView().findViewById(R.id.insert_post_text);

    postImageBtn = (ImageButton)getView().findViewById(R.id.post_img_btn);
    postImageBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
            galleryIntent.setType("image/*");
            startActivityForResult(galleryIntent, GALLERY_REQUEST);
        }
    });
}

This is my onActivityResult method with my logs: 这是我的日志的onActivityResult方法:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Log.d("---------", "activity result");
    if (requestCode == GALLERY_REQUEST && requestCode == getActivity().RESULT_OK){
        Log.d("-----------", "result ok");
        Uri imageUri = data.getData();
        postImageBtn.setImageURI(imageUri);
    } else {
        Log.d("-----------", "result not ok");
        Log.d("----------", String.valueOf(requestCode));
        Log.d("----------", String.valueOf(RESULT_OK));
    }
}

These are my log results: 这些是我的日志结果:

03-07 14:23:50.767 30717-30717/? D/---------: activity result
03-07 14:23:50.767 30717-30717/? D/-----------: result not ok
03-07 14:23:50.767 30717-30717/? D/----------: 1
03-07 14:23:50.767 30717-30717/? D/----------: -1

And finally this is my android manifest: 最后这是我的android清单:

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Have you guys any idea on why is this happening? 你们有没有想过为什么会这样?

Try changing your gallery intent to this: 尝试将您的图库意图更改为:

 Intent pickPhoto = new Intent(Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        if (isAdded() && pickPhoto.resolveActivity(getActivity().getPackageManager()) != null)
            startActivityForResult(pickPhoto, requestCode);

Which uses a ContentProvider style mech 其中使用了ContentProvider样式的机器

That makes me feel really ashamed but still I need to post the answer just in case someone reaches this question. 这让我感到非常惭愧,但我仍然需要发布答案以防万一有人达到这个问题。 I should be comparing 我应该比较一下

getActivity().RESULT_OK

with

resultCode

so this is how it works now: 所以这就是它现在的工作方式:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
Log.d("---------", "activity result");
if (requestCode == GALLERY_REQUEST && resultCode == getActivity().RESULT_OK){
    Log.d("-----------", "result ok");
    Uri imageUri = data.getData();
    postImageBtn.setImageURI(imageUri);
} else {
    Log.d("-----------", "result not ok");
    Log.d("----------", String.valueOf(requestCode));
    Log.d("----------", String.valueOf(RESULT_OK));
}
}

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

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