4
 Intent tostart = new Intent(Intent.ACTION_VIEW);
                tostart.setDataAndType(Uri.parse(video_path+".***"), "video/*");
                startActivity(tostart);

Let's say I have a file path

/mnt/sdcard/video/my_birthday_moovie001

'my_birthday_moovie001' can be either .mkv, .mpg or .mkv. I've tried to add ".***" to the file path but I still can't open the file.

Bö macht Blau
  • 12,820
  • 5
  • 40
  • 61
artouiros
  • 3,947
  • 12
  • 41
  • 54

5 Answers5

1

Well i read the comments you have stored your path in db without extensions there are many extensions that exists so android cant automatically pick the extension you have to create some way to detect extension.

following is a robust way that is best match in your case but not recommended in proper cases where extensions are known

public String chk_path(String filePath)
{
//create array of extensions
String[] ext=new String[]{".mkv",".mpg"}; //You can add more as you require

//Iterate through array and check your path which extension with your path exists

String path=null;
for(int i=0;i<ext.Length;i++)
{
  File file = new File(filePath+ext[i]);
  if(file.exists())
    { 
     //if it exists then combine the extension
     path=filePath+ext[i];
     break;
    }
}
return path; 
}

now to play a song in your code

if(chk_path(video_path)!=null)
{
 Intent tostart = new Intent(Intent.ACTION_VIEW);
                tostart.setDataAndType(Uri.parse(video_path), "video/*");
                startActivity(tostart);
}
else
 //tell user that although the path in database but file on this path do not exists
Zain Ul Abidin
  • 2,467
  • 1
  • 17
  • 29
1

Well as I put on comments

You could compare if the path matches with any filename(it doesn't contains the extension) and then if it does you got it.

You can simply do this :

Get the directory path

File extStore = Environment.getExternalStorageDirectory();

Set the file name my_birthday_moovie001 on my example I put unnamed but change it as your like

String NameOfFile = "unnamed";

Add the videos, I put it Downloads but you can change it

String PathWithFolder = extStore + "/Download/";

Create a method that lists all the files from your path

private List<String> getListFiles(File parentDir) {
    ArrayList<String> inFiles = new ArrayList<String>();
    File[] files = parentDir.listFiles();
    for (File file : files) {
        if (file.isDirectory()) {
            inFiles.addAll(getListFiles(file));
        } else {
            String AbsolutePath = file.getAbsolutePath();
            //Get the file name ex : unnamed.jpg
            String nameofFile = AbsolutePath.substring(AbsolutePath.lastIndexOf("/") + 1, AbsolutePath.length());
            //Remove the .jpg --> Output unnamed
            String fileNameWithoutExtension = nameofFile.substring(0, nameofFile.lastIndexOf('.'));
            //Add each file
            inFiles.add(fileNameWithoutExtension);
        }
    }
    return inFiles;
}

You got the names of the files doing this

List<String> files = getListFiles(new File(PathWithFolder));

Simply add a for that looks for a match of your file

for (int i = 0; i<=files.size()-1; i++){
   if(PathWithFolder.equals(files.get(i))) {
       Toast.makeText(MainActivity.this, "You got it!", Toast.LENGTH_SHORT).show();
   }
   else{
       Toast.makeText(MainActivity.this, "You don't.", Toast.LENGTH_SHORT).show();
    }
}

If you want to get the path as well and do what @Zain Ul Abidin proposed and compare it on getListFiles() method add this :

String fileExtension = nameofFile.substring(nameofFile.lastIndexOf("."));

Hope it helps.

Skizo-ozᴉʞS ツ
  • 19,464
  • 18
  • 81
  • 148
  • 1
    Thanks for this method, but I found that @Zain Ul Abidin answer would be faster.(because my files.size() will return a number with 3 zeros) Anyway thanks! – artouiros Dec 28 '15 at 11:58
0

From the other question :

Consider DirectoryScanner from Apache Ant:

DirectoryScanner scanner = new DirectoryScanner();
scanner.setIncludes(new String[]{"**/*.java"});
scanner.setBasedir("C:/Temp");
scanner.setCaseSensitive(false);
scanner.scan();
String[] files = scanner.getIncludedFiles();

You'll need to reference ant.jar (~ 1.3 MB for ant 1.7.1).

And then, run on files array and check if files[i].include(yourfile) yourfile= files[i]

Nirel
  • 1,855
  • 1
  • 15
  • 26
0

You may try in this way , first getting the name of file and extension then finally compare and implement. like this :

Example file name is 04chamelon and extension is .png:

File f = new File("/mnt/storage/sdcard/Pictures/04chameleon");
        File yourDir = new File("/mnt/storage/sdcard/Pictures");
        nametwo = f.getName();
        for (File fa : yourDir.listFiles()) {
            if (fa.isFile())
                fa.getName();
            String path = fa.getName(); // getting name and extension
            filextension = path.substring(path.lastIndexOf(".") + 1); // seperating extension
            name1 = fa.getName();
            int pos = name1.lastIndexOf(".");
            if (pos > 0) {
                name1 = name1.substring(0, pos);
            }
        }
        if (name1.equals(nametwo)) {
            Intent tostart = new Intent(Intent.ACTION_VIEW);
            tostart.setDataAndType(Uri.parse(f + "." + filextension), "image/*");
            //tostart.setDataAndType(Uri.parse(f + "." + filextension), "video/*");
            startActivity(tostart);
        }
User Learning
  • 3,165
  • 5
  • 30
  • 51
0

With the latest ContentResolver, you can easily make this work using the contentResolver.getType(uri) function which detects the filetype.

private fun getIntentForFile(intent: Intent, filePath: String, context: Context): Intent { 
    val uri = FileProvider.getUriForFile(                                                  
        context,                                                                           
        context.applicationContext.packageName + ".fileprovider",                          
        File(filePath)                                                                     
    )                                                                                      
    intent.putExtra(Intent.EXTRA_STREAM, uri)                                              
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)                                 

    intent.setDataAndType(uri, context.contentResolver.getType(uri))                       
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);                                        
    return intent                                                                          
}   
Sreekant Shenoy
  • 1,420
  • 14
  • 23