从相机拍摄图像后未在imageview中设置图像

时间:2015-07-13 08:37:06

标签: android

我试图在按钮点击时从相机拍摄图像并在活动图像视图中设置它,但图像视图上没有设置图像。我需要将其设置为缩略图。上传时遇到同样的问题。请解决。

这是我的代码:

private static final int CAMERA_PIC_REQUEST = 1111;


takephoto.setOnClickListener(new 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); 
            }
        });

 protected void onActivityResult(int requestCode, int resultCode, Intent data) 
     {
            if (requestCode == CAMERA_PIC_REQUEST) {

                Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
                iv.setImageBitmap(thumbnail);
                ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
                File file = new File(Environment.getExternalStorageDirectory()+File.separator + "image.jpg");
                try {
                    file.createNewFile();
                    FileOutputStream fo = new FileOutputStream(file);
                    fo.write(bytes.toByteArray());
                    fo.close();
                } 
                catch (IOException e) 
                {

                    e.printStackTrace();
                }
            }
     }

3 个答案:

答案 0 :(得分:0)

以下代码可以帮助您:

public class MyCameraActivity extends Activity {
    private static final int CAMERA_REQUEST = 1000; 
    private ImageView imageView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        this.imageView = (ImageView)this.findViewById(R.id.imageView1);
        Button photoButton = (Button) this.findViewById(R.id.button1);
        photoButton.setOnClickListener(new View.OnClickListener() {

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

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
        if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {  
            Bitmap photo = (Bitmap) data.getExtras().get("data"); 
            imageView.setImageBitmap(photo);
        }  
    } 
}

以下是xml:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
        <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/photo"></Button>
        <ImageView android:id="@+id/imageView1" android:layout_height="wrap_content" android:src="@drawable/icon" android:layout_width="wrap_content"></ImageView>

</LinearLayout>

在manifest.xml中添加:

<uses-feature android:name="android.hardware.camera"></uses-feature> 

答案 1 :(得分:0)

试试这个

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CAMERA_PIC_REQUEST) {
            Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
            mImage.setImageBitmap(thumbnail);
            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
            File file = new File(Environment.getExternalStorageDirectory()+File.separator + "image.jpg");
            try {
                file.createNewFile();
                FileOutputStream fo = new FileOutputStream(file);
                fo.write(bytes.toByteArray());
                fo.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

并将此添加到您最明显的

<uses-feature android:name="android.hardware.camera"></uses-feature> 

答案 2 :(得分:0)

这可能是一个大图片尺寸的问题。请尝试使用此代码来缩小图片大小。

            Bitmap photo;
            File file = new File(picturePath);
            int file_size = Integer
                    .parseInt(String.valueOf(file.length() / 1024));
            if (file_size > 2048)
            {
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize = 4;
                photo = BitmapFactory.decodeFile(picturePath, options);
            } else if (file_size > 1024)
            {
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize = 2;
                photo = BitmapFactory.decodeFile(picturePath, options);
            } else
                photo = BitmapFactory.decodeFile(picturePath);
            iv.setImageBitmap(photo);