Copy Video From One location to another in Android - android

I m stuck with an issue that I want to copy/move a video from one location to another inside sdcard in android.
does anybody know how to do that.?

private String CopyFile(String srcPath,String destPath)
{
String videoFileName = VIDEO_FILE_PREFIX + mCurrentQuestion.getQuestionId() + VIDEO_FILE_SUFFIX ;
File source= new File(srcPath);
File destination= new File(destPath+"/"+videoFileName);
source.renameTo(destination);
return destPath+"/"+videoFileName;
}

Related

Where to store files for user in Android?

I have to export files from my application and looking for a solution, where I can save files, to give the user the possibility to open them.
I tried already getFilesDir().getPath() which worked well, until I realized that the folder can't open from a real device (/data/user/0/com.myapplication.example/files) since the /data path is just a storage area for the application.
What are the alternatives?
You should have a look here https://developer.android.com/training/data-storage
I'm not sure what file type you are trying to store however what you have tried stores the file withing the applications directory and not the devices. To combat this I would look under either Media or Documents and other files again in the above link. I would be able to be of further assistance if I knew what file type you are trying to store. Hope this helps you in some way.
This is a function to store a float array to the phone external storage. Pass the file name.extension in the String name. You could modify it to export your file.
public static void save(float[] input_array, String name)
{
final String TAG2 = "->save()";
String string_array = Arrays.toString(input_array);
String fullName = Environment.getExternalStorageDirectory().getPath() + "/SercanFolder/" + name;
String path = Environment.getExternalStorageDirectory().getPath() + "/SercanFolder";
File folder = new File(path);
if(!folder.exists())
{
folder.mkdirs();
}
BufferedWriter buf;
try
{
buf = new BufferedWriter(new FileWriter(fullName));
buf.write(string_array,0,string_array.length());
buf.close();
Log.d(TAG+TAG2, "array saved as document. ");
}
catch (IOException e)
{
Log.e(TAG+TAG2, "problems while saving the file. ");
}
}
The suggestion with getExternalStorage().getPath() (Thanks to blackapps) helped me to save the pdf in a folder, which can be opened in the file manager.

unity how to use www to load mp3 file with absolute path on android

I'm making a mobile rhythm game. I get absolute path of song like this.
/storage/emulated/0/Music/a.mp3
I want to convert the mp3 file to audioClip.
But I can't a receive mp3 file from absolute path.
here is my code :
IEnumerator GetSoundFile(string path)
{
//path : '/storage/emulated/0/Music/a.mp3'
testText.text = path;
source = GetComponent<AudioSource>();
using (var www = new WWW(path))
{
yield return www;
source.clip = www.GetAudioClip();
}
SoundManager.instance.PlaySingle(source.clip);
}
How can I receive a music file?
You need to get the persistent path for android location , Try this
https://docs.unity3d.com/ScriptReference/Application-persistentDataPath.html
In android, add this function to get correct path:
...
path = GetUri(path);
...
private string GetUri(string uri)
{
if (uri.Contains("://") || uri.Contains(":///"))
return uri;
return "file://" + uri;
}

Unable to create file in Android app

I am making a android app in which i am going to use File handling but to do that first i need to create it but by using following code:
File logf = new File("log1.txt");
Boolean bb = logf.exists();
if(!bb)
try {
bb = logf.createNewFile();
} catch (IOException e) {
msg.setText("not able to create file.");
}
'msg' is the TextView object which i am using to display error, and when i run this app.. it goes into IOException e catch.. Please tell me if i am doing something wrong or what?.. besides i am using Android 2.2 for my app.
In app storage, please try this:
File file = new File(getFilesDir(), "file.txt");
Or if you want to append file name, try this:
File file = new File(getFilesDir() + File.separator + "file.txt");
you need to provide absolute path of file like below.
File file = new File(/data/packagename/files/ + "log1.txt");

Unity 3D Android and File

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

Android folder monitor

Am using sdcard for saving data ,my problem is that when app is running and some one delete this folder then the application fail. Is there anyway to notify the app , when this folder get deleted ?. Please help me
Try like this.
private static File logFile = null;
String SD_CARD_PATH = Environment.getExternalStorageDirectory().toString();
logFile = new File(SD_CARD_PATH + "/" + "folderName");
if (logFile.exists()){
//exist.so do your work
}
else{
//not exists
}

Categories

Resources