Java 将本地图像上传到android数据库

Java 将本地图像上传到android数据库,java,android,image-uploading,Java,Android,Image Uploading,我可以使用本地选择器(而不是相机)更改图像,并将其设置为imageview(profile_image),如下所示: public void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == RESULT_OK) { if (requestCode == SELECT_PICTURE) { Uri selectedImageUri =

我可以使用本地选择器(而不是相机)更改图像,并将其设置为imageview(profile_image),如下所示:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == SELECT_PICTURE) {
            Uri selectedImageUri = data.getData();
            //selectedImagePath = getPath(selectedImageUri);
            //Utils.log("selectedImagePath: " + selectedImagePath);
            Utils.log("selectedImageUri: " + selectedImageUri);
            profile_image.setImageURI(selectedImageUri);
            final int takeFlags = data.getFlags()
                    & (Intent.FLAG_GRANT_READ_URI_PERMISSION
                    | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

            String id = selectedImageUri.getLastPathSegment().split(":")[1];
            final String[] imageColumns = {MediaStore.Images.Media.DATA };
            final String imageOrderBy = null;

            Uri uri = getUri();
            String selectedImagePath = "path";

            Cursor imageCursor = managedQuery(uri, imageColumns,
                    MediaStore.Images.Media._ID + "="+id, null, imageOrderBy);

            if (imageCursor.moveToFirst()) {
                selectedImagePath = imageCursor.getString(imageCursor.getColumnIndex(MediaStore.Images.Media.DATA));
                new LocalImageUpload().execute(selectedImagePath);
            }
            Utils.log("selectedImagePath:" + selectedImagePath); // use selectedImagePath
        }else{
            Uri uri = data.getData();
            String[] projection = { MediaStore.Images.Media.DATA };

            Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
            cursor.moveToFirst();
            Utils.log("DatabaseUtils:" + DatabaseUtils.dumpCursorToString(cursor));

            int columnIndex = cursor.getColumnIndex(projection[0]);
            String picturePath = cursor.getString(columnIndex); // returns null
            cursor.close();
        }
    }
}
我用
Utils.log获得了
selectedImageUri
(“selectedImageUri:+selectedImageUri”)
,但当
log Utils.log(“selectedImagePath:+selectedImagePath”)时,我得到了
null

更新:我像那样改变,并且已经得到selectedImagePath。已注释旧selectedImagePath


所以我想在
profile\u image.setImageURI(selectedImageUri)之后将图像上传到数据库,如何将本地图像选择器上载到数据库?我是说后台程序的邮政编码?

你能试试下面的吗

将Uri作为参数传递到下面的函数中,并从Uri中获取路径

private String getRealPathFromURI (Uri contentUri) {
    String path = null;
    String[] proj = { MediaStore.MediaColumns.DATA };
    Cursor cursor = getContentResolver().query(contentUri, proj, null, null, null);
    if (cursor.moveToFirst()) {
       int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
       path = cursor.getString(column_index);
    }
    cursor.close();
    return path;
}
现在使用以下代码在图像视图上设置位图:

String mRealPath = getRealPathFromURI(your_uri);
Bitmap bmImg = BitmapFactory.decodeFile(mRealPath);
imageView.setImageBitmap(bmImg);
编辑:

现在您必须为异步任务传递参数

new LocalImageUpload().execute(mRealPath);

希望这会对您有所帮助。

确保您已授予读取外部存储权限,并且您正在选择图像表格库本身。已将其放在AndroidManifests上。此帖子可以帮助您将图像上载到数据库@NigamPatro,这就是我要找的,谢谢。这是正确的答案。抱歉,回复时间太长,因为我需要先检查它。我得到了图像路径(更新代码),现在我正在搜索如何将其发布到数据库。@carijawaban,我已经编辑了我的答案,请检查。是的,像新的LocalImageUpload().execute(mRealPath);已经找到要上传到数据库的后台任务。@carijawaban,那个么你们需要我做什么?