简体   繁体   English

使用相机拍照后无法显示图片

[英]Picture not displaying after using camera to take photo

I am trying to click a photo in a fragment and display it inside my app. 我试图单击片段中的照片,并将其显示在我的应用程序中。 After I click the photo, the imageView is not set and I don't see the photo getting displayed. 单击照片后,未设置imageView,并且看不到显示照片。 Does anyone know why? 有人知道为什么吗?

Also do you think there is a better way of writing the code for taking a picture inside the fragment? 您还认为有更好的方式编写代码以在片段内拍照吗?

public class ImageFragment extends Fragment{


private Uri imageUri;
private String mPath;
private ImageView image;
Bitmap bitmap = null;
private File tempPhoto;
 @Override 
 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
   { 
     View v = inflater.inflate(R.layout.fragment_image, container, false);
     ImageButton snap=((ImageButton)v.findViewById(R.id.snap));
     image = ((ImageView) v.findViewById(R.id.image));
     snap.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {

                Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                File imagePath;

                try
                {
                    tempPhoto = createTemporaryFile("picture", ".png");
                    tempPhoto.delete();
                }

                catch(Exception e)
                {

                    return ;
                }

                imageUri = Uri.fromFile(tempPhoto);
                cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);
                startActivityForResult(cameraIntent, 1);

            }
        });

     return v;
  }

 private File createTemporaryFile(String part, String ext) throws Exception
    {
        File tempDir= Environment.getExternalStorageDirectory();
        tempDir=new File(tempDir.getAbsolutePath()+"/.temp/");
        if(!tempDir.exists())
        {
            tempDir.mkdir();
        }
        return File.createTempFile(part, ext, tempDir);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
       // super.onActivityResult(requestCode, resultCode, data);
        ContentResolver cr = getActivity().getContentResolver();
        try {
            cr.notifyChange(imageUri, null);
            File imageFile = new File(tempPhoto.getAbsolutePath());


        } catch (Exception e) {
            e.printStackTrace();
        }

            Bitmap photo=null;
            if (resultCode == 1) {
                try {
                    photo = android.provider.MediaStore.Images.Media.getBitmap(cr, Uri.fromFile(tempPhoto));
                    image.setImageBitmap(photo);
                } catch (FileNotFoundException e) {

                    e.printStackTrace();
                } catch (Exception e) {

                    e.printStackTrace();
                }
            }

    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onLowMemory() {
        // TODO Auto-generated method stub
        super.onLowMemory();
    }

} }

You compare 'resultCode' with Activity.RESULT_OK(=-1) in onActivityResult function. 您在onActivityResult函数中将'resultCode'与Activity.RESULT_OK(=-1)进行比较。

Replace: if (resultCode == 1) by: if (resultCode == Activity.RESULT_OK) Replace: if(resultCode == 1) Replace: by: if(resultCode == Activity.RESULT_OK)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM