0

I'm new to android and trying to create an app which has a file picker and plays a particular file. I have been able to create the intent to open files; however I'm not able to pass the file path from the file picker to the intent to open that same particular file. Code given below. Appreciate any help. thanks.

EDIT Adding lines I missed___

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 July 27, 2018

Updated my code as per @miguelarc's suggestion, but still unable to pass the name. Any suggestions or mistakes pointed out?


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

}

Lavin_38
  • 11
  • 3

1 Answers1

0

You should pass the filePath to your DecryptTask. Add a constructor to it, and add the filePath, so you can initialize your videoFile2Play with the valid path. At the moment, your file is not initializing anything, that's why the intent is not starting/showing anything.

Add a constructor like so:

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

}

then, use the filePath you got inside the decrypt() method.

miguelarc
  • 791
  • 7
  • 13
  • it doesn't seem to work, I've updated my question with the updated code. any help is appreciated. thanks @miguelarc – Lavin_38 Jul 27 '18 at 12:26
  • @Lavin_38 have you debugged it? What is the value of this.filePath in the decrypt() method called inside doInBackground() method? – miguelarc Jul 27 '18 at 13:46