I am new to Android and Samba. I am trying to use the JCIFS copy. To method to copy a file from a Samba directory to the 'Download' directory under sdcard on an Android 3.1 device. Following is my code:
from = new SmbFile("smb://username:password#a.b.c.d/sandbox/sambatosdcard.txt");
File root = Environment.getExternalStorageDirectory();
File sourceFile = new File(root + "/Download", "SambaCopy.txt");
to = new SmbFile(sourceFile.getAbsolutePath());
from.copyTo(to);
I am getting a MalformedURLException on the 'to' file. Is there a way to get around this problem using the copyTo method, or is there an alternate way to copy a file from the samba folder to the sdcard folder using JCIFS or any other way? Thanks.
The SmbFile's copyTo() method lets you copy files from network to network. To copy files between your local device and the network you need to use streams. E.g.:
try {
SmbFile source =
new SmbFile("smb://username:password#a.b.c.d/sandbox/sambatosdcard.txt");
File destination =
new File(Environment.DIRECTORY_DOWNLOADS, "SambaCopy.txt");
InputStream in = source.getInputStream();
OutputStream out = new FileOutputStream(destination);
// Copy the bits from Instream to Outstream
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
// Maybe in.close();
out.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Related
I'm doing an application with alot of data in the database so I need to make an external database with SQlite browser and put it in my application
my problem is
I can't see data/data/database because my mobile isn't rooted and I can't root it for reasons
I'm using BlueStack emulator in Eclipse IDE
Please help
Thank you
you can create your database and put it in your assets directory
and on first use copy to data directory.
try {
String destPath = "/data/data/" + getPackageName()
+ "/databases/YOURDbFileName";
File f = new File(destPath);
if(!f.exists()){
Log.v(TAG,"File Not Exist");
InputStream in = getAssets().open("YOURDbFileName");
OutputStream out = new FileOutputStream(destPath);
byte[] buffer = new byte[1024];
int length;
while ((length = in.read(buffer)) > 0) {
out.write(buffer, 0, length);
}
in.close();
out.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
Log.v("TAG","ioexeption");
e.printStackTrace();
}
You need to copy the external database into a folder that is accessible by the phone. So that you can manipulate the database from there.
new location must be: "/data/data/your.app.package/databases/yourdatabasename"
I am developing an android app. I have tested the app in mobile and now i need to see the database of my app(i.e mobile database). I am using Mac and confused how to pull the database from the mobile and see it in SQLite browser.
Thanks for your help guys.
You won't be able to on a real device (unless it is rooted). The data directories are protected against access by anything other than the owning app.
To get it off of the device you need to set up a method in your app to copy the database to somewhere that is accessible (like an SD card). Following is code I've used to do that before:
public void backup() {
try {
File sdcard = Environment.getExternalStorageDirectory();
File outputFile = new File(sdcard,
"yourDB.bak");
if (!outputFile.exists())
outputFile.createNewFile();
File data = Environment.getDataDirectory();
File inputFile = new File(data,
"data/your.package.name/databases/yourDB");
InputStream input = new FileInputStream(inputFile);
OutputStream output = new FileOutputStream(outputFile);
byte[] buffer = new byte[1024];
int length;
while ((length = input.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
output.flush();
output.close();
input.close();
} catch (IOException e) {
e.printStackTrace();
throw new Error("Copying Failed");
}
}
I have an SQLite database in my application. When I go to check if the database is created or not in File Explorer, there is no any database file within the data folder. It didn't even show up using the adb shell command. Does this mean that the database hasn't been created yet? Any suggestions on how to solve this?
If you are using an actual device, you will not be able to see or access it from outside unless you root your phone.
If you are using the emulator, the DDMS view will let you navigate to it and pull it from there.
Or, you could have an issue with creating your DB. Without seeing your code we cannot tell.
EDIT
If you want to get the file off of a real device, you'll need to implement a method to copy it from your data directory to someplace where you do have access (such as your SD card). This would do the trick:
public void backup() {
try {
File sdcard = Environment.getExternalStorageDirectory();
File outputFile = new File(sdcard,
"YourDB.bak");
if (!outputFile.exists())
outputFile.createNewFile();
File data = Environment.getDataDirectory();
File inputFile = new File(data, "data/your.package.name/databases/yourDB");
InputStream input = new FileInputStream(inputFile);
OutputStream output = new FileOutputStream(outputFile);
byte[] buffer = new byte[1024];
int length;
while ((length = input.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
output.flush();
output.close();
input.close();
} catch (IOException e) {
e.printStackTrace();
throw new Error("Copying Failed");
}
}
I have an interesting problem: My application is designed to send and open up a zip full of files, and the zip has a special extension (easier for the user). I can zip up the files I need to attach in an e-mail, and I can send them.
When I use the g-mail "view" button and select my app to open the file, it doesn't unzip them correctly. However, if I use the gmail "download" button, and then open the file through a file explorer, the file unzips correctly.
This is the code I use to download the attachment:
// get attachment
try {
attachment = getContentResolver().openInputStream(
getIntent().getData());
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// Save it
try {
File root = Environment.getExternalStorageDirectory();
path = root.getPath() + "/PSattachment.psz";
savedFile = new File(path);
FileOutputStream fos = new FileOutputStream(savedFile, false);
BufferedOutputStream os = new BufferedOutputStream(fos);
byte[] buffer = new byte[1024];
int byteRead = 0;
while ((byteRead = attachment.read(buffer)) != -1) {
os.write(buffer, 0, byteRead);
}
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
Am I doing something wrong? Thanks in advance. (Also, the process of unzipping is the same in both cases [file explorer and view from email], so I'm pretty sure it's something in here. Also, the file DOES download, and is the right size. It just won't unzip).
I found the answer!!! Took a while, but at least it works now:
try {
InputStream attachment = getContentResolver()
.openInputStream(getIntent().getData());
savedFile = new File(Environment
.getExternalStorageDirectory().getAbsolutePath(),
"temp" + System.currentTimeMillis() + ".psz");
FileOutputStream f = new FileOutputStream(savedFile);
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = attachment.read(buffer)) > 0) {
f.write(buffer);
}
f.close();
} catch (Exception e) {
}
I just used this code to download the attachment and now everything works perfectly =D
Check this out please:
http://www.jondev.net/articles/Unzipping_Files_with_Android_(Programmatically)
A guide to unzip files in android, hope it helps solve your problem
I have downloaded a file from HttpConnection using the FileOutputStream in android and now its being written in phone's internal memory on path as i found it in File Explorer
/data/data/com.example.packagename/files/123.ics
Now, I want to open & read the file content from phone's internal memory to UI. I tried to do it by using the FileInputStream, I have given just filename with extension to open it but I am not sure how to mention the file path for file in internal memory,as it forces the application to close.
Any suggestions?
This is what I am doing:
try
{
FileInputStream fileIn;
fileIn = openFileInput("123.ics");
InputStream in = null;
EditText Userid = (EditText) findViewById(R.id.user_id);
byte[] buffer = new byte[1024];
int len = 0;
while ( (len = in.read(buffer)) > 0 )
{
Userid.setText(fileIn.read(buffer, 0, len));
}
fileIn.close();
} catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
String filePath = context.getFilesDir().getAbsolutePath();//returns current directory.
File file = new File(filePath, fileName);
Similar post here
read file from phone memory
If the file is where you say it is, and your application is com.example.packagename, then calling openFileInput("123.ics"); will return you a FileInputStream on the file in question.
Or, call getFilesDir() to get a File object pointing to /data/data/com.example.packagename/files, and work from there.
I am using this code to open file in internal storage. i think i could help.
File str = new File("/data/data/com.xlabz.FlagTest/files/","hello_file.xml");