How to open a file without knowing its extension, but knowing the full name?

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

Say I have a file path

/mnt/sdcard/video/my_birthday_moovie001

'my_birthday_moovie001' may be either .mkv, .mpgor .mkv. I tried adding ". ***" to the file path, but I still can not open the file.

+4
source share
4 answers

Well, I read the comments that you saved in db without extensions, there are many extensions that exist, so can not anroid automatically selects the extension you must create in order to determine the extension method.

- , , ,

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

,

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
+1

,

, ( ), , .

:

File extStore = Environment.getExternalStorageDirectory();

my_birthday_moovie001 , unnamed,

String NameOfFile = "unnamed";

videos, Downloads,

String PathWithFolder = extStore + "/Download/";

,

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

,

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

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

, @Zain Ul Abidin getListFiles(), :

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

, .

+1

From another 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 will need the ant.jar link (~ 1.3 MB for ant 1.7.1).

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

0
source

You can try this way, first get the file name and extension, and then finally compare and implement. eg:

An example file name is 04chamelon and the 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);
        }
0
source

Source: https://habr.com/ru/post/1621940/


All Articles