从存储中选择文件后获取文件路径

时间:2018-04-24 05:13:06

标签: android upload android-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);
        }
    });

我的活动结果代码:

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)

1 个答案:

答案 0 :(得分:0)

试试这个

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();
        }
    }
}

希望这可以帮到你。这将为您返回文件的原始路径。