上传时出错 - Android

时间:2018-02-22 18:59:06

标签: android image-uploading multipartform-data

我正在尝试将图像作为多部分数据(图像和大小)上传到服务器。我用烧瓶服务器。我尝试在服务器上发送数据并且在应用程序中没有错误但是在发送时,它从不上传,但在上传期间显示错误。我用邮递员检查服务器,它工作正常。我提供代码:

public void uploadMultipart() {
    //getting name for the image
    String name = editText.getText().toString().trim();
    //getting the actual path of the image
    String path = getPath(filePath);
    Integer size = 0;
    //Uploading code
    File file = new File(path);
    Bitmap bf = BitmapFactory.decodeFile(path);
    Integer height = bf.getHeight();
    Integer width = bf.getWidth();
    size = width*height;
    try {
        //Creating a multi part request
        new MultipartUploadRequest(this.getApplicationContext(), "", Constants.UPLOAD_URL)
                .addFileToUpload(path, "file") //Adding file
                .addParameter("size", size.toString()) //Adding text parameter to the request
                .setNotificationConfig(new UploadNotificationConfig())
                .setMaxRetries(2)
                .setDelegate(new UploadServiceBroadcastReceiver())
                .startUpload(); //Starting the upload

    } catch (Exception exc) {
        Toast.makeText(this, exc.getMessage(), Toast.LENGTH_SHORT).show();
    }
}

public String getPath(Uri uri) {
    Cursor cursor = getContentResolver().query(uri, null, null, null, null);
    cursor.moveToFirst();
    String document_id = cursor.getString(0);
    document_id = document_id.substring(document_id.lastIndexOf(":") + 1);
    cursor.close();

    cursor = getContentResolver().query(
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
            null, MediaStore.Images.Media._ID + " = ? ", new String[]{document_id}, null);
    cursor.moveToFirst();
    String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
    cursor.close();

    return path;
}`

0 个答案:

没有答案