捕获的图像在imageView中显示黑色

时间:2017-01-12 16:58:41

标签: android sqlite uri

我一直在寻找很多教程,以找出将图像保存到SQLite的最佳方法。最后我决定获取imagePath并将其保存到SQLite,并将捕获的图像显示为ImageView

    private void activeTakePhoto() {
            ContentValues values = new ContentValues();
            values.put(MediaStore.Images.Media.TITLE, "Image File name");
             mCapturedImageURI = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
            Intent intentPicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            intentPicture.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);
            startActivityForResult(intentPicture, REQUEST_IMAGE_CAPTURE);
        }

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case REQUEST_IMAGE_CAPTURE:
                    try {
                       String selectedImage = getRealPathFromURI(mCapturedImageURI);
                        imageView.setImageDrawable(Drawable.createFromPath(selectedImage));
                    } catch (Exception e) {
                        Toast.makeText(getApplication(),"Failed",Toast.LENGTH_SHORT).show();
                    }
                    break;

                case RESULT_LOAD_IMAGE:
                    //
                    break;
            }
        }
    }
public String getRealPathFromURI(Uri contentUri)
    {
        try
        {
            String[] proj = {MediaStore.Images.Media.DATA};
            Cursor cursor = managedQuery(contentUri, proj, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        }
        catch (Exception e)
        {
            return contentUri.getPath();
        }
    }

我做了一个小测试来打印selectedImage并显示值,但我的imageView是黑色。

2 个答案:

答案 0 :(得分:0)

试试这段代码:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (RESULT_OK == resultCode) {

       if (REQUEST_IMAGE_CAPTURE == requestCode && data != null) {
            Bundle extras = data.getExtras();
            if (extras != null && extras.containsKey("data")) {
                Bitmap bitmap = (Bitmap) extras.get("data");
                if (bitmap != null) 
                         imageView.setImageBitmap(bitmap);
                    else
                        Toast.makeText(getApplication(),"Failed",Toast.LENGTH_SHORT).show();
                }
            }
        } 
    }

如果你想使用uri

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        switch (requestCode) {
            case REQUEST_IMAGE_CAPTURE:
                try {
                    Uri selectedImage = data.getData();
                    String[] filePathColumn = { MediaStore.Images.Media.DATA };
                    Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
                    cursor.moveToFirst();
                    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                    String picturePath = cursor.getString(columnIndex);
                    cursor.close();
                    imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
                } catch (Exception e) {
                    Toast.makeText(getApplication(),"Failed",Toast.LENGTH_SHORT).show();
                }
                break;

            case RESULT_LOAD_IMAGE:
                //
                break;
        }
    }
}

答案 1 :(得分:0)

试试这段代码:

捕捉图片:

private void activeTakePhoto() {
Intent imageLoader = (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT)
        ? new Intent(MediaStore.ACTION_IMAGE_CAPTURE_SECURE)
        : new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(imageLoader, REQUEST_IMAGE_CAPTURE);
}

处理请求:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    //super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        switch (requestCode) {
            case REQUEST_IMAGE_CAPTURE:
                try {
                    imageView.setImageBitmap((Bitmap) data.getExtras().get("data"));
                } catch (Exception e) {
                    Toast.makeText(getApplication(),"Failed",Toast.LENGTH_SHORT).show();
                }
                break;

            case RESULT_LOAD_IMAGE:
                //
                break;

            default: 
                // 'not RESULT_OK' or any other case
                super.onActivityResult(requestCode, resultCode, data);
                break;
        }
    }
}