从本地本地获取文件路径和名称

时间:2018-12-02 00:22:42

标签: java android android-intent

我是android开发的新手,我打算在本地选择文件,以便将其上传到一些云存储中。我已经意识到很多情况,返回的文件名以image:b的形式表示某个数字b。例如,对于文件,我得到path = /document/image:91filename = image:91。这是否意味着我的结果是错误的?我期望得到实际的文件名。我只想知道我的代码是否正确。

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);
    Log.d(TAG, "requestCode: " + requestCode);
    Log.d(TAG, "resultCode: " + resultCode);
    Log.d(TAG, "Actiity.RESULT_OK: " + Activity.RESULT_OK);
    Log.d(TAG, "intent data: " + data);


    if (requestCode == CHOOSE_FILE_REQUESTCODE && resultCode == Activity.RESULT_OK && null != data) {

        Uri image = data.getData();
        String path = image.getPath();
        this.file = new File(path);
        String filename = image.getLastPathSegment();
        //File destination = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/CustomFolder/" + filename);
        Log.d(TAG, "file name: " + filename);
        Log.d(TAG, "file path: " + path);
        this.fileName.setText(filename);


    }
}

开始意图。

public void onClickSelectPhoto(View v){
        Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        intent.setType("*/*");
        startActivityForResult(intent, CHOOSE_FILE_REQUESTCODE);
}

0 个答案:

没有答案