我站在一個大問題面前,我已經問過很多人,但沒有人能幫助我。ImageView不會在片段中插入相機圖片
在標題中,我製作了一個帶有三個片段的Android應用程序。 在一個片段中,我想打開一個攝像頭,當點擊一個空的ImageView時。 拍攝照片後,ImageView應顯示來自相機的拍攝圖像。
該應用程序打開相機,也需要一張照片,但不會將圖片插入到ImageView中。
此代碼是在我的片段
public class FragmentNew extends Fragment {
public static final int CAMERA_PIC_REQUEST_1 = 1;
public static final int CAMERA_PIC_REQUEST_2 = 2;
public static final int SELECT_PIC_REQUEST_1 = 3;
public static final int SELECT_PIC_REQUEST_2 = 4;
ImageView imageView1;
ImageView imageView2;
View rootView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragment_new,container, false);
imageView1 = (ImageView) rootView.findViewById(R.id.image_pic1);
imageView2 = (ImageView) rootView.findViewById(R.id.image_pic2);
return rootView;
}
public void check(int requestCode, int resultCode, Intent data,ImageView imv1,ImageView imv2) {
Log.d("Name", "onActivityResult");
switch(requestCode){
case CAMERA_PIC_REQUEST_1:
if(resultCode== Activity.RESULT_OK && data != null && data.getExtras().get("data")!=null){
Log.d("TAG1","Photo pic1");
Bitmap photo = (Bitmap) data.getExtras().get("data");
imv1.setImageBitmap(photo);
Log.d("TAG1","Photo pic1 was set");
}
break;
case CAMERA_PIC_REQUEST_2:
if(resultCode== Activity.RESULT_OK && data != null && data.getExtras().get("data")!=null){
Log.d("TAG2","Photo pic2");
Bitmap photo2 = (Bitmap) data.getExtras().get("data");
//imv2.setImageBitmap(photo2);
imv2.setBackgroundColor(Color.rgb(255, 0, 255));
Log.d("TAG2","Photo pic2 was set");
}
break;
default:
Log.d("Error","It doesn't work");
}
}
}
MainActivity:
public void onButtonClick(View view)
{
switch(view.getId())
{
case R.id.image_accept:
EditText text;
text = (EditText)findViewById(R.id.edit_title);
Toast.makeText(this,"Decision "+ text.getText().toString()+" was created!",Toast.LENGTH_SHORT).show();
break;
case R.id.image_pic1:
Builder builderpic1 = new Builder(this);
builderpic1.setTitle("Pick a Pic")
.setIcon(android.R.drawable.ic_dialog_info)
.setItems(R.array.selection, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch(which)
{
case 0:
//Intent galeryintent = new Intent(Intent.ACTION_VIEW, Uri.parse(
// "content://media/internal/images/media"));
// startActivity(galeryintent);
//Intent intent = new Intent();
//intent.setType("image/*");
//intent.setAction(Intent.ACTION_GET_CONTENT);
//startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PIC_REQUEST_1);
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
//startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PIC_REQUEST_1);
startActivityForResult(intent, FragmentNew.SELECT_PIC_REQUEST_1);
break;
case 1:
Intent photointent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(photointent, FragmentNew.CAMERA_PIC_REQUEST_1);
break;
case 2:
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://images.google.com/"));
startActivity(browserIntent);
break;
}
dialog.dismiss();
}
})
.show();
break;
case R.id.image_pic2:
Builder builderpic2 = new Builder(this);
builderpic2.setTitle("Pick a Pic")
.setIcon(android.R.drawable.ic_dialog_info)
.setItems(R.array.selection, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch(which)
{
case 0:
Intent galeryintent = new Intent(Intent.ACTION_VIEW, Uri.parse(
"content://media/internal/images/media"));
startActivity(galeryintent);
break;
case 1:
Intent photointent2 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(photointent2, FragmentNew.CAMERA_PIC_REQUEST_2);
break;
case 2:
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://images.google.com/"));
startActivity(browserIntent);
break;
}
dialog.dismiss();
}
})
.show();
break;
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Fragment myFragment = (Fragment) getSupportFragmentManager().findFragmentById(R.id.fragment_new);
ImageView imv1= (ImageView)findViewById(R.id.image_pic1);
ImageView imv2= (ImageView)findViewById(R.id.image_pic2);
((FragmentNew)myFragment).check(requestCode, resultCode, data,imv1,imv2);
}
的BACKGROUNDCOLOR正好被設置以檢查問題是位圖,但BackgroundColor不ch ange也是如此,所以我認爲問題在於片段在onActivityResult()中找不到ImageView。 Log.d顯示在控制檯中,但ImageView保持空白。
我希望你能幫助我。
我不認爲你的圖像視圖沒有找到,或者它會是null,你會在imv2.setBackgroundColor(Color.rgb(255,0,255))有一個NullPointerException;所以至少這不是你的問題。它會輸出你的Log.d(「TAG2」,「Photo pic2」); ? – Damien
是輸出打印在控制檯 – RoterBaron
它執行日誌輸出但不設置背景?你能看到以前的背景嗎?你確定你的ImageView的尺寸嗎? – Damien