文件上载路径错误

时间:2018-02-24 15:31:59

标签: android image upload

我的应用可以运行但是当我尝试上传图片时它会崩溃。错误指向这一行:

  

String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));

public String getPath(Uri uri) {
    Cursor cursor = getActivity().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 = getActivity().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;
}
  

字符串路径= getPath(filePath);

public void uploadMultipart() {
        String path = getPath(filePath);

        try {
            String uploadId = UUID.randomUUID().toString();

            uploadReceiver.setDelegate(this);
            uploadReceiver.setUploadID(uploadId);

            APIV2 apiv2 = new APIV2();

            String url = apiv2.getChangeAvatarAPI();
            //Creating a multi part request
            new MultipartUploadRequest(getContext(), uploadId, url)
                    .addFileToUpload(path, "file") //Adding file
                    .addParameter("type", "talent-avatar")
                    .addParameter("secret_key", apiv2.getSecretKey())
                    .setNotificationConfig(new UploadNotificationConfig())
                    .setMaxRetries(2)
                    .addHeader("Accept", "application/json")
                    .addHeader("Authorization", "Bearer "+user.getToken())
                    .setMethod("POST")
                    .startUpload(); //Starting the upload

        } catch (Exception exc) {
            Toast.makeText(getContext(), exc.getMessage(), Toast.LENGTH_SHORT).show();
            System.out.println("ERROR UPLOAD: "+exc.getMessage());
        }
    }
  

和这个

     

uploadMultipart();

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        System.out.println("REQUEST CODE "+ requestCode);
        System.out.println("RESULT CODE "+ resultCode);
        if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
            filePath = data.getData();
            try {
                bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), filePath);
                profileImg.setImageBitmap(bitmap);
                uploadMultipart();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        else if (requestCode == REQUEST_IMAGE_CAPTURE && data.getExtras() != null ) {
            bitmap = (Bitmap) data.getExtras().get("data");

            // CALL THIS METHOD TO GET THE URI FROM THE BITMAP
            filePath = getImageUri(getActivity().getApplicationContext(), bitmap);

            System.out.println("CAMERA IMAGE PATH "+filePath.toString());
            profileImg.setImageBitmap(bitmap);
            uploadMultipart();
        }
    }

0 个答案:

没有答案