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

时间:2015-11-26 02:59:49

标签: 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 = 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();
        }
    }
}

我使用selectedImageUri得到Utils.log("selectedImageUri: " + selectedImageUri);,但null

时收到了log Utils.log("selectedImagePath: " + selectedImagePath);

更新:我改变了这一点,并且已经有了selectedImagePath。评论旧的selectedImagePath。

所以我想在profile_image.setImageURI(selectedImageUri);之后将图片上传到数据库,如何将本地图片选择器上传到数据库?我的意思是后台处理POST代码?

1 个答案:

答案 0 :(得分:0)

你能在下面试一下吗?

功能下面

将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;
}

现在使用以下代码在 ImageView 上设置位图

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

修改

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

new LocalImageUpload().execute(mRealPath);

希望这会对你有所帮助。