从存储中获取 select 文件后的文件路径

Getting the file path after select file from storage

我正在尝试上传图片(稍后还有视频)android 应用程序。所以我想从存储中获取文件图像,然后在 imageView 中显示,然后在按钮内按下它会上传到服务器。

我偶然发现图像路径不存在的问题,不知道为什么。这是我的代码: 从设备获取图像的按钮:

buttonUploadPhoto.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media
                    .EXTERNAL_CONTENT_URI);
            i.setType("image/*");
            startActivityForResult(i, REQUEST_IMAGE);
        }
    });

我在 activity 结果代码:

super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        try {
            final Uri imageUri = data.getData();
            final InputStream imageStream = getContentResolver().openInputStream(imageUri);
            final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);

            String filename = imageUri.getPath();
            destination = new File(Environment.getExternalStorageDirectory(), filename + ".jpg");
            imagePath = destination.getAbsolutePath();
            Log.e(LOG, "imagePath: " + imagePath);

            setcardPic.setImageBitmap(selectedImage);
            buttonSubmitPhoto.setVisibility(View.VISIBLE);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            Toast.makeText(getApplicationContext(), "Something went wrong", Toast.LENGTH_LONG).show();
        }

    }else {
        Toast.makeText(getApplicationContext(), "You haven't picked Image",Toast.LENGTH_LONG).show();
    }

我从那里得到了图像路径,但它不是正确的名称,它给我这样的:/storage/emulated/0/external/images/media/298.jpg,实际文件名像 cherry.jpg

这里是我的上传按钮代码:

public int uploadFile(String sourceFileUri) {

    String fileName = sourceFileUri;
    Log.e(LOG, "fileName :" + fileName);
    showUploadButton = false; //revert upload button to hidden
    HttpURLConnection conn = null;
    DataOutputStream dos = null;
    String lineEnd = "\r\n";
    String twoHyphens = "--";
    String boundary = "*****";
    int bytesRead, bytesAvailable, bufferSize;
    byte[] buffer;
    int maxBufferSize = 1024 * 1024;
    File sourceFile = new File(sourceFileUri);
    Log.e(LOG, "running upload file :" + fileName);

    if (!sourceFile.isFile()) {
        dialog.dismiss();
        Log.e(LOG, "Source File not exist :" + imagePath);
        return 0;
    } else { ... (upload to server this part is working)

试试这个

final Uri imageUri = data.getData();
String path = getRealPathFromURI(getApplicationContext(), imageUri);

函数在哪里

public String getRealPathFromURI(Context context, Uri contentUri) {
    Cursor cursor = null;
    try {
        String[] proj = {MediaStore.Images.Media.DATA};
        cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}

希望这对您有所帮助。这将为您 return 文件的原始路径。