14 Jan 2016
|
Android
URL을 이용해서 HTTP 기반으로 이미지를 다운로드하는 코드입니다.
일단, 인터넷 경로를 통해 다운을 받기 위해서는 AndroidManifest.xml 파일에
다음 permission을 추가해줘야 합니다.
Permission
<uses-permission android:name="android.permission.INTERNET" />
URL을 이용하여 Bitmap 가져오는 함수
private Bitmap getImageFromURL(String imageUrl) {
Bitmap bitmap = null;
try {
URL url = new URL(imageUrl);
URLConnection connection = url.openConnection();
connection.connect();
int size = conn.getContentLength();
BufferedInputStream bis = new BufferedInputStream(connection.getInputStream(), size);
bitmap = BitmapFactory.decodeStream(bis);
bis.close();
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
12 Jan 2016
|
Android
모바일 디바이스내 내부/외부 저장소의 용량을 확인하는 함수입니다.
MemoryUtil.java
public class MemoryUtil {
public static void showStatus() {
Log.i("", "< MemoryStatus >");
Log.i("", "Total Internal MemorySize : " + getFormattedSize(GetTotalInternalMemorySize()));
Log.i("",
"Available Internal MemorySize : " + getFormattedSize(GetAvailableInternalMemorySize()));
if (IsExternalMemoryAvailable() == true) {
Log.i("", "Total External MemorySize : " + getFormattedSize(GetTotalExternalMemorySize()));
Log.i("",
"Available External MemorySize : " + getFormattedSize(GetAvailableExternalMemorySize()));
}
}
private static boolean IsExternalMemoryAvailable() {
return android.os.Environment.getExternalStorageState()
.equals(android.os.Environment.MEDIA_MOUNTED);
}
private static long GetTotalInternalMemorySize() {
File path = Environment.getDataDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long totalBlocks = stat.getBlockCount();
return totalBlocks * blockSize;
}
private static long GetAvailableInternalMemorySize() {
File path = Environment.getDataDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
return availableBlocks * blockSize;
}
private static long GetTotalExternalMemorySize() {
if (IsExternalMemoryAvailable() == true) {
File path = Environment.getExternalStorageDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long totalBlocks = stat.getBlockCount();
return totalBlocks * blockSize;
}
return -1;
}
private static long GetAvailableExternalMemorySize() {
if (IsExternalMemoryAvailable() == true) {
File path = Environment.getExternalStorageDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
return availableBlocks * blockSize;
}
return -1;
}
private static String getFormattedSize(long size) {
String suffix = "";
if (size >= 1024) {
suffix = "KB";
size /= 1024;
if (size >= 1024) {
suffix = "MB";
size /= 1024;
}
}
StringBuilder sb = new StringBuilder(Long.toString(size));
int commaOffset = sb.length() - 3;
while (commaOffset > 0) {
sb.insert(commaOffset, ',');
commaOffset -= 3;
}
sb.append(suffix);
return sb.toString();
}
}