【问题标题】:How to get the file path for the picked files from the external storage in android?如何从android的外部存储中获取挑选文件的文件路径?
【发布时间】:2017-10-01 09:56:21
【问题描述】:

我在选择文件的文件路径时遇到问题,我搜索了整个堆栈溢出但问题没有解决。从设备中选择文件的代码如下所示。

 Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
           intent.setType("*/*");
           intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
           //intent.addFlags(ST)
           startActivityForResult(Intent.createChooser(intent, "Choose File to Upload.."), PICK_FILE_REQUEST);

intent 中选择的文件是由

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
   super.onActivityResult(requestCode, resultCode, data);
   if (resultCode == Activity.RESULT_OK) {
       if (requestCode == PICK_FILE_REQUEST) {
           if (data != null) {
               //no data present
               Uri uri = data.getData();
              String filePath = data.getData().getPath();
        //       String path = uri.getPath();
               file = new File(filePath);

               String name = getContentName(getContentResolver(), uri);
               try {
                   InputStream inStream = getContentResolver().openInputStream(uri);

               } catch (FileNotFoundException e) {
                   e.printStackTrace();
               }
               try {
                   bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);

               } catch (IOException e) {
                   e.printStackTrace();
               }

               LinearLayout linearLayout = new LinearLayout(this);
               linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                       LinearLayout.LayoutParams.WRAP_CONTENT));
               linearLayout.setOrientation(LinearLayout.VERTICAL);

               ImageView imageView = new ImageView(this);
               imageView.setImageBitmap(bitmap);
               attachFile.addView(imageView);


               TextView textView = new TextView(this);
               textView.setText(name);
               attachFile.addView(textView);

               return;
           }

       }
   }

}

在上面的代码中文件路径是通过`String filePath = data.getData().getPath();得到的但是在将文件上传到服务器时会抛出异常,例如无效的文档和文件。如何从 uri 中获取文件的正确路径?

但是文件名是通过使用来选择的

`public static String getContentName(ContentResolver resolver, Uri uri) {
       Cursor cursor = resolver.query(uri, null, null, null, null);
       cursor.moveToFirst();
       int nameIndex = cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME);
       if (nameIndex >= 0) {
           return cursor.getString(nameIndex);
       } else {
           return null;
       }
   }`

如何获取所选文件的正确文件路径?

【问题讨论】:

    标签: android android-intent android-file


    【解决方案1】:

    如何获取所选文件的正确文件路径?

    你没有。没有文件。 ACTION_GET_CONTENT 与文件关系不大。它与内容有关。

    使用ContentResolveropenInputStream() 处理Uri 标识的内容。对于上传,要么:

    • InputStream 提供给上传API,如果它可以从那里上传,或者

    • 使用InputStreamFileOutputStream 对您控制的某个文件(例如,在getCacheDir() 中)制作内容副本,然后从本地副本上传

      李>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多