I would like to know what is best way to store images from internet in folder and read from folder because later on I will need to show them in gallery. Should I store them to external or internal storage, should I give user option to choose storage if his memory is low or? Also how should I optimize it so it doesn't take too much space. Generally I need some idea how to make it fast, stable and optimized for devices with no memory card.
我想知道什么是从文件夹中存储图像和从文件夹中读取图像的最佳方法,因为稍后我将需要在库中显示它们。我应该将它们存储在外部存储器还是内部存储器中,如果内存不足,我应该给用户选择存储的选项吗?另外我应该如何优化它,以免占用太多空间。通常我需要知道如何使它快速,稳定和优化没有存储卡的设备。
1 个解决方案
#1
0
in your Oncreate() method
在你的Oncreate()方法中
getBitmapFromURL(data);
where data is the image url.
其中data是图片网址。
then
public Bitmap getBitmapFromURL(String src) {
try {
URL url = new URL(src);
URLConnection connection = (URLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
bitmap = BitmapFactory.decodeStream(input);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
String filename;
Date date = new Date(0);
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
filename = sdf.format(n);
File myDir=new File("/sdcard/Pictures");
myDir.mkdirs();
String fname = "Image-"+ n +".jpg";
// filename = sdf.format(date);
File file = new File(myDir, fname);
if (file.exists ()) file.delete ();
try {
file.createNewFile();
FileOutputStream fo = new FileOutputStream(file);
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
Toast.makeText(getApplicationContext(),"Saved to Light Box",Toast.LENGTH_LONG).show();
// 5
fo.write(bytes.toByteArray());
fo.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return bitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
#1
0
in your Oncreate() method
在你的Oncreate()方法中
getBitmapFromURL(data);
where data is the image url.
其中data是图片网址。
then
public Bitmap getBitmapFromURL(String src) {
try {
URL url = new URL(src);
URLConnection connection = (URLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
bitmap = BitmapFactory.decodeStream(input);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
String filename;
Date date = new Date(0);
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
filename = sdf.format(n);
File myDir=new File("/sdcard/Pictures");
myDir.mkdirs();
String fname = "Image-"+ n +".jpg";
// filename = sdf.format(date);
File file = new File(myDir, fname);
if (file.exists ()) file.delete ();
try {
file.createNewFile();
FileOutputStream fo = new FileOutputStream(file);
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
Toast.makeText(getApplicationContext(),"Saved to Light Box",Toast.LENGTH_LONG).show();
// 5
fo.write(bytes.toByteArray());
fo.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return bitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}