我想知道什么是从文件夹中存储图像并从文件夹中读取图像的最佳方法,因为稍后我需要在图库中显示它们。我应该将它们存储在外部存储器还是内部存储器中,如果内存不足,我是否应该让用户选择存储?另外我应该如何优化它,以免占用太多空间。一般来说,我需要知道如何使其快速,稳定并针对没有存储卡的设备进行优化。
答案 0 :(得分:0)
getBitmapFromURL(data);
其中data是图片网址。
然后
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;
}
}