======================
LOOK! LICENSING TERMS
======================
look! is licensed under the BSD 3-Clause (also known as "BSD New" or
"BSD Simplified"), as follows:
Copyright (c) 2010-2012, Look...
If you think the Android project Look listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
/**
*-----------------------------------------------------------------------------
* Copyright (c) 2012, Look! Development Team
* All rights reserved./*fromwww.java2s.com*/
*
* Distributed under the terms of the BSD Simplified License.
*
* The full license is in the LICENSE file, distributed with this software.
*-----------------------------------------------------------------------------
*/package es.ucm.look.data.filesManager;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import es.ucm.look.data.remote.ConfigNet;
/**
* A file manager for accessing binary files remotely
*
* @author Sergio
*
*/publicclass LookFilesManager {
privatestatic LookFilesManager instance;
/**
* Base directory
*/privateFile directory;
private LookFilesManager() {
directory = Environment.getExternalStorageDirectory();
}
/**
* Method singleton to access a unique instance of LookFilesManager
* @return
*/publicstatic LookFilesManager getFileManager() {
if (instance == null)
instance = new LookFilesManager();
return instance;
}
/**
* Set the basis directory for the file manager
*
* @param directory
* the basis directory
*/publicvoid setDirectory(String directory) {
File f = newFile(directory);
f.mkdirs();
this.directory = f;
}
/**
* Returns an input stream for a file
*
* @param uriFile
* uri file
* @return the input stream
*/public InputStream getInputStream(String uriFile) {
File f = newFile(directory, uriFile);
try {
returnnew FileInputStream(f);
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}
}
/**
* Returns the bitmap for the given file
*
* @param uriFile the uri file
* @return the bitmap
*/public Bitmap getImageBitmap(String uriFile) {
Bitmap bm = null;
File isFile = newFile(directory, uriFile);
if (isFile.exists()) {
bm = BitmapFactory.decodeFile(isFile.getAbsolutePath());
} elseif ( ConfigNet.getInstance().isConnected() ){
try {
URL aURL = new URL(ConfigNet.getInstance().getFilesURL(uriFile));
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
bm = BitmapFactory.decodeStream(bis);
// Save the image in the sd
FileOutputStream out = new FileOutputStream(newFile(directory, uriFile ));
byte[] buffer = newbyte[1024];
int len;
while ((len = is.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
is.close();
bm.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
bis.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return bm;
}
}