i want to open a .mp4 file, so that i can access each frame.
I tried to do it using javacv, but it didnt work.(see code below)
Does anyone know why it didnt work or how else i could achieve my goal?
i tried to call
string path = Environment.getExternalStorageDirectory().getAbsolutePath()
+ File.separator + "Test";
opencv_videoio.VideoCapture cap = new opencv_videoio.VideoCapture(path + File.separator + "test.mp4");
but then cap.isOpend() returns false. I have set the permission WRITE_EXTERNAL_STORAGE(which includes READ)
Related
I am trying to add data into a folder which is inside Android/Data/packagename. So I am trying this out:
String tempsubdirtest = Environment.getExternalStorageDirectory()
+ File.separator + "/Android/data/" + getPackageName() + "/files/Images";
File subdirecttest = new File(tempsubdirtest);
if (!subdirecttest.exists())
{
subdirecttest.mkdirs();
}
The question is that is there an easy way to reduce the code for:
Environment.getExternalStorageDirectory()
+ File.separator + "/Android/data/" + getPackageName()
instead of me typing /Android/data/ +getPackName etc.
Thanks!
You can use this existing method getExternalCacheDir
This will return the directory path plus a cache folder.
So in your case, you can just exclude that cache path.
/storage/emulated/0/Android/data/com.example.test/cache
You might gonna check this, which will return something similar to what you want.
String pathImage = getExternalFilesDir(Environment.DIRECTORY_PICTURES).getPath();
/storage/emulated/0/Android/data/com.example.test/files/Pictures
I have a problem with Android and Unity 3D. I have a file read code. When I put my code on the computer, it works. However, my code does not work on Android (Mobile). How can I solve this problem? Thank you.
FileInfo theSourceFile = new FileInfo(filename);
if (System.IO.File.Exists(fname))
{
StreamReader reader = theSourceFile.OpenText();
string text = reader.ReadLine();
print(string);
}
EDIT updated code
string filename = "file.txt";
FileInfo theSourceFile = new FileInfo(filename);
filename = Application.persistentDataPath + "/"+filename;
System.IO.File.WriteAllText(filename,"Test");
if (System.IO.File.Exists(filename))
{
StreamReader reader = theSourceFile.OpenText();
string text = reader.ReadLine();
print(string);
}
You need to change your build settings for android Device.
Change Configuration >> write access to external (sd card).
if not, your app is pointing to internal path and you need root permission in your android device.
You must use Application.persistentDataPath on Android to be able to read anything.
Change that to string filename = Application.persistentDataPath + "/file.txt"; and your code should work fine.
Bear in mind that before the read function can work, you must write to the directory first. So file.txt must exist in Application.persistentDataPath first.
For example
string filename = Application.persistentDataPath + "/file.txt";
System.IO.File.WriteAllText(filename,"Test");
EDIT:
You new code is still not working because you had FileInfo theSourceFile = new FileInfo(filename); before filename = Application.persistentDataPath + "/"+filename;. This means that the file name is still not valid. Pay attention the order your script execute. After switching it, it worked on my Android. Below is the whole code.
string filename = "file.txt";
filename = Application.persistentDataPath + "/" + filename;
System.IO.FileInfo theSourceFile = new System.IO.FileInfo(filename);
System.IO.File.WriteAllText(filename, "Test");
if (System.IO.File.Exists(filename))
{
System.IO.StreamReader reader = theSourceFile.OpenText();
string text = reader.ReadLine();
print(text);
}
I am able create and start an intent to capture a video. But how can I set the file name and save it to a specific directory?
Here's what I have so far:
videoPath = "/X1," + num1 +
",Y1," + num2 +
",X2," + num3 +
",Y2," + num4 +
",A," + num5 +
",G," + num6 +
",la," + num7 +
",lo," + num8+ ".mp4";
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, videoPath);
startActivityForResult(intent, VIDEO_INTENT);
But how can I set the file name and save it to a specific directory?
You are setting the filename. It is most of what you have in videoPath.
However:
Your videoPath does not specify a directory, other than an invalid leading slash. Use getExternalFilesDir() on Context to get a likely File object for the directory, then create a new File object pointing to the actual file you want to use.
I am not sure if commas will work well in filenames here.
EXTRA_OUTPUT is a Uri, not a String or a File. Use Uri.fromFile() to convert your File to a Uri.
i can read and write file with this method for storing game progres.
filePath = Application.dataPath + "/";
filenam= "SavedGame";
extension = ".txt";
Lettura ----------------
var FRead = new File.OpenText(filePath + fileNam + extension);
for (var i : int= 0; i<N; i++) {
FRead.ReadLine();
}
Scrittura--------------------
var FWrite: StreamWriter = new StreamWriter(filePath + filenam + extension);
FWrite.WriteLine("AAAA");
A run time in the Unity editor all work perfectly.
But in Android device Application.dataPath seems dont work.
I tryed Application.persistentDataPath that work fine but in (Read Only),but i need place the SavedGame.txt manually in the patch.
I tried a fantomatic "jar:file://" + Application.dataPath + "!/assets/"; but dont work.
Note when i build the project in apk packege semms dont appear my SavedGame.txt,that
in the editor is in UnityProject\Assets .
Come posso risalire al percorso giusto del mio SavedGame.txt, posizionato in ad esempio in UnityProject\Assets nei device?
How i cand do this in a correct way read and write SavedGame.txt in android device?
On android you might not have direct write access to the dataPath of the project.
Try using Application.persistentDataPath instead. This is where save files are intended to be stored.
Change:
filePath = Application.dataPath + "/";
to
filePath = Application.persistentDataPath + "\\";
I want to save the downloaded file into a custom folder previously created as :
String trainingDirectory = "swimmer" + File.separator + "trainings";
String trainingsPath = Environment.getExternalStorageDirectory().toString() + File.separator + trainingDirectory;
File trainingSubdirectory = new File(getFilesDir() + File.separator + trainingsPath );
trainingSubdirectory.mkdirs();
to store the downloaded file into this directory, I tried to follow the solution given : Set custom folder Android Download Manager
writing
request.setDestinationInExternalPublicDir ( "/trainings", "mydownloadedfile.mp4");
In this case , the download manager is creating a new 'training' directory , not using the one I created previously...
I tried also to use
request.setDestinationInExternalPublicDir ( "/swimmer/trainings", "mydownloadedfile.mp4");
but in this case an error is raised ( a path with separators is not accepted..)
where am I wrong ?
Use this:
String directoryPath = Environment.getExternalStorageDirectory() + "/swimmer/trainings/"
// ...
request.setDestinationUri(Uri.fromFile(new File(directoryPath + "fileName.ext")));