将文件名传递给意图

时间:2018-07-23 08:00:38

标签: java android android-intent

我是android的新手,正在尝试创建一个具有文件选择器并播放特定文件的应用。我已经能够创建打开文件的意图;但是我无法将文件选择器中的文件路径传递给打开同一特定文件的意图。代码如下。感谢任何帮助。谢谢。

EDIT 添加我错过的行___

public class MainActivity extends ListActivity {
private List<String> item = null;
private List<String> path = null;
public String root = "/mnt/usbhost0";
public TextView myPath;


   @Override

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myPath = (TextView) findViewById(R.id.path);
        getDir(root);
    }


    public void getDir(String dirPath)
    {
        myPath.setText("Location: " + dirPath);
        item = new ArrayList<String>();
        path = new ArrayList<String>();
        File f = new File(dirPath);
        File[] files = f.listFiles();
        if (!dirPath.equals(root))
        {
            item.add(root);
            path.add(root);
            item.add("../");
            path.add(f.getParent());
        }


        for (int i = 0; i < files.length; i++)
        {
            File file = files[i];
            path.add(file.getPath());
            if (file.isDirectory())
                item.add(file.getName() + "/");
            else
                item.add(file.getName());
        }

        ArrayAdapter<String> fileList =
                new ArrayAdapter<String>(this, R.layout.row, item);
        setListAdapter(fileList);
    }

    File file;


    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        File file = new File(path.get(position));
        if (file.isDirectory())
        {
            if (file.canRead())
                getDir(path.get(position));
            else
            {
                new AlertDialog.Builder(this)
                        .setIcon(R.drawable.icon)
                        .setTitle("[" + file.getName() + "] folder can't be read!")
                        .setPositiveButton("OK",
                                new DialogInterface.OnClickListener() {


                                    @Override

                                    public void onClick(DialogInterface dialog, int which) {
                                        // TODO Auto-generated method stub

                                    }

                                }).show();

            }

        } else

        {
            new AlertDialog.Builder(this)
                    .setIcon(R.drawable.icon)
                    .setTitle("Select")
                    .setMessage("Select " + file.getName() + " to play ?") //Send fileurl from here //
                    .setPositiveButton("Select", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            new DecryptTask().execute();
                        }
                    })
                    .setNegativeButton("No", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    }).show();


        }


    }

    void decrypt() throws IOException, NoSuchAlgorithmException,
            NoSuchPaddingException, InvalidKeyException {
        File videoFile2Play = new File(//to here);
        Intent i = new Intent();
        i.setAction(android.content.Intent.ACTION_VIEW);
        i.setDataAndType(Uri.fromFile(videoFile2Play), "video/m4v");
        startActivity(i);
    }

    public class DecryptTask extends AsyncTask<String, String, String> {
        @Override
        protected String doInBackground(String... params) {
            try {
                decrypt();
            } catch (InvalidKeyException e) {
                e.printStackTrace();
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            } catch (NoSuchPaddingException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    }
}

EDIT 2018年7月27日

按照@miguelarc的建议更新了我的代码,但仍然无法传递名称。指出任何建议或错误吗?


public class MainActivity extends ListActivity {
private List<String> item = null;
private List<String> path = null;
public String root = "/mnt/usbhost0";
public TextView myPath;


@Override

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    myPath = (TextView) findViewById(R.id.path);
    getDir(root);
}


public void getDir(String dirPath)
{
    myPath.setText("Location: " + dirPath);
    item = new ArrayList<String>();
    path = new ArrayList<String>();
    File f = new File(dirPath);
    File[] files = f.listFiles();
    if (!dirPath.equals(root))
    {
        item.add(root);
        path.add(root);
        item.add("../");
        path.add(f.getParent());
    }


    for (int i = 0; i < files.length; i++)
    {
        File file = files[i];
        path.add(file.getPath());
        if (file.isDirectory())
            item.add(file.getName() + "/");
        else
            item.add(file.getName());
    }

    ArrayAdapter<String> fileList =
            new ArrayAdapter<String>(this, R.layout.row, item);
    setListAdapter(fileList);
}

File file;


@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    final File file = new File(path.get(position));
    if (file.isDirectory())
    {
        if (file.canRead())
            getDir(path.get(position));
        else
        {
            new AlertDialog.Builder(this)
                    .setIcon(R.drawable.icon)
                    .setTitle("[" + file.getName() + "] folder can't be read!")
                    .setPositiveButton("OK",
                            new DialogInterface.OnClickListener() {


                                @Override

                                public void onClick(DialogInterface dialog, int which) {
                                    // TODO Auto-generated method stub

                                }

                            }).show();

        }

    } else

    {
        new AlertDialog.Builder(this)
                .setIcon(R.drawable.icon)
                .setTitle("Select")
                .setMessage("Select " + file.getName() + " to play ?")
                .setPositiveButton("Select", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        new DecryptTask(file.getAbsolutePath()).execute();
                    }
                })
                .setNegativeButton("No", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).show();


    }


}

void decrypt(String filePath) throws IOException, NoSuchAlgorithmException,
        NoSuchPaddingException, InvalidKeyException {
    File extStore = Environment.getExternalStorageDirectory();
    File videoFile2Play = new File(file.getAbsolutePath());
    Intent i = new Intent();
    i.setAction(android.content.Intent.ACTION_VIEW);
    i.setDataAndType(Uri.fromFile(videoFile2Play), "video/*");
    startActivity(i);
}


public class DecryptTask extends AsyncTask<String, String, String> {
    ProgressDialog pd;
    String filePath;

    public DecryptTask(String filePath){
        this.filePath = filePath;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pd = new ProgressDialog(MainActivity.this);
        pd.setMessage("Loading your video");
        pd.show();

    }




    @Override
    protected String doInBackground(String... params) {

        try {
            decrypt(this.filePath);
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        pd.dismiss();
    }
}

}

1 个答案:

答案 0 :(得分:0)

您应该将filePath传递给DecryptTask。向其添加构造函数,并添加filePath,以便可以使用有效路径初始化videoFile2Play。目前,您的文件没有初始化任何东西,这就是为什么意图没有开始/显示任何东西的原因。

添加这样的构造函数:

public class DecryptTask extends AsyncTask<String, String, String> {
    String filePath;

    public DecryptTask(String filePath){
        this.filePath = filePath;
    }

    @Override
    protected String doInBackground(String... params) {
        try {
            decrypt(this.filePath); //<--- Add filePath as param of decrypt method
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}

然后,使用您在decrypt()方法中获得的filePath。