【问题标题】:Image not set in imageview after taking images from camera从相机拍摄图像后,图像未在 imageview 中设置
【发布时间】:2015-07-13 08:37:06
【问题描述】:

我试图在按钮单击时从相机获取图像并将其设置在 Activity 的图像视图中,但图像未在图像视图中设置。我需要将其设置为缩略图。上传时遇到同样的问题,请解决。

这是我的代码:

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();
                }
            }
     }

【问题讨论】:

  • 照片有吗?看看这个answer
  • 是的。但是当我试图在 imageview.image 中设置它时,没有设置。我认为它的大小。

标签: android


【解决方案1】:

以下代码将对您有所帮助:

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> 

【讨论】:

  • 我已经尝试过这段代码。但是未在 imageview 中设置静止图像。我认为是因为它的尺寸更大。我需要图片作为缩略图。
  • 您可以使用代码调整大小:stackoverflow.com/questions/8471226/…
【解决方案2】:

试试这个

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> 

【讨论】:

    【解决方案3】:

    这可能是一个大图片大小问题。试试这个代码来减小图片的大小。

                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);
    

    【讨论】:

      猜你喜欢
      • 2014-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-07
      • 2012-10-11
      • 2011-07-15
      相关资源
      最近更新 更多