通过将它存储在相机意图机器人

时间:2017-09-20 05:56:09

标签: android image android-intent compression

我正试图从相机意图中捕捉图像并存储它。 但问题是存储的图像最大为40kb,根本不可读。 我从图库中保存图像时使用相同的方法,同时从图库中保存图像,图像尺寸非常完美。

我的代码如下: 请帮助

void opengallery() {

        Intent gallery =
                new Intent(Intent.ACTION_PICK,
                        MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(gallery, GALLERY_REQUEST);
    }

    void opencamera() {

        Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(cameraIntent, CAM_REQUEST);
    }


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

        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == GALLERY_REQUEST) {
            Uri imageUri = data.getData();

            try {
                Bitmap bitmaps = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
                Toast.makeText(this, ""+bitmaps, Toast.LENGTH_SHORT).show();
                saveimage(bitmaps);              
            } catch (IOException e) {
                e.printStackTrace();
            }


        }
        if (requestCode == CAM_REQUEST && resultCode == Activity.RESULT_OK) {

            Bitmap photobit = (Bitmap) data.getExtras().get("data");
            Toast.makeText(this, ""+photobit, Toast.LENGTH_SHORT).show();
            saveimage(photobit);
        }
}


public void saveimage(Bitmap image) {

        ByteArrayOutputStream bytes = new ByteArrayOutputStream();

        image.compress(Bitmap.CompressFormat.JPEG, 100, bytes);

        FileOutputStream fileOutputStream = null;
        try {
            String fileName = "UserImage.jpg";
            File ExternalStorageDirectory = Environment.getExternalStorageDirectory();
            file = new File(ExternalStorageDirectory + File.separator + fileName);
            file.createNewFile();
            fileOutputStream = new FileOutputStream(file);
            fileOutputStream.write(bytes.toByteArray());


            Toast.makeText(this,
                    file.getAbsolutePath(),
                    Toast.LENGTH_LONG).show();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }

0 个答案:

没有答案