获取文件的android系统中的路径 [英] get the path of file in android

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

问题描述

您好,我们有文件选择器,并希望在与文件选择器的用户选择文件,上传返回所选文件的路径。现在我要怎样才能得到所选文件的路径。我想要的文件这样的路径 - > /mnt/sdcard/DCIM/13.jpg

code:

 私人无效fnFileChooser(){
    意向意图=新意图(Intent.ACTION_GET_CONTENT);
    intent.setType(* / *);
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    尝试{
        startActivityForResult(
                Intent.createChooser(意向,选择要上传的文件),
                fileSelected code);
    }
    赶上(android.content.ActivityNotFoundException前){
    //直接潜在用户市场提供一个对话
    Toast.makeText(这一点,请安装一个文件管理器。
            Toast.LENGTH_SHORT).show();
    }
}
@覆盖
保护无效的onActivityResult(INT申请code,INT结果code,意图数据){
            如果(结果code == RESULT_OK){
                //获取所选文件的URI
                URI URI = data.getData();
                PATH = uri.getPath();
                txtAddress.setText(uri.getPath());
                ImageView的IMG =(ImageView的)findViewById(R.id.imgTest);
                img.setImageURI(URI);
                / *字符串路径= FileUtils.getPath(这一点,URI);
                Log.d(TAG,文件路径:+路径); * /
                //获取文件的实例
                //文件文件=新的文件(路径);
                //启动上传
            }
    super.onActivityResult(要求code,结果code,数据);
}


解决方案

尝试使用这些code拿到哪一个是 openFileOutput

 公共字符串getFilePath(上下文的背景下,字符串YourFileName)
        {
            文件F = context.getFileStreamPath(YourFileName);
            返回f.getAbsolutePath();
        }

hello we have file picker and want when user select file with file picker , return path of selected file for upload. now i want how can get path of selected file . i want the path of file like this -- > /mnt/sdcard/DCIM/13.jpg

code :

private void fnFileChooser(){
    Intent intent=new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("*/*");
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    try{
        startActivityForResult(
                Intent.createChooser(intent, "Select a File to Upload"),
                fileSelectedCode);
    }
    catch (android.content.ActivityNotFoundException ex) {
    // Potentially direct the user to the Market with a Dialog
    Toast.makeText(this, "Please install a File Manager.",
            Toast.LENGTH_SHORT).show();
    }
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {


            if (resultCode == RESULT_OK) {
                // Get the Uri of the selected file
                Uri uri = data.getData();
                path=uri.getPath();
                txtAddress.setText(uri.getPath());
                ImageView img=(ImageView) findViewById(R.id.imgTest);
                img.setImageURI(uri);
                /*String path = FileUtils.getPath(this, uri);
                Log.d(TAG, "File Path: " + path);*/
                // Get the file instance
                // File file = new File(path);
                // Initiate the upload
            }


    super.onActivityResult(requestCode, resultCode, data);
}

解决方案

Try to use these code to get the path of file which one is created with openFileOutput

public String getFilePath(Context context,String YourFileName)
        {
            File f= context.getFileStreamPath(YourFileName);
            return f.getAbsolutePath();
        }

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

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