如何显示最后从android中的相机捕获的图像?

时间:2012-06-05 06:10:54

标签: android android-camera android-imageview

我在onActivityResult中使用以下代码,但它无法正常工作

File file1 = new File( Environment.getExternalStorageDirectory().getAbsolutePath(),imagecapture);

            if (file1.exists()) {
              //Do action
                image=(ImageView)findViewById(R.id.image);
             Bitmap myBitmap = BitmapFactory.decodeFile(file1.getAbsolutePath());
          image.setImageBitmap(myBitmap);
       image.invalidate();



            }

但我无法理解为什么它不起作用我也用过 Android Camera Intent: how to get full sized photo? 但它在行mImageUri = Uri.fromFile(photo);上出错 任何帮助都会得到赞赏。

2 个答案:

答案 0 :(得分:2)

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */

    Button btnTakePhoto;
    ImageView imgTakenPhoto;
    private static final int CAM_REQUREST = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        btnTakePhoto = (Button) findViewById(R.id.button1);
        imgTakenPhoto = (ImageView) findViewById(R.id.imageView1);

        btnTakePhoto.setOnClickListener(new btnTakePhotoClicker());
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);

          if (requestCode == CAMERA_PIC_REQUEST) {
              Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
              imgTakenPhoto.setImageBitmap(thumbnail);
          }
    }

    class btnTakePhotoClicker implements Button.OnClickListener
    {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
        }
    }
}

答案 1 :(得分:0)

File root=new File(Environment.getExternalStorageDirectory()
                + File.separator + "MYPICS" + File.separator);
File sdImageMainDirectory ;
if (!root.exists()) {
            root.mkdirs();
        }
ImageButton cam =(ImageButton)findViewById(R.id.ImageButton1);
cam.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                imageName = "MyPic" + i + ".png";
                sdImageMainDirectory = new File(root, imageName);

                Uri imgUri = Uri.fromFile(sdImageMainDirectory);

                Intent intent = new Intent(
                        android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, imgUri);

                startActivityForResult(intent, 0);


            }
        });




    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (resultCode) {
        case 0:
            // finish();
            break;

        case -1:
            files = root.listFiles();
            count = files.length;

            try {
                               BitmapFactory.Options option = new BitmapFactory.Options();
                    option.inSampleSize = 2;
            bm = BitmapFactory.decodeFile(root.getPath()+"/"+imageName,option); 

                   ImageView imageview =(ImageView)findViewById(R.id.imageView1);
                    imageview .setImageBitmap(bm);
    }

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