【问题标题】:get the path of file in android获取android中文件的路径
【发布时间】:2015-07-17 22:50:54
【问题描述】:

您好,我们有文件选择器,当用户使用文件选择器选择文件时,希望返回所选文件的路径以供上传。现在我想如何获取所选文件的路径。我想要这样的文件路径--> /mnt/sdcard/DCIM/13.jpg

代码:

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);
}

【问题讨论】:

标签: android file


【解决方案1】:

ACTION_GET_CONTENT 不可靠,因为不要求 Uri 返回指向您可以访问的文件。该文件可能位于应用程序的内部存储中,也可能位于可移动媒体上,甚至可能位于某个地方的云中。这周感觉像是第五次......a Uri is not a file

按设计使用Uri,通过ContentResolver 上的方法,如openInputStream()getType()

【讨论】:

    【解决方案2】:

    尝试使用这些代码获取使用openFileOutput创建的文件的路径

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

    【讨论】:

      猜你喜欢
      • 2016-10-29
      • 2011-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-29
      • 1970-01-01
      • 1970-01-01
      • 2020-02-20
      相关资源
      最近更新 更多