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