简体   繁体   English

如何从带有文件名的SD卡获取文件的实际路径?

[英]How to get actual path of file from sdcard with file name?

I have a file in sdcard "firmware.PTI". 我在sdcard“firmware.PTI”中有一个文件。 And i am browsing this file from my app. 我正在从我的应用程序浏览此文件。 Below is the code i am using. 以下是我正在使用的代码。

 @Override
        public void onClick(View v) {

            int id = v.getId();

            switch (id) {
            case R.id.btn_browse_pti_file:

                Intent mediaIntent = new Intent(Intent.ACTION_GET_CONTENT);
                mediaIntent.setType("*/*"); //set mime type as per requirement
                startActivityForResult(mediaIntent, REQ_CODE_FOR_PTI);
                break;
            }
        }

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

            if (requestCode == REQ_CODE_FOR_PTI && resultCode == Activity.RESULT_OK) {
                Uri uriPTI = data.getData();
                String path = Utility.getRealPathFromURI(getActivity(), uriPTI);
                Log.i(CLASSTAG, "PTI file path: "+path);
                mTvBrowsePath.setText(path);
            }
        }

public static String getRealPathFromURI(Context context, Uri contentUri) {

        String path = null;
        if (contentUri != null)
        {
            path = contentUri.toString();
            if (path.toLowerCase().startsWith("file://"))
            {
                // Selected file/directory path is below
                path = (new File(URI.create(path))).getAbsolutePath();
            }

        }
        return path;
    }

If i am selecting any other file like JPEG or PNG, it is working fine and showing the correct path with file name. 如果我选择任何其他文件,如JPEG或PNG,它工作正常,并显示文件名正确的路径。 But, if i am selecting any .PTI file then it is getting crashed. 但是,如果我选择任何.PTI文件,那么它就会崩溃。 How can i resolve this problem? 我该如何解决这个问题?

You have to check what the method getRealPathFromURI is returning because it may return null if contentUri is null or if path does not start with file:// . 您必须检查getRealPathFromURI返回的方法,因为如果contentUrinull或者path不以file://开头,它可能返回null

In addition to this, it is a better approach to check for the scheme in contentUri . 除此之外,还有一种更好的方法来检查contentUri的方案。 Below is an updated version of your method. 以下是您的方法的更新版本。

    public static String getRealPathFromURI(Context context, Uri uri) {
        if( "content".equalsIgnoreCase( uri.getScheme() ) ) {
            String[] projection = { MediaStore.Images.Media.DATA };
            Cursor cursor = null;

            try {
                cursor = context.getContentResolver().query(uri, projection, null, null, null);
                int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                if (cursor.moveToFirst()) {
                    return cursor.getString(column_index);
                }
            } catch (Exception e) {
                // Eat it
                e.printStackTrace();
            } finally {
                if(cursor != null) {
                    cursor.close();
                }
            }
        } else if( "file".equalsIgnoreCase( uri.getScheme() ) ) {
            return uri.getPath();
        }
        return null;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM