I have written an app to download a .PDF file from the given url path.
But when I run it in the vm,I found my app cannot download the file.
So I tryed the DownloadManager in the vm,still it cannot download the file .
And the DownloadManage stopped with just and always showing "OnGoing".
Can anyone tell me the reason and how to solve this problem?
Thanks in advance!
PS:I write this
public File writeFileToSDCard(String path,String fileName,InputStream in){
File file = null;
OutputStream out = null;
try{
if(!createDir(path)){
Log.e("FileUtils.java", "writeFileToSDCard().createDir() failed!");
}else{
Log.e("FileUtils.java", "writeFileToSDCard().createDir() Successed!");
}
file = createFile(path+fileName);
out = new FileOutputStream(file);
byte buffer[] = new byte[128];
//write the file
do{
int length = in.read(buffer);
if(length != -1){
Log.e("FileUtils.java", "Writing to card!");
out.write(buffer, 0, length);
}else{
break;
}
}while(true);
}catch(Exception e){
Log.e("FileUtils.java", "writeFileToSDCard() failed!");
}finally {
try{
out.close();
}catch (Exception e) {
Log.e("FileUtils.java", "writeFileToSDCard() finally failed!");
}
}
return file;
}
*PSS:*I dont think my code goes wrong,but Is it the problem of the android emulator?
Most probably it is because you don't have permission to download the file from the server.
I had a similar problem few days back, I solved it by creating a .htacces file in the server directory. Create a file named .htacess containing this :
satisfy any
and save it in the directory where your pdf file is. I am not familiar with how this works, but I guess it has something to do with the permissions. Note that if you do this anyone can access the contents of that directory. And I believe this will only wirk for Apache servers.
Don't know if it will work, but its worth a shot.
Related
So far I've successfully integrated Dropbox API with my project. For that I've been using an example program that is given in Dropbox SDK. From the way i can download(random Picture) and upload files easily. My question is, How can we download a folder or multiple files at a time from their dropbox account? . Additionally When i click on the download button it randomly chosen one image file then displaying, instead of doing this i want to download all image files or particular folder. Any suggestions or help would be appreciated.
Thanks in Advance.
Hi please go through following code, may be this is helpful to you.
private boolean downloadDropboxFile(String dbPath, File localFile) throws IOException{
BufferedInputStream br = null;
BufferedOutputStream bw = null;
try {
if (!localFile.exists()) {
localFile.createNewFile(); //otherwise dropbox client will fail silently
}
FileDownload fd = api.getFileStream("dropbox", dbPath, null);
br = new BufferedInputStream(fd.is);
bw = new BufferedOutputStream(new FileOutputStream(localFile));
byte[] buffer = new byte[4096];
int read;
while (true) {
read = br.read(buffer);
if (read <= 0) {
break;
}
bw.write(buffer, 0, read);
}
} finally {
//in finally block:
if (bw != null) {
bw.close();
}
if (br != null) {
br.close();
}
}
return true;
}
For more information please check the source.
If you need to get all FILES and FOLDER from drop box, then simply copy and paste this code in your project and see the result. It will help you a lot.
String mPath="/"; //You can change the path here to specific FOLDER
Entry dirent = null;
try
{
dirent = mApi.metadata(mPath, 1000, null, true, null);
}
catch (DropboxException e)
{
System.out.println("Error Detail "+e.getMessage());
}
//Perform a Loop and retrieve all FILES and FOLDER from the PATH
for (Entry ent: dirent.contents)
{
String name = ent.fileName();
System.out.println("My File in Folder "+name);
}
The Sync API works by downloading files on-demand when you open them. So just open all the files you're trying to read.
I'm trying to figure how to store my application's data for the long term. Basically I get a list of data from from a web service, and I don't want to go back to the web service the next time the app runs. I'd prefer to just store it locally. How do I do this?
I don't mind serialising the data to any particular format. I don't see this on the Xamarin site for Android. There's a tutorial for iOS, but I'm not interested in that.
I personally copy the data from webservice in raw format in a text file on the memory.
So, I have just to open the inputStream from the file the same I did from the webservice and my code remains clean.
But I guess there are indeed thousands of ways to copy this data.
I just wanted to share the one I found more convenient.
The code just for information:
InputStream source = getStreamFromWebservice();// <= YOUR CODE HERE
File dir = context.getDir("CACHE", Context.MODE_PRIVATE);
dir.mkdirs();
File file = new File(dir, fileName);
// Write to Memory
try {
FileOutputStream f = new FileOutputStream(file);
byte[] buffer = new byte[32768];
int read;
try {
while ((read = source.read(buffer, 0, buffer.length)) > 0) {
f.write(buffer, 0, read);
}
} catch (IOException e) {
e.printStackTrace();
}
f.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
}
Can we push a database that is created by some ide like sqlitestudio and push it into our emulator for app uses?
is there any way to push our ".db" format into andriod emulator?
I think you want to ship you application by creating database outside ,
these are good tutorial to add database to your application , and these are few good tutorials to start with
http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/
http://mfarhan133.wordpress.com/2010/10/24/database-crud-tutorial-for-android/
http://www.anotherandroidblog.com/2010/08/04/android-database-tutorial/
If your device is an emulator or is a physical device connected thru USB, you can use this command-line:
adb push c:\local_path\myfile.db /path_on_the_device/myfile
You better add db into assets and copy it into sd or internal storage.
Here is some code snippet for you
private void CopyFileFromAssets() {
AssetManager asm = getAssets();
String[] files = null;
try {
files = asm.list("");
} catch (IOException e) {
Log.e("tag", e.getMessage());
}
for(String filename : files) {
InputStream in = null;
OutputStream out = null;
try {
in = asm.open(filename);
//you can even create folder to put your file
out = new FileOutputStream("/sdcard/" + filename);
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);
}
}
Hope this can help
Go to file explorer - data - data - your pkg name - select database and click on push a file onto the device.
First remove the db extension.
1.then select DDMS
2.then select emulator from device tab
3.move to data/data//databases
4.now push file into emulator using the top-right open in window.
5.Now run the app.
Thank you.
I have several files stored in my project /res/values folder, is there any way to open and read these files from my android application? Each file contains text informations about one level of my game.
I really appreciate any help.
I find what I needed here:
http://developer.android.com/guide/topics/data/data-storage.html
"If you want to save a static file in your application at compile time, save the file in your project res/raw/ directory. You can open it with openRawResource(), passing the R.raw. resource ID. This method returns an InputStream that you can use to read the file (but you cannot write to the original file). "
Sorry if My question was not clear.
And big thanks to Radek Suski for some additional information and example. I appreciate that.
As far I know you can either access files within the directory "files" from your project directory or from the SD-Card.
But no other files
EDIT
FileInputStream in = null;
InputStreamReader reader = null;
try {
char[] inputBuffer = new char[256];
in = openFileInput("myfile.txt");
reader = new InputStreamReader(in);
reader.read(inputBuffer);
String myText = new String(inputBuffer);
} catch (Exception e) {;}
finally {
try {
if (reader != null)reader.close();
} catch (IOException e) {; }
try {
if (in != null)in.close();
} catch (IOException e) {;}
}
Then your file will be located in:
/data/data/yourpackage/files/myfile.txt
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");