mobile-ffmpeg: No such file or directory - java

I'm using mobile-ffmpeg to convert a .mp4 file to .mjpeg file so that it can be processed by opencv, the problem is it cannot find the file
this is my code:
File video = new File(GlobalValue.uri.getPath());
File dfanFolder = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator+"Dfan");
if(!dfanFolder.exists()){
dfanFolder.mkdir();
}
String videoPath = video.getAbsolutePath();
String mjpegPath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator+"Dfan"+File.separator+"text.mjpeg";
int rc = FFmpeg.execute("-i '" + videoPath + "' -vcodec mjpeg '" +mjpegPath+"'");
VideoCapture videoCapture = new VideoCapture();
videoCapture.open(mjpegPath);
and the error is
E/mobile-ffmpeg: /raw/storage/emulated/0/DCIM/Camera/VID_20210715_102314.mp4: No such file or directory

Related

FileNotFoundException - When creating a file and saving to external storage. - Android

I'm trying to save a file to external storage, however it's giving the FileNotFoundException error.
Note: I've already made the changes to Android Manifest.
File file = null;
if (isExternalStorageWritable()) {
Log.d("SEND", "Entrou no IF do ExernalStorage");
//File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File path = ContaListFragment.this.getActivity()
.getApplicationContext()
.getExternalFilesDir(
Environment.DIRECTORY_PICTURES);//getExternalFilesDir
file = new File(path, "file.json");
//Make sure the Pictures directory exists.
Log.d("SEND", "" + path.mkdir() + " PATH=" + file.getAbsolutePath());
//file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), "file.json");
}
if (!file.mkdirs()) {
Log.d("SEND", "Directory not created");
}
if (!isExternalStorageWritable()) {
file = new File(ContaListFragment.this.getActivity()
.getApplicationContext().getFilesDir(), "file.json");
Log.d("SEND", "Passei do File");
output = new BufferedWriter(new FileWriter(file));
Log.d("SEND", "Passou do Buffered");
output.write(jsonObject.toString());
uri = Uri.fromFile(file);
}
The issue was resolved with the help of:
See the answers to this question
The problem was that I was creating a directory instead of a file. Just replace: file.mkdirs(); with file.createNewFile();

Rename the file while preserving file extension in java

How to rename a file by preserving file extension?
In my case I want to rename a file while uploading it. I am using Apache commons fileupload library.
Below is my code snippet.
File uploadedFile = new File(path + "/" + fileName);
item.write(uploadedFile);
//renaming uploaded file with unique value.
String id = UUID.randomUUID().toString();
File newName = new File(path + "/" + id);
if(uploadedFile.renameTo(newName)) {
} else {
System.out.println("Error");
}
The above code is changing the file extension too. How can I preserve it?
Is there any good way with apache commons file upload library?
Try to split and take only the extension's split:
String[] fileNameSplits = fileName.split("\\.");
// extension is assumed to be the last part
int extensionIndex = fileNameSplits.length - 1;
// add extension to id
File newName = new File(path + "/" + id + "." + fileNameSplits[extensionIndex]);
An example:
public static void main(String[] args){
String fileName = "filename.extension";
System.out.println("Old: " + fileName);
String id = "thisIsAnID";
String[] fileNameSplits = fileName.split("\\.");
// extension is assumed to be the last part
int extensionIndex = fileNameSplits.length - 1;
// add extension to id
System.out.println("New: " + id + "." + fileNameSplits[extensionIndex]);
}
BONUS - CLICK ME

Java is putting my file path twice

Hi i have made a 2 files that is suppose to copy file, but it makes the file url twice e.g. Users/Name/Users/Name/Desktop/jar.jar
Its adding the location of the runnable jar i opened to start then my path i want.
Code:
String path1 = System.getProperty("user.dir") + File.separator + "Desktop" + File.separator + "Coding" + File.separator + "Temp";
File file = new File(path1);
String path2 = System.getProperty("user.dir") + File.separator + "Library" + File.separator + "LaunchAgents" + File.separator + "program.jar";
File file2 = new File(path2);
if(file2.exists()) {
logger.warning("File 3 def");
return;
}
File file4 = new File(file.getAbsolutePath() + File.separator + "copied.jar");
if(!file4.exists()) {
logger.warning("cp " + file4.getAbsolutePath() + " : " + file2.getAbsolutePath());
logger.warning("File 4 def");
return;
}
Log:
WARNING: cp /Users/myuser/Desktop/Coding/Temp/Desktop/Coding/Temp/program.jar : /Users/myuser/Desktop/Coding/Temp/Library/LaunchAgents/copied.jar
WARNING: File 4 def
System.getProperty("user.dir") gets the current working directory. See documentation.
Perhaps you meant System.getProperty("user.home"), which gets your home directory.

Android App using opencv: how to save an image without overwriting?

I'm using the following code to save an image to a folder when I select an option:
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
String filename = "teste.png";
File file = new File(path, filename);
filename = file.toString();
Highgui.imwrite(filename, mRgba);
But i'd like the saved image to NOT OVERWRITE the image that's already in the folder. How could I do that? Using a kind of index for each image or something like that, I think, but how?
Thanks.
Maybe something like this?
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "MyAppDir");
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.e(TAG, "failed to create directory");
return null;
}
}
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
mediaFile = new File(mediaStorageDir.getPath() + File.separator + "testimage_" + timeStamp + ".png");

Create a File in a folder

In Java, I have a File object representing a folder:
String folderName = "/home/vektor/folder";
File folder = new File(folderName);
Now I want to create another File representing a file in this folder. I want to avoid doing a string concatenation like this:
String fileName = "test.txt";
File file = new File(folderName + "/" + fileName);
Because if I go deeper in creating this structure, I will come up with something like this:
File deepFile = new File(folderName + "/" + anotherFolderName + ... + "/" + fileName);
I would instead like to do something like
File betterFile = folder.createUnder(fileName);
Or even:
File otherFile = SomeFileUtils.createFileInFolder(folder, fileName);
Do you know of such solution?
Note: It's quite OK to use "/" because Java will translate it to "\" for Windows, but it is not clean - I should use something like "file.separator" from System.getProperties().
Look at the Javadoc for File and you will see that the constructor takes a File object as parent.
Use the following form:
File deepFile = new File(folder, fileName);
I would use
String folderName =
String fileName =
File under = new File(folderName, fileName);
or
File folderFile =
String fileName =
File under = new File(folderFile, fileName);
simple as that ;)

Categories

Resources