簡體   English   中英

無法從Fragment中選擇圖庫圖像

[英]Unable to pick gallery image from Fragment

這是怎么回事?

我正在嘗試從圖庫中選擇一張照片,然后將該照片設置為我的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);
        }
    });
}

這是我的日志的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));
    }
}

這些是我的日志結果:

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

最后這是我的android清單:

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

你們有沒有想過為什么會這樣?

嘗試將您的圖庫意圖更改為:

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

其中使用了ContentProvider樣式的機器

這讓我感到非常慚愧,但我仍然需要發布答案以防萬一有人達到這個問題。 我應該比較一下

getActivity().RESULT_OK

resultCode

所以這就是它現在的工作方式:

@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