存储位图截图在Android的高速缓存 [英] Store Bitmap Screenshot in Android Cache
本文介绍了存储位图截图在Android的高速缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有人可以帮我做下面的商城我的code缓存,而不是我的SD卡。我以我的应用程序的屏幕截图,以用于在社交媒体上分享。
//图像的命名和路径,包括SD卡
字符串的mpath = Environment.getExternalStorageDirectory()的toString()+/+ ACCUWX.IMAGE_APPEND。
//创建位图截屏
点阵位图;
查看V1 = mCurrentUrlMask.getRootView();
v1.setDrawingCacheEnabled(真正的);
位图= Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(假);
OutputStream的FOUT = NULL;
镜像文件=新的文件(的mpath);
尝试 {
FOUT =新的FileOutputStream(镜像文件);
bitmap.com preSS(Bitmap.Com pressFormat.JPEG,90,FOUT);
fout.flush();
fout.close();
}赶上(FileNotFoundException异常E){
// TODO自动生成的catch块
e.printStackTrace();
}赶上(IOException异常E){
// TODO自动生成的catch块
e.printStackTrace();
}
这里是我的社会媒体code分享的截图:
意图socialIntent =新的意图(Intent.ACTION_SEND);
socialIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
开放的我们的uri = Uri.fromFile(新文件(的mpath));
socialIntent.putExtra(Intent.EXTRA_STREAM,URI);
socialIntent.setType(为image / jpeg);
startActivity(Intent.createChooser(socialIntent,你怎么想的份额?));
解决方案
虽然不存储位图缓存,因为会出现内存泄漏问题是个好主意。但是,你可以做以下的事情存储位图缓存。
静态HashMap中<字符串,位图>缓存=新的HashMap<字符串,位图>();
存储位图到缓存
cache.put(文件名,BMP);
后,下面的code检索缓存的使用位图
如果(cache.containsKey(文件名)){
imageView.setImageBitmap(cache.get(文件名));
}
Can someone please help me make my code below store to cache rather than my sd card. I am taking a screenshot of my application to use for sharing on social media.
// image naming and path to include sd card
String mPath = Environment.getExternalStorageDirectory().toString() + "/" + ACCUWX.IMAGE_APPEND;
// create bitmap screen capture
Bitmap bitmap;
View v1 = mCurrentUrlMask.getRootView();
v1.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
OutputStream fout = null;
imageFile = new File(mPath);
try {
fout = new FileOutputStream(imageFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fout);
fout.flush();
fout.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
And here is my Social Media code to share the screenshot:
Intent socialIntent = new Intent(Intent.ACTION_SEND);
socialIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Uri uri = Uri.fromFile(new File(mPath));
socialIntent.putExtra(Intent.EXTRA_STREAM, uri);
socialIntent.setType("image/jpeg");
startActivity(Intent.createChooser(socialIntent, "How do you wanna share?"));
解决方案
Although it is not a good idea to store bitmap in cache because there will be memory leak issue. But, you can do following thing to store bitmap in cache.
static HashMap<String, Bitmap> cache=new HashMap<String, Bitmap>();
to store bitmap into cache
cache.put(filename, bmp);
later, to retrieve bitmap from cache use following code,
if(cache.containsKey(filename)){
imageView.setImageBitmap(cache.get(filename));
}
这篇关于存储位图截图在Android的高速缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文