代码之家  ›  专栏  ›  技术社区  ›  baycisk

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

  •  2
  • baycisk  · 技术社区  · 7 年前

    我正在尝试制作上传图片(稍后还有视频)的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/simulated/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 回复  |  直到 7 年前
        1
  •  0
  •   Tomin B Azhakathu    7 年前

    试试这个

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

    希望这对你有帮助。这将为您返回文件的原始路径。

    推荐文章