Search code examples
androidvideoplayback

Sorry, this video can not be played


I am getting string path as below

File f = new File(
        Environment.getExternalStoragePublicDirectory(File.separator),
        "abc");
File file[] = f.listFiles();

and then converting this file array to String array.

list = new String[file.length];
for (int i = 0; i < file.length; i++) {
    list[i] = file[i].getAbsolutePath();
}

Now in another class, I am getting these individual elements from list array and passing it to

intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.parse("file://" + list[index]));

String ext = MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(file).toString());
String type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext);

Log.d(TAG,"type  = "+type);
intent.setType(type);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(intent);

From the above code I am getting "Sorry, this video can not be played" error. I have tried many posts here but could not succeed. Please help in getting through this problem.


Solution

  • Following code worked for me. It ll be very helpful if any one explains the diff.

    String filePath = data[position];
        Log.d(TAG,"onclick of item position = "+position+" filepAth = "+filePath);
        File file = new File(filePath);
        Log.d(TAG,"file = "+file);
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
    
        String ext = MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(file).toString());
        String type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext);
    
        Log.d(TAG,"type  = "+type);
       intent.setDataAndType(Uri.fromFile(file), type);
    
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        mContext.startActivity(intent);