图片未在imageview中显示

时间:2014-01-13 05:47:19

标签: android camera

我打开相机按钮点击并拍照并在imageview中显示。它在谷歌Nexus工作。但是它没有在Samsung Tab和Micromax canvas HD中工作,为什么?

我的按钮点击代码:

       CAMERA_PIC_REQUEST = 100;

     String path =  Environment.getExternalStorageDirectory()
            + "/MySampleApp/image.jpg";

            File file = new File(path);
            Uri outputFileUri = Uri.fromFile(file);

            Intent cameraIntent = new Intent(
                    android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
            startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);

OnActivityResult代码:

         BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 4;
              Bitmap bitmap = BitmapFactory.decodeFile(path,
                            options);
                    mImageView.setImageBitmap(bitmap);

manifest.xml中的权限

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera" />

为什么这段代码在三星和micromax中不起作用?

这是正确的代码吗?

有人请帮帮我吗?

2 个答案:

答案 0 :(得分:1)

尝试使用此代码,它可以在micromax设备中使用..使用此uri

btnGallery.setOnClickListener(new OnClickListener()
            {

                @Override
                public void onClick(View v)
                {
                    Intent intent = new Intent();
                    intent.setType("image/*");
                    intent.setAction(Intent.ACTION_GET_CONTENT);
                    pop.dismiss();
                    startActivityForResult(Intent.createChooser(intent, "Select Picture"), StaticMembers.galleryRequestCode);
                }
            });
            ImageView btnCamera = (ImageView) pop.findViewById(R.id.ivCamera);
            btnCamera.setOnClickListener(new OnClickListener()
            {

                @Override
                public void onClick(View v)
                {

                    //With Camera Utils
                    pop.dismiss();
                    outpuUri = CameraUtil.startCam(yourActivity.this);
                }
            });


@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        Log.d("Alpha", "onActivity : " + requestCode);
        System.gc();
        Log.d("Alpha", "onActivity : " + requestCode + " RESULT CODE : " + resultCode);
        System.gc();
        long timestamp = System.currentTimeMillis() / 1000L;
        String time = timestamp + "";
        String imgPath = null;

        if (requestCode == CameraUtil.IMAGE_CAPTURED && resultCode == Activity.RESULT_OK)
        {
            if (outpuUri != null)
            {
                Log.d("Alpha", "URI NOT NULL");
                imgPath = outpuUri.getPath();
                //Log.d("Alpha", "ACT RES PATH : " + imgPath);
                //mCapturedBitmap = CameraUtil.sampleBitmap(imgPath, ivHeightWidth);
                //iv.setImageBitmap(mCapturedBitmap);
            }
            else
            {
                Log.d("Alpha", "URI NULL IN CAM");
            }
        }
        else if (requestCode == StaticMembers.galleryRequestCode && resultCode == Activity.RESULT_OK)
        {
            outpuUri = data.getData();
            imgPath = getPath(outpuUri);
            mCapturedBitmap = CameraUtil.sampleBitmap(imgPath, ivHeightWidth);// BitmapFactory.decodeFile(imgPath);
            iv.setImageBitmap(mCapturedBitmap);
            Log.d("Alpha", "In Gallery " + imgPath);
        }
        if (imgPath != null)
        {
            showConfrirmDialog(imgPath, time);
        }

    }



    public class CameraUtil
    {

        private static Uri outpuUri;

        public static final int IMAGE_CAPTURED = 200;

        public static String imageName;

        private static String imageFolder;


        public static Uri startCam(Activity context)
        {
            imageName = "sample" + System.currentTimeMillis() + ".jpg";
            outpuUri = Uri.fromFile(new File(getImageFolderFile().getAbsolutePath() + File.separator + imageName));
            Log.d("CHECK", "BEFORE STARTING CAM URI : " + outpuUri.getPath());
            Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            i.putExtra(MediaStore.EXTRA_OUTPUT, outpuUri);
            i.putExtra(MediaStore.EXTRA_SCREEN_ORIENTATION, ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            context.startActivityForResult(i, IMAGE_CAPTURED);
            return outpuUri;
        }


        public static File getImageFolderFile()
        {

            imageFolder = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "sparkchat_camera_pics";

            File f = new File(imageFolder);
            Log.d("Alpha", f.getAbsolutePath() + " exists > " + f.exists());
            if (!f.exists())
                f.mkdirs();
            return f;
        }
    }

答案 1 :(得分:1)

试试这个

 btn.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0)
            {
                CAMERA_PIC_REQUEST = 100;
                Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);   
            }
        });

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {   
     super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == CAMERA_PIC_REQUEST && resultCode == RESULT_OK) {  
      Bitmap photo = (Bitmap) data.getExtras().get("data"); 
      mImageView.setImageBitmap(photo);
      } 
    }