簡體   English   中英

如何從相機獲取圖像

[英]How to get image from camera

我已經嘗試了很多來自stackoverflow和Internet的代碼。但是我能夠找到如何從相機中拾取圖像。我使用了以下代碼,但是data.getData()始終返回null。不知道如何解決。請幫忙。

 Intent takePictureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
           if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
                    startActivityForResult(takePictureIntent, PICK_IMAGE_CAMERA1);
                }
        protected void onActivityResult(int requestCode, int resultCode, Intent data)
                {  
                    if (requestCode == 30 && resultCode == Activity.RESULT_OK&&data!null)
                    {  

                        Bundle extras = data.getExtras();
            Bitmap imageBitmap = (Bitmap) extras.get("data");
                        imageView.setImageBitmap(imageBitmap);

                    }  
                }

嘗試使用此代碼@Thrishool,

private static final int CAMERA = 1;

//choosing the image from camera
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(intent, CAMERA);



//now get the data from the onActivity result

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);

   if (requestCode == CAMERA) {
        Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
        imageview.setImageBitmap(thumbnail);
        ByteArrayOutputStream baos=new  ByteArrayOutputStream();
        thumbnail.compress(Bitmap.CompressFormat.PNG,100, baos);
        byte [] b=baos.toByteArray();
        String temp = Base64.encodeToString(b, Base64.DEFAULT);
        Log.e("savedImage",temp);

        Toast.makeText(ProfileActivity.this, "Image Saved!", Toast.LENGTH_SHORT).show();
    }
}

試試這個,讓我知道@Thrishool

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM