2017-03-07 276 views
1

發生了什麼事?無法從片段中選取圖片庫圖片

我想從圖庫中選擇一張照片,然後將該照片設置爲我的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" /> 

你們有什麼想法,爲什麼會發生這種情況?

+0

不畫廊開放?應用程序崩潰? – tahsinRupam

+0

是的,畫廊打開,不,應用程序不會崩潰。我無法設法更新我的ImageButton,但我認爲這是因爲我的RESULT_OK等於-1 – Rob

回答

0

這讓我感到很慚愧,但我仍然需要張貼以防萬一有人達到這個問題的答案。我應該是比較

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

試着改變你的畫廊意圖是:

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風格機甲