获取文件的名称和完整路径 [英] get name and full path of a file

查看:94
本文介绍了获取文件的名称和完整路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Intent ACTION_GET_CONTENT来显示手机内存中的最新文件.包括图片,pdf,google驱动器文档(pdf,xlsx),如下面的屏幕快照所示.我想获取名称和完整路径,以便可以将文件上传到服务器.到目前为止,我/我已经正确获得了mime类型.

I used Intent ACTION_GET_CONTENT to show recent files from phone memory. That includes images, pdf, google drive documents(pdf, xlsx) as shown in screenshot below. I want to get the name and full path so that I can upload the file to server. I/m getting the mime type correctly as of now.

public class MainActivity extends AppCompatActivity {

    Button btn;
    TextView txt;
    private final static int EXTERNAL = 111;
    private final static int ATTACH = 11;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn = (Button)findViewById(R.id.btn);
        txt = (TextView)findViewById(R.id.txt);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    if (ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
                        photoIntent();
                    } else {
                        if (shouldShowRequestPermissionRationale(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                            // showToast("Permission Required...");
                        }
                        requestPermissions(new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, EXTERNAL);
                    }


                } else {

                    photoIntent();
                }
            }
        });



    }

    private void photoIntent() {
        Intent intent = new Intent();
        Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath());
        intent.setDataAndType(uri, "*/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, "Complete action using"), ATTACH);
    }

    @Override
    public void onRequestPermissionsResult(int requestcode, String[] permission, int[] grantRes){

        if (requestcode == EXTERNAL) {
            if (grantRes[0] == PackageManager.PERMISSION_GRANTED) {
                photoIntent();
            } else {
                Toast.makeText(this, "Unable to Access Image", Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == ATTACH && resultCode == RESULT_OK){
            Uri uri = data.getData();
            System.out.println("sammy_sourceUri "+uri);
            String mimeType = getContentResolver().getType(uri);
            System.out.println("sammy_mimeType "+ mimeType);
            


        }

    }
}

推荐答案

我想获取名称和完整路径,以便可以将文件上传到服务器

I want to get the name and full path so that I can upload the file to server

没有完整路径",因为可能没有文件".

There is no "full path", because there may not be a "file".

您正在调用ACTION_GET_CONTENT.这将对某些内容返回Uri.内容的来源取决于用户选择的ACTION_GET_CONTENT活动的开发者.可能是:

You are invoking ACTION_GET_CONTENT. This returns a Uri to some content. Where that content comes from is up to the developers of the ACTION_GET_CONTENT activity that the user chose. That could be:

  • 文件系统上的一个普通文件,恰好是您可以访问的文件
  • 文件系统上的普通文件,位于您无法访问的位置,例如另一个应用程序的内部存储空间
  • 需要某种转换才能使用的文件,例如解密
  • 数据库中的BLOB
  • 动态生成的内容,即Stack Overflow服务器动态生成此网页的方式
  • 依此类推
  • An ordinary file on the filesystem that happens to be one that you could access
  • An ordinary file on the filesystem that resides somewhere that you cannot access, such as internal storage for the other app
  • A file that requires some sort of conversion for it to be useful, such as decryption
  • A BLOB column in a database
  • Content that is generated on the fly, the way this Web page is generated on the fly by the Stack Overflow servers
  • And so on

要使用Uri中的内容,请使用ContentResolveropenInputStream().

To use the content from the Uri, use a ContentResolver and openInputStream().

如何使用此InputStream作为文件上传到服务器API?

how to use this InputStream to upload to server API as file?

您选择的HTTP API支持InputStream作为该内容的来源,或者不支持.如果是这样,只需直接使用InputStream.如果不是,请使用InputStream作为内容的临时副本作为您 可以直接访问的文件(例如,在getCacheDir()中),上传该文件,然后在上传完成.

Either your chosen HTTP API supports an InputStream as the source of this content, or it does not. If it does, just use the InputStream directly. If not, use the InputStream to make a temporary copy of the content as a file that you can directly access (e.g., in getCacheDir()), upload that file, then delete the file when the upload is complete.

这篇关于获取文件的名称和完整路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆