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.
Related
My application allows users to select an image to upload. When users select an image from a picasa album my data intent comes back with dat=content://com.sec.android.gallery3d.provider/picasa/item/....
Apparently when selecting an image from a picasa folder, I must handle getting the image differently as noted in this answer.
But before I implement a fix, I want to be able to reproduce the crash so I can verify my fix actually works. So how can I get a Picasa folder on my new (marshmallow) Android test device since Picasa has been killed by Google?
The most guaranteed way of getting a file send inside an intent, is to open a stream to it and copy it over to a private folder on your app.
This way works for local file, content uri, picasa, all of it.
Something like that:
private File getSharedFile() {
Uri uri = intent.getExtras().getParcelable(Intent.EXTRA_STREAM);
// or using the new compat lib
Uri uri = ShareCompat.IntentReader(this).getStream();
InputStream is = null;
OutputStream os = null;
try {
File f = ... define here a temp file // maybe getCacheDir();
is = getContentResolver().openInputStream(uri);
os = new BufferedOutputStream(new FileOutputStream(f));
int read;
byte[] bytes = new byte[2048];
while ((read = is.read(bytes)) != -1) {
os.write(bytes, 0, read);
}
return f;
} catch (Exception e) {
... handle exceptions, buffer underflow, NPE, etc
} finally {
try { is.close(); } catch (Exception e) { /* u never know */ }
try {
os.flush();
os.close();
} catch (Exception e) { /* seriously can happen */ }
}
return null;
}
i am able to get the path of the picture i want to copy, and able to get the path from where i want it to be copy, but still cant find the way to copy them.
any suggestion?
private void copyPictureToFolder(String picturePath, String folderName)
throws IOException {
Log.d("debug", folderName);
Log.d("debug", picturePath);
try {
FileInputStream fileInputStream = new FileInputStream(picturePath);
FileOutputStream fileOutputStream = new FileOutputStream(folderName+"/");
int bufferSize;
byte[] bufffer = new byte[512];
while ((bufferSize = fileInputStream.read(bufffer)) > 0) {
fileOutputStream.write(bufffer, 0, bufferSize);
}
fileInputStream.close();
fileOutputStream.close();
} catch (Exception e) {
Log.d("disaster","didnt work");
}
}
thanks.
You should use Commons-IO to copy a file, we are in 2013 ! No one wants do that manually. If you really want then you should consider a few things :
first a loop that copies your file, at every iteration you copy buffer.length bytes. In you current code, you don't loop and copy 512 bytes of source image into dest (whatever the source image size is).
take care of last iteration and only copy what you read
your try/catch structure is not correct, you should add a finally close to always close your source and destination file. Look here for an example : what is the exact order of execution for try, catch and finally?
With IOUtils, it will give something like
try {
IOUtils.copy( source, dest );
} finally {
IOUtils.closeQuietly( source );
IOUtils.closeQuietly( dest );
}
and don't catch anything, it will be forwarded to the caller.
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.
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