如果您对在Android中从内存中保存和读取位图/图像感兴趣,那么本文将是一篇不错的选择,我们将为您详在本文中,您将会了解到关于在Android中从内存中保存和读取位图/图像的详细内容,我们还将为您解
如果您对在 Android 中从内存中保存和读取位图/图像感兴趣,那么本文将是一篇不错的选择,我们将为您详在本文中,您将会了解到关于在 Android 中从内存中保存和读取位图/图像的详细内容,我们还将为您解答android 读取内部存储文件的相关问题,并且为您提供关于Android WebView从内部存储加载| Android 11 | Android R、android – Glide缓存内存中的屏幕图像、android – LogCat条目在内存中保存多长时间?、android – 为什么Picasso没有在缓存中保存图像以供离线使用?的有价值信息。
本文目录一览:- 在 Android 中从内存中保存和读取位图/图像(android 读取内部存储文件)
- Android WebView从内部存储加载| Android 11 | Android R
- android – Glide缓存内存中的屏幕图像
- android – LogCat条目在内存中保存多长时间?
- android – 为什么Picasso没有在缓存中保存图像以供离线使用?
在 Android 中从内存中保存和读取位图/图像(android 读取内部存储文件)
我想要做的是将图像保存到手机的内部存储器 (不是 SD 卡) 。
我该怎么做?
我已将图像直接从相机获取到我的应用程序中的图像视图,它一切正常。
现在我想要将此图像从图像视图保存到我的 android 设备的内部存储器,并在需要时访问它。
谁能指导我如何做到这一点?
我对android有点新,所以请,如果我能有一个详细的程序,我将不胜感激。
答案1
小编典典使用以下代码将图像保存到内部目录。
private String saveToInternalStorage(Bitmap bitmapImage){ ContextWrapper cw = new ContextWrapper(getApplicationContext()); // path to /data/data/yourapp/app_data/imageDir File directory = cw.getDir("imageDir", Context.MODE_PRIVATE); // Create imageDir File mypath=new File(directory,"profile.jpg"); FileOutputStream fos = null; try { fos = new FileOutputStream(mypath); // Use the compress method on the BitMap object to write image to the OutputStream bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos); } catch (Exception e) { e.printStackTrace(); } finally { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } return directory.getAbsolutePath(); }
解释 :
1.目录将以给定的名称创建。Javadocs 用于告诉它将在哪里创建目录。
2.您必须提供要保存的图像名称。
从内部存储器中读取文件。使用下面的代码
private void loadImageFromStorage(String path){ try { File f=new File(path, "profile.jpg"); Bitmap b = BitmapFactory.decodeStream(new FileInputStream(f)); ImageView img=(ImageView)findViewById(R.id.imgPicker); img.setImageBitmap(b); } catch (FileNotFoundException e) { e.printStackTrace(); }}
Android WebView从内部存储加载| Android 11 | Android R
找到答案,
在定位setAllowFileAccess时,必须从Build.VERSION_CODES.R显式地将其设置为true
,否则将不允许您加载file:// URLs
。
所以解决方案是
webView.settings.allowFileAccess = true
在Java中,
webView.getSettings().setAllowFileAccess(true);
这在两种情况下都可以正常工作。
android – Glide缓存内存中的屏幕图像
我试过了:
Glide.with().load(uri).into(new SimpleTarget<GlideDrawable>(WIDTH,HEIGHT) { @Override public void onResourceReady(GlideDrawable resource,GlideAnimation<? super GlideDrawable> glideAnimation) { //left empty } } );
但是当我稍后调用在ImageView中加载uri时,它仍然会从路径加载它而不是内存.
是否有另一种方法可以将图像缓存在内存中而无需在UI上加载它们?
解决方法
以下是页面中的一些示例代码,显示如何将文件下载到缓存,而不在UI中显示图像:
FutureTarget<File> future = Glide.with(applicationContext) .load(yourUrl) .downloadOnly(500,500); // Can be omitted if you only want to have the data cached (same goes for the "future" instance variable File cacheFile = future.get();
但是,请确保已配置Glide以启用缓存:https://github.com/bumptech/glide/wiki/Configuration
android – LogCat条目在内存中保存多长时间?
解决方法
您可以使用adb logcat -g(通常返回介于64Kb和512Kb之间的值)来了解大小限制.
android – 为什么Picasso没有在缓存中保存图像以供离线使用?
Picasso.with(getContext()) .load(userInfo.getUserPictureUri()) .networkPolicy(NetworkPolicy.OFFLINE) .resize(80,80) .error(R.drawable.profile_picture) .centerCrop() .into(imageView_ProfilePictureSide,new Callback() { @Override public void onSuccess() { } @Override public void onError() { // Try again if cache Failed Picasso.with(getActivity()) .load(userInfo.getUserPictureUri()) .error(R.drawable.profile_picture) .into(imageView_ProfilePictureSide); } });
解决方法
compile ''com.squareup.picasso:picasso:2.5.2'' compile ''com.squareup.okhttp:okhttp:2.4.0''
Picasso使用HTTP客户端请求进行磁盘缓存操作因此您可以使自己的http请求标头具有属性Cache-Control with max-age并使用Okhttp创建自己的静态毕加索实例而不是默认的Picasso.
Okhttp和毕加索图书馆都由平组团队提供.
参考文献:How do I use disk caching in Picasso?和Github issue about disk cache,@ jake-wharton回答了两个问题 – > Question1和Question2
今天关于在 Android 中从内存中保存和读取位图/图像和android 读取内部存储文件的介绍到此结束,谢谢您的阅读,有关Android WebView从内部存储加载| Android 11 | Android R、android – Glide缓存内存中的屏幕图像、android – LogCat条目在内存中保存多长时间?、android – 为什么Picasso没有在缓存中保存图像以供离线使用?等更多相关知识的信息可以在本站进行查询。
本文标签: