无法从片段中选择图库图像
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
风格的机械
这让我感到很惭愧,但我仍然需要 post 回答,以防有人遇到这个问题。我应该比较
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));
}
}
怎么了?
我正在尝试从图库中选择一张照片,然后将该照片设置为我的 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
风格的机械
这让我感到很惭愧,但我仍然需要 post 回答,以防有人遇到这个问题。我应该比较
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));
}
}