Java 如何从特定路径显示文件?

Java 如何从特定路径显示文件?,java,android,android-studio,Java,Android,Android Studio,我想显示来自特定路径的文件,我将在RecyclerView中实现它。但我不知道如何显示该文件 这是我的MainActivity.java @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn_add = findViewById(R.

我想显示来自特定路径的文件,我将在RecyclerView中实现它。但我不知道如何显示该文件

这是我的MainActivity.java

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

    btn_add = findViewById(R.id.btn_add);
    tv_filePath = findViewById(R.id.tv_filePath);

    btn_add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.setType("*/*");
            startActivityForResult(intent, 10);
        }
    });
}

    @Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
        case 10:
            if (resultCode == RESULT_OK) {
                String path = data.getData().getPath();
                tv_filePath.setText(path);
                uri = Uri.fromFile(new File(path));
                toDisplay = new File(uri.getPath());
            }
            break;
    }
}

我已经有了文件路径,如何从文件路径显示该文件?

使用Volley库,您可以将字符串、图像和视频等数据发送到服务器

 public void stringpost(){

        String url = "you web service url ";

        final String test = "test";




        StringRequest istek = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.e("Cevap",response);



                try{
                    JSONObject jsonObject = new JSONObject(response);
                    String succes = jsonObject.getString("success");







                } catch (JSONException e){
                    e.printStackTrace();

                }




            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        }){

            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String,String> params = new HashMap<>();

                params.put("test",test);


                return params;
            }
        };
        Volley.newRequestQueue(getActivity()).add(istek);



    }
public void stringpost(){
String url=“您的web服务url”;
最终字符串测试=“测试”;
StringRequest istek=新的StringRequest(Request.Method.POST,url,new Response.Listener()){
@凌驾
公共void onResponse(字符串响应){
Log.e(“Cevap”,答复);
试一试{
JSONObject JSONObject=新JSONObject(响应);
String succes=jsonObject.getString(“成功”);
}捕获(JSONException e){
e、 printStackTrace();
}
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
}
}){
@凌驾
受保护的映射getParams()引发AuthFailureError{
Map params=新的HashMap();
参数put(“测试”,测试);
返回参数;
}
};
newRequestQueue(getActivity()).add(istek);
}

您可以将字符串列表中的文件exist in folder传递给适配器

File folder = new File(your path);
String[] files = folder.list(); 

//folder.list(); gives you the name of all files inside that path
//folder.listFiles(); gives you the file of all files inside that path

list <String> filesNames = Arrays.asList(files);
//then u can pass files name to adapter and set your recyclerView
File folder=新文件(您的路径);
String[]files=folder.list();
//folder.list();提供该路径中所有文件的名称
//folder.listFiles();提供该路径中所有文件的文件
list filesNames=Arrays.asList(文件);
//然后,您可以将文件名传递给适配器并设置您的recyclerView

您需要添加更多信息才能对此负责。它是什么类型的文件?“显示文件”是什么意思?显示它的内容?显示路径?“我已经有了文件路径”--不,您没有。
Uri
不是文件<只有当
Uri
的方案是
file
时,
Uri
上的code>getPath()才有意义。很可能,你的计划不是。除此之外,“显示文件”是什么意思?你的意思是打开其他应用程序来查看文件吗?你的意思是将文件内容读入你的应用程序吗?你是说别的吗?文件的大小取决于用户单击的内容。它可以是docx、pptx、xlsx和图像文件。就像文件管理器一样,我想显示在RecyclerView中选择的文件,当我们单击该文件时,我们可以读取它。你说的“显示文件”和“我们可以读取它”是什么意思?您是否希望让其他现有应用程序(如Microsoft Office)显示内容?或者,您希望在自己的应用程序中显示其内容,或者使用某些商业第三方库?是的,我正在寻找其他现有应用程序来显示内容。