捕获图像并将其上载到数据库

时间:2018-02-19 10:34:26

标签: java android

我想将图像从相机上传到数据库。我已经从已经上传到数据库的图库中的图库图像中解决了问题但是它不能从相机或捕获图像工作,我试图用相同的代码与图库编码,但我有一个错误。

这是我的代码。

这是我的 Android Manifest.xml 代码

<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-feature android:name="android.hardware.camera"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

MainActivity

这是我的提交按钮

reportsubmit.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View view) {
        ImageUploadToServerFunction();
     }
  });
}

这是我将图片上传到数据库的代码

public void ImageUploadToServerFunction(){

    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 85, bytes);
    byte[] byteArrayVar = bytes.toByteArray();
    final String ConvertImage = Base64.encodeToString(byteArrayVar, Base64.DEFAULT);

}

这是我打开相机的代码

private void openCamera() {
    Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(cameraIntent, CAMERA_REQUEST);
}

这是我的OnActivityResult代码

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (attachment >= 1 && attachment <= 8) {
        if (resultCode == RESULT_OK && requestCode == 1 && data != null && data.getData() != null) {
            Uri uri = data.getData();
            try {
                if (attachment == 1) {
                    dynamic = (ImageView) findViewById(R.id.imgview);
                } else if (attachment == 2) {
                    dynamic = (ImageView) findViewById(R.id.imgview1);
                } else if (attachment == 3) {
                    dynamic = (ImageView) findViewById(R.id.imgview2);
                } else if (attachment == 4) {
                    dynamic = (ImageView) findViewById(R.id.imgview3);
                } else if (attachment == 5) {
                    dynamic = (ImageView) findViewById(R.id.imgview4);
                } else if (attachment == 6) {
                    dynamic = (ImageView) findViewById(R.id.imgview5);
                } else if (attachment == 7) {
                    dynamic = (ImageView) findViewById(R.id.imgview6);
                } else if (attachment == 8) {
                    dynamic = (ImageView) findViewById(R.id.imgview7);
                }

                bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
                dynamic.setImageBitmap(bitmap);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {

                if (attachment == 1) {
                    dynamic = (ImageView) findViewById(R.id.imgview);
                } else if (attachment == 2) {
                    dynamic = (ImageView) findViewById(R.id.imgview1);
                } else if (attachment == 3) {
                    dynamic = (ImageView) findViewById(R.id.imgview2);
                } else if (attachment == 4) {
                    dynamic = (ImageView) findViewById(R.id.imgview3);
                } else if (attachment == 5) {
                    dynamic = (ImageView) findViewById(R.id.imgview4);
                } else if (attachment == 6) {
                    dynamic = (ImageView) findViewById(R.id.imgview5);
                } else if (attachment == 7) {
                    dynamic = (ImageView) findViewById(R.id.imgview6);
                } else if (attachment == 8) {
                    dynamic = (ImageView) findViewById(R.id.imgview7);
                }

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

            dynamic.setImageBitmap(imageBitmap);

            // CALL THIS METHOD TO GET THE URI FROM THE BITMAP
            Uri tempUri = getImageUri(getApplicationContext(), imageBitmap);

            // CALL THIS METHOD TO GET THE ACTUAL PATH
            Toast.makeText(report.this,"Here "+ getRealPathFromURI(tempUri),Toast.LENGTH_LONG).show();

           }
        System.out.print(resultCode);
    }
}

public Uri getImageUri(Context inContext, Bitmap inImage) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
    return Uri.parse(path);
}

public String getRealPathFromURI(Uri uri) {
    Cursor cursor = getContentResolver().query(uri, null, null, null, null);
    cursor.moveToFirst();
    int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
    return cursor.getString(idx);
}

0 个答案:

没有答案