【问题标题】:Android: Get file path while uploading image file as multi-part file using Ion Koush libraryAndroid:使用 Ion Koush 库将图像文件作为多部分文件上传时获取文件路径
【发布时间】:2019-03-04 15:26:36
【问题描述】:

我通过以下意图选择图像:

Intent getIntent = new Intent(Intent.ACTION_GET_CONTENT);
getIntent.setType("image/*");

Intent pickIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
pickIntent.setType("image/*");

Intent chooserIntent = Intent.createChooser(getIntent, "Select Image");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] {pickIntent});

startActivityForResult(chooserIntent, PICK_IMAGE);

我正在通过覆盖 onActivityResult 来检索意图

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if (requestCode == PICK_IMAGE) {
        Uri imageUri = data.getData();
        ProfileCalls.editProfileImage(imageUri, getContext()); //Ion post data
    }
}

然后我尝试通过以下方式获取图像的路径,这会引发以下异常 java.io.FileNotFoundException: /external/images/media/5302 (No such file or directory)

public static void editProfileImage(Uri profileImageUri, final Context context) {
    Ion.with(context)
            .load("url")
            .setMultipartFile("profileImage", "application/json; charset=UTF-8", new File(profileImageUri.getPath()))
            .asJsonObject()
            .setCallback(new FutureCallback<JsonObject>() {
                @Override
                public void onCompleted(Exception e, JsonObject result) {
                    System.out.print(e);
                    System.out.print(result);
                }
            });
}

【问题讨论】:

    标签: java android android-intent ion-koush


    【解决方案1】:

    这是我后来在这里找到的答案

    private static String getPath(Context context, Uri uri) {
        String result = null;
        String[] proj = {MediaStore.Images.Media.DATA};
        Cursor cursor = context.getContentResolver().query(uri, proj, null, null, null);
        if (cursor != null) {
            if (cursor.moveToFirst()) {
                int column_index = cursor.getColumnIndexOrThrow(proj[0]);
                result = cursor.getString(column_index);
            }
            cursor.close();
        }
        if (result == null) {
            result = "Not found";
        }
        return result;
    }
    

    【讨论】:

      猜你喜欢
      • 2014-06-28
      • 1970-01-01
      • 1970-01-01
      • 2019-04-22
      • 2013-03-04
      • 1970-01-01
      • 2018-12-11
      • 2013-12-17
      • 1970-01-01
      相关资源
      最近更新 更多