I have been trying to copy a file from one location to another. To my understanding the copying mechanism of my program is working properly, however when I run the app I constantly get a file path error. I happen to be working with the data files within the xbmc app.
AssetManager-->addDefaultAssets CIP path not exsit
/data/data/org.xbmc.xbmc/.xbmc/userdata/guisettings.bak: open failed: ENOENT (No such file or directory)
The problem seems to occur when I create File objects for the string paths. Here is the code snippet for that portion of the program:
File inputFile = new File(inputPath);
File outputFile = new File(outputPath);
No matter how I try to access the files, I keep getting the above error. I have tried using the File, FileInputStream, and Uri libraries to get the file paths with no luck. Could I be having issues with write privileges, or am I just not specifying the file paths correctly? I am posting the whole solution just in case the problem lies elsewhere within the code.
public class myActivity extends Activity {
private static final String TAG = "myActivity.java";
//The package name representing the application directory that is to be accessed
String packageName = "org.xbmc.xbmc";
//The input filename to read and copy
String inputFileName = "guisettings.bak";
//The output filename to append to
String outputFileName = "guisettings.xml";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
//Check the status of the external storage
if(isExternalReady()) {
//The external file system is ready
//Start the specific file operation
restoreFile();
} else {
//Not ready. Create a Broadcast Receiver and wait for the filesystem to mount
BroadcastReceiver mExternalInfoReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context arg0, Intent intent) {
//The external filesystem is now mounted
//Start the specific file operation
restoreFile();
}
};
//Get the IntentFilter Media Mounted
IntentFilter filter = new IntentFilter(Intent.ACTION_MEDIA_MOUNTED);
//Specify the filesystem data type
filter.addDataScheme("file");
//Register the receiver as "mExternalInfoReceiver" with the Media Mount filter
registerReceiver(mExternalInfoReceiver, new IntentFilter(filter));
}
}
//Restore the specific xml file
public void restoreFile() {
/*
//Get the internal storage of this app (Android/data/com.website.appname/files/)
String internalStorage = Environment.getFilesDir();
//Get the external storage path now that it is available
String externalStorage = Environment.getExternalStorageDirectory().toString();
//The directory of the file desired
String filePath = "Android/data/org.xbmc.xbmc/files/.xbmc/userdata/";
*/
//The information of the desired package
ApplicationInfo desiredPackage;
//Get the path of the application data folder if the application exists
try {
//Get the info of the desired application package
desiredPackage = getPackageInfo(packageName);
} catch (Exception e) {
//Output the stack trace
e.printStackTrace();
//Stop the function
return;
}
//Get the data dir of the package
String rootPackageDir = desiredPackage.dataDir;
//The path to the file in the package
String packageFilePath = "/.xbmc/userdata/";
//Construct the complete path of the input and output files respectively
//based on the application path
String inputPath = String.format("%s%s%s", rootPackageDir, packageFilePath, inputFileName);
String outputPath = String.format("%s%s%s", rootPackageDir, packageFilePath, outputFileName);
try {
//Copy the input file to the output file
if(copyFile(inputPath, outputPath)) {
//The file has been successfully copied
//Exit the application
System.exit(0);
}
} catch (IOException e) { e.printStackTrace(); return; }
}
//Is the external storage ready?
public boolean isExternalReady() {
//Get the current state of the external storage
//Check if the state retrieved is equal to MOUNTED
Boolean isMounted = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
if(isMounted) {
//The external storage is ready
return true;
}
//The external storage is not ready
return false;
}
//Get the data dir of a specific app if it exists
public ApplicationInfo getPackageInfo(String packageName) throws PackageNotFoundException {
List<ApplicationInfo> packages;
PackageManager pm;
//Init the package manager as pm
pm = getPackageManager();
//Get all installed applications
packages = pm.getInstalledApplications(0);
//Get the ApplicationInfo as packageInfo from each packages
for(ApplicationInfo packageInfo:packages) {
//Check for a name that matches the packageName
if(packageInfo.packageName.equals(packageName)) {
//The package exists
return packageInfo;
}
}
//The package was not found
//Throw an exception
throw new PackageNotFoundException("Package not found");
}
//Copy a file from an input directory to an output directory
public boolean copyFile(String inputPath, String outputPath) throws IOException {
//Construct the input and output paths as File objects with respective read/write privileges
//File inputFile = new File("/data/data/org.xbmc.xbmc/files/.xbmc/userdata/guisettings.bak");
//File outputFile = new File("/data/data/org.xbmc.xbmc/files/.xbmc/userdata/guisettings.xml");
//File inputFile = getDir(inputPath, MODE_PRIVATE);
//File outputFile = getDir(outputPath, MODE_PRIVATE);
File inputFile = new File(inputPath);
File outputFile = new File(outputPath);
//Check if the input and output files exist
if(!inputFile.exists()) {
return false;
}
if(!outputFile.exists()) {
//Create the output file
new File(outputPath);
}
//Get the input read state
boolean canReadInput = inputFile.canRead();
//Get the output write state
boolean canWriteOutput = outputFile.canWrite();
//Check if the input file can be read and if the output file can be written
if(canReadInput && canWriteOutput) {
//Open respective input and output buffer streams
InputStream in = new FileInputStream(inputFile);
OutputStream out = new FileOutputStream(outputFile);
//Create a byte array
byte[] buffer = new byte[1024];
//The current position of the byte buffer
int bufferPosition;
//While the bufferPosition is reading the file 1024 bytes at a time (-1 = no longer reading)
while((bufferPosition = in.read(buffer)) != -1) {
//Append the current buffer in memory to the output file
//With a pointer offset of 0 and a count of the current bufferPosition
out.write(buffer, 0, bufferPosition);
}
//Close the file streams
in.close();
out.close();
return true;
}
return false;
}
//The Package Error Class
private class PackageNotFoundException extends Exception {
//If an error is thrown with a message parameter
PackageNotFoundException(String m) {
//Pass the message to the super Exception class
super(m);
}
}
}
It turns out that my android device was having some issues connecting to my computer, hence the CIP Error.
After switching to another device, I also discovered that the input file itself was not being found, as I was trying to access the it through the "data/data" directory. The app data directories for installed apps can only be accessed through the external storage path. Since this varies from device to device, it needs to be retrieved dynamically.
Environment.getExternalStorageDirectory().getAbsolutePath();
After accessing the file this way I was able to successfully copy it.
Related
I have my first smartphone since one week and try make a App with Xamarin.
I use SQLite with EntityFrameworkCore to store data.
It is work fine, but to debug easier I want use a SQLite browser.
The database file path is 'data/data/{AppName}/Database.db'.
I debug from a physic device by USB, but when I explore the device with Windows Explorer I cannot find the SQLite DB file. The 'data/data' folder is not available. Then I can not use a SQLite browser to see the data.
In this post, the author use a Android emulator and can see 'data/data' folder :
https://blog.xamarin.com/building-android-apps-with-entity-framework/
But I prefer use a real device.
Have you a solution?
A solution from the MikeT, in development store the db file in available folder like this :
public static string DatabasePath
{
get
{
var dbFolder = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads).AbsolutePath;
var fileName = "database.db";
var dbFullPath = Path.Combine(dbFolder, fileName);
return dbFullPath;
}
}
In production, copy the db file to a available folder.
One a real device you would, I believe, need to root the device to directly access the data.
However, what you could do is to copy the database file elsewhere e.g. to external storage. In following is the core process that I use:-
try {
FileInputStream fis = new FileInputStream(dbfile);
OutputStream backup = new FileOutputStream(backupfilename);
//byte[] buffer = new byte[32768];
int length;
while((length = fis.read(buffer)) > 0) {
backup.write(buffer, 0, length);
}
backup.flush();
backup.close();
fis.close();
catch (IOException e) {
e.printStackTrace();
confirmaction = false;
}
I use the following to get the pathname for backupfilename:-
File dir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),subdirectory);
this.directory = dir.getPath();
I then add the actual filename (which is user input).
Note! I do checks to determine that EXTERNAL storage is mounted etc.
To get the database path i use:-
String dbfilename = this.getDatabasePath(
DBConstants.DATABASE_NAME).getPath();
dbfile = new File(dbfilename);
This is of course Java, but I'd assume that it could be converted/adapted to suit. The crux of the answer is to get the database file into a place from which you can access it.
Call the ExtractDb method from your activity
public void ExtractDB()
{
var szSqliteFilename = "databasename.db3";
var szSourcePath = new FileManager().GetLocalFilePath(szSqliteFilename);
var szDatabaseBackupPath = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath + "/databasename_Backup.db3";
if (File.Exists(szSourcePath))
{
File.Copy(szSourcePath, szDatabaseBackupPath, true);
Toast.MakeText(this, "Copied", ToastLength.Short).Show();
}
}
Get path to the android device storage as shown below
public class FileManager
{
public string GetLocalFilePath(string filename)
{
string path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
return Path.Combine(path, filename);
}
}
you need to add the android.permission.WRITE_EXTERNAL_STORAGE permission in your manifest file.
I want to create a XML file inside my Android app.
This file I want to write into the documents folder of my Android device.
Later I want to connect my Android device to my PC using USB and read that XML file out of the documents folder.
My Device is an Android Galaxy Tab Pro 10.1, Android 4.4.2.
I tried already:
String fileName = "example.xml";
String myDirectory = "myDirectory";
String externalStorage = Environment.getExternalStorageDirectory().getAbsolutePath();
File outputFile = new File(externalStorage + File.separator + myDirectory + File.separator + fileName);
But no file is created. I also want later to read that file out of the documents folder into may app again.
Any help is appreciated, thanks!
I know this is late, but you can get the documents directory like this:
File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);
File file = new File(dir, "example.txt");
//Write to file
try (FileWriter fileWriter = new FileWriter(file)) {
fileWriter.append("Writing to file!");
} catch (IOException e) {
//Handle exception
}
Set permission in Android Manifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Use this code to write to external directory
String fileName = "example.xml";
String dirName = "MyDirectory";
String contentToWrite = "Your Content Goes Here";
File myDir = new File("sdcard", dirName);
/*if directory doesn't exist, create it*/
if(!myDir.exists())
myDir.mkdirs();
File myFile = new File(myDir, fileName);
/*Write to file*/
try {
FileWriter fileWriter = new FileWriter(myFile);
fileWriter.append(contentToWrite);
fileWriter.flush();
fileWriter.close();
}
catch(IOException e){
e.printStackTrace();
}
Before creating file you have to create directory in which you are saving the file.
Try like this one:-
String fileName = "example.xml";
String myDirectory = "myDirectory";
String externalStorage = Environment.getExternalStorageDirectory().getAbsolutePath();
File outputDirectory = new File(externalStorage + File.separator + myDirectory );
if(!outputDirectory.exist()){
outputDirectory.mkDir();
}
File outputFile = new File(externalStorage + File.separator + myDirectory + File.separator + fileName);
outputFile.createFile();
Try restarting you device and then check if the file exists. If so, you are creating it (which it looks like you should be based on your code) but it is not showing up until the media is scanned on your device. Try implementing MediaScannerConnectionClient so it will show become visible after creation.
public class MainActivity extends Activity implements MediaScannerConnectionClient {
private MediaScannerConnection msConn;
private File example;
...
#Override
protected void onCreate(Bundle savedInstanceState) {
...
msConn = new MediaScannerConnection(this.getApplicationContext(), this);
String dir = Environment.getExternalStorageDirectory() + "/Documents/";
example = new File(dir, "example.xml");
msConn.connect();
}
#Override
public void onMediaScannerConnected() {
msConn.scanFile(example.getAbsolutePath(), null);
}
#Override
public void onScanCompleted(String path, Uri uri) {
msConn.disconnect();
}
From Android 10 onwards, Android started using Scoped Storage model to protect user privacy.
If you want to share this file with the User, then you should write this file in Shared Storage. To write a file in Shared Storage, this has to be done in 3 steps:-
Step 1: Launch System Picker to choose the destination by the user. This will return Uri of the destination directory.
private ActivityResultLauncher<Intent> launcher; // Initialise this object in Activity.onCreate()
private Uri baseDocumentTreeUri;
public void launchBaseDirectoryPicker() {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
launcher.launch(intent);
}
Step 2: Launch System Picker to choose the destination by the user. This will return the Uri of the destination directory. Also, you can optionally persist the permissions and Uri for future use.
#Override
public void onActivityResult(ActivityResult result) {
if (result.getResultCode() == Activity.RESULT_OK) {
baseDocumentTreeUri = Objects.requireNonNull(result.getData()).getData();
final int takeFlags = (Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
// take persistable Uri Permission for future use
context.getContentResolver().takePersistableUriPermission(result.getData().getData(), takeFlags);
SharedPreferences preferences = context.getSharedPreferences("com.example.fileutility", Context.MODE_PRIVATE);
preferences.edit().putString("filestorageuri", result.getData().getData().toString()).apply();
} else {
Log.e("FileUtility", "Some Error Occurred : " + result);
}
}
Step 3: Write CSV content into a file.
public void writeFile(String fileName, String content) {
try {
DocumentFile directory = DocumentFile.fromTreeUri(context, baseDocumentTreeUri);
DocumentFile file = directory.createFile("text/*", fileName);
ParcelFileDescriptor pfd = context.getContentResolver().openFileDescriptor(file.getUri(), "w");
FileOutputStream fos = new FileOutputStream(pfd.getFileDescriptor());
fos.write(content.getBytes());
fos.close();
} catch (IOException e) {
}
}
For more explanation, you can read "How to Save a file in Shared Storage in Android 10 or Higher" or Android official documentation.
I have this code which makes new Excel file.
The file is blank, it only creates a sheet.
Code goes like this
public void onClick(View v) {
Workbook workbook = new HSSFWorkbook();
Sheet sheet = workbook.createSheet("Havaji");
Cell cell = sheet.createRow(0).createCell(0);
cell.setCellValue("Hi there");
try{
FileOutputStream output = new FileOutputStream("Test2.xls");
workbook.write(output);
output.close();
}
...
Where is that file saved ?
How to manage to save a file on the location on the mobile device that i want?
How to create a directory where all the files are gonna be stored?
Here are a few methods you'll find useful for your purposes:
Creates all the directories along the path provided:
public static boolean createPath(String path) {
File pathFile = new File(path);
if (!pathFile.exists()) {
boolean result = pathFile.mkdirs();
if (!result) {
Log.e(TAG, "Unable to create directory path: " +
path);
return false;
}
}
if (!pathFile.isDirectory()) {
return false;
}
return true;
}
Returns the root of the external storage directory:
public static String extDirectory() {
File file = Environment.getExternalStorageDirectory();
return file.getAbsolutePath();
}
Returns the path to the root of an application's external storage directory:
public static String externalMyAppDataRoot(Context context) {
return externalAppDataRoot() + File.separatorChar
+ context.getPackageName() + File.separatorChar + "data";
}
Returns the path to the root of the Android application data directory:
public static String externalAppDataRoot() {
return extDirectory() + File.separatorChar + "Android/data";
}
I'm guessing that is being stored in /data/app//files/Test2.xls, though i'm not completely sure
I would try to pass in an absolute file path. If you want the file to be in the sdcard, i would use the Context.getExternalFilesDir to get the root path of the sdcard.
I am working on an android game. I want to copy a text file to external SD card when the user installs the game for the first time. The text file is important for properly running the game.
How can I do that? Where should i place the text file in eclipse source project so that when i build the apk file, my text file also gets bundled in it and when a use installs application from that apk file the text file gets copied to "SDcard\data" folder.?
What code should i write and where, so that it gets executed only once at installation time.
Thanks in advance
This is the methods I use to copy a file to the sd card when the app is first installed:
public class StartUp extends Activity {
/**
* -- Called when the activity is first created.
* ==============================================================
**/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FirstRun();
}
private void FirstRun() {
SharedPreferences settings = this.getSharedPreferences("YourAppName", 0);
boolean firstrun = settings.getBoolean("firstrun", true);
if (firstrun) { // Checks to see if we've ran the application b4
SharedPreferences.Editor e = settings.edit();
e.putBoolean("firstrun", false);
e.commit();
// If not, run these methods:
SetDirectory();
Intent home = new Intent(StartUp.this, MainActivity.class);
startActivity(home);
} else { // Otherwise start the application here:
Intent home = new Intent(StartUp.this, MainActivity.class);
startActivity(home);
}
}
/**
* -- Check to see if the sdCard is mounted and create a directory w/in it
* ========================================================================
**/
private void SetDirectory() {
if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {
extStorageDirectory = Environment.getExternalStorageDirectory().toString();
File txtDirectory = new File(extStorageDirectory + "/yourAppName/txt/");
// Create
// a
// File
// object
// for
// the
// parent
// directory
txtDirectory.mkdirs();// Have the object build the directory
// structure, if needed.
CopyAssets(); // Then run the method to copy the file.
} else if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED_READ_ONLY)) {
AlertsAndDialogs.sdCardMissing(this);//Or use your own method ie: Toast
}
}
/**
* -- Copy the file from the assets folder to the sdCard
* ===========================================================
**/
private void CopyAssets() {
AssetManager assetManager = getAssets();
String[] files = null;
try {
files = assetManager.list("");
} catch (IOException e) {
Log.e("tag", e.getMessage());
}
for (int i = 0; i < files.length; i++) {
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open(files[i]);
out = new FileOutputStream(extStorageDirectory + "/yourAppName/txt/" + files[i]);
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch (Exception e) {
Log.e("tag", e.getMessage());
}
}
}
private void copyFile(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
}
For this target the best way is make SharedPreferences or your file must be added in "assets" directory in android project.
as per link
There is the ACTION_PACKAGE_ADDED Broadcast Intent, but the application being installed doesn't receive this.
So it looks using SharedPreferences is the easiest way...
SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(this);
boolean firstRun = p.getBoolean(PREFERENCE_FIRST_RUN, true);
p.edit().putBoolean(PREFERENCE_FIRST_RUN, false).commit();
Put the file in the assets folder. Then, using whatever logic you come up with, when your app launches determine if it is the first run of the app.
If it is you can use getAssets() from an Activity to access the asset file and just copy it to wherever necessary.
Since the file on the sdcard is something that could be accidentally deleted by the user, you should probably check directly for its presence (and possibly verify contents) rather than trying to use something independent such as a shared preference to tell if this is the first run of the activity.
For purposes of potential app upgrades, you should probably put a version number in the file, and check that.
If the file is something you want to let power users manually edit (to change expert options) then you may have a little bit of a challenging situation to handle on upgrade.
ey up. ive built a simple music app that reads wav files from the sdcard and plays them.
how do i access the default media directory?
this is how i get the sdcard
public void LoadSounds() throws IOException
{
String extState = Environment.getExternalStorageState();
if(!extState.equals(Environment.MEDIA_MOUNTED)) {
//handle error here
}
else {
File sd = new File(Environment.getExternalStorageDirectory ()); //this needs to be a folder the user can access, like media
as usual the docs dont give an actual example of usage but it says this - If you're using API Level 8 or greater, use getExternalFilesDir() to open a File that represents the external storage directory where you should save your files. This method takes a type parameter that specifies the type of subdirectory you want, such as DIRECTORY_MUSIC...
how do i use it?
thank you
edit:
this makes it crash if i try to fill a spinner array with file path Strings.
File path = getExternalFilesDir(Environment.DIRECTORY_MUSIC);
File sd = new File(path, "/myFolder");
File[] sdDirList = sd.listFiles(new WavFilter());
if (sdDirList != null)
{
//sort the spinner
amountofiles = sdDirList.length;
array_spinner=new String[amountofiles];
......
final Spinner s = (Spinner) findViewById(R.id.spinner1); //crashes here
ArrayAdapter<?> adapter = new ArrayAdapter<Object>(this,
android.R.layout.select_dialog_item, array_spinner);
EDIT2:
ok so ive done this test that is supposed to write a txt file to the music directory.
i run the app, no txt file is written anywhere on the device i can find.
// Path to write files to
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC).getAbsolutePath();
String fname = "mytest.txt";
// Current state of the external media
String extState = Environment.getExternalStorageState();
// External media can be written onto
if (extState.equals(Environment.MEDIA_MOUNTED))
{
try {
// Make sure the path exists
boolean exists = (new File(path)).exists();
if (!exists){ new File(path).mkdirs(); }
// Open output stream
FileOutputStream fOut = new FileOutputStream(path + fname);
fOut.write("Test".getBytes());
// Close output stream
fOut.flush();
fOut.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
another edit: i will get this working!!
so if i use this line it creates a folder on the sdcard called 'Musictest'. dont understand??
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC + "test").getAbsolutePath();
////////////////////////////////////////////////////////////////////
Final Edit:
right so this will look for a folder called test in the devices music directory.
if it doesnt exist, it will be created.
(some fixing to be done here, error if empty) it then lists the files in the directory and adds them to an array.
public void LoadSounds() throws IOException
{
String extState = Environment.getExternalStorageState();
// Path to write files to
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC + "/test").getAbsolutePath();
if(!extState.equals(Environment.MEDIA_MOUNTED)) {
//handle error here
}
else {
//do your file work here
// Make sure the path exists
boolean exists = (new File(path)).exists();
//if not create it
if (!exists){ new File(path).mkdirs(); }
File sd = new File(path);
//This will return an array with all the Files (directories and files)
//in the external storage folder
File[] sdDirList = sd.listFiles();
if (sdDirList != null)
{
//add the files to the spinner array
array_spinnerLoad=new String[sdDirList.length];
files = new String[sdDirList.length];
for(int i=0;i<sdDirList.length;i++){
array_spinnerLoad[i] = sdDirList[i].getName();
files[i] = sdDirList[i].getAbsolutePath();
}
}
}
}
as mentioned in the docs, getExternalFilesDir() return File. And File object can represent either file or directory.
Therefore:
File musicDirectory = new File( getExternalFilesDir(Environment.DIRECTORY_MUSIC));
Will give you the object to play with.