ImageView不会将相机图片插入片段中

时间:2015-05-04 20:28:40

标签: android android-fragments camera imageview onactivityresult

我站在一个大问题面前,我已经问了很多人,但是没有人可以帮助我。

在标题中,我制作了一个包含三个片段的Android-App。 在一个片段中,我想在单击空的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也没有改变,所以我认为问题是,片段在onActivityResult中找不到ImageView ()。 Log.d显示在控制台中,但ImageView保持为空。

我希望你能帮助我。

1 个答案:

答案 0 :(得分:0)

对不起,但我找不到你的onActivityResult方法以及你设置ImageView OnclickListener的地方。也许他们在你的活动课上。 除了ImageView args之外,你的check方法看起来很像onActivityResult方法。你自己打电话给你的检查方法吗?是你的onActivityResult吗?

也许你做过类似的事情然后我不知道你的代码有什么问题:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
    mFragment.check(requestCode, resultCode, data, mImageView1, mImageView2);
}

实际上,如果你不介意将图片保存在设备上,你也可以这样做:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(outputFile));
startActivityForResult(intent,CAMERA_PIC_REQUEST_1);

在你的onActivityResult中:

Bitmap bm = BitmapFactory.decodeFile(outputFile.getPath());
mImageView1.setImageBitmap(bm);

但我认为你的方式也应该有效,我从来没有尝试过。

您能否添加更多代码,以便我们可以看到您调用startActivityForResult和onActivityResult方法的位置?