android– 保存从图库中选取的图像以备将来使用

android– 保存从图库中选取的图像以备将来使用,第1张

概述嘿,我一直在寻找一段时间.以下代码从android库中选择图像并在imageView中显示.但事实上,只要应用程序关闭并重新启动,就必须再次选择.我想知道如何编辑以下内容以在imageView中保存图像.@OverrideprotectedvoidonActivityResult(intrequestCode,intresultCode,Intentdata

嘿,我一直在寻找一段时间.以下代码从android库中选择图像并在imageVIEw中显示.但事实上,只要应用程序关闭并重新启动,就必须再次选择.我想知道如何编辑以下内容以在imageVIEw中保存图像.

@OverrIDeprotected voID onActivityResult(int requestCode, int resultCode, Intent data) {    super.onActivityResult(requestCode, resultCode, data);    if (requestCode == RESulT_LOAD_IMAGE && resultCode == RESulT_OK && null != data) {        Uri selectedImage = data.getData();        String[] filePathColumn = { MediaStore.Images.Media.DATA };        Cursor cursor = getContentResolver().query(selectedImage,                filePathColumn, null, null, null);        cursor.movetoFirst();        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);        String picturePath = cursor.getString(columnIndex);        cursor.close();        ImageVIEw imageVIEw = (ImageVIEw) findVIEwByID(R.ID.imgVIEw);        imageVIEw.setimageBitmap(BitmapFactory.decodefile(picturePath));    }}

解决方法:

用户唯一挑选的是图片的路径.因此,如果您将路径保存到SharedPreferences,那么每次启动应用程序时,您都可以使用现有代码,但只需更改获取路径的位置:

String picturePath = PreferenceManager.getDefaultSharedPreferences(this).getString("picturePath", "");if(!picturePath.equals("")){   ImageVIEw imageVIEw = (ImageVIEw) findVIEwByID(R.ID.imgVIEw);   imageVIEw.setimageBitmap(BitmapFactory.decodefile(picturePath));}

编辑:
这是一个可以在OnCreate中使用的完整方法:

String picturePath = PreferenceManager.getDefaultSharedPreferences(this).getString("picturePath", "");if(!picturePath.equals("")){   ImageVIEw imageVIEw = (ImageVIEw) findVIEwByID(R.ID.imgVIEw);   imageVIEw.setimageBitmap(BitmapFactory.decodefile(picturePath));}else {   selectimage();}

在select image中使用当前代码开始挑选活动,然后在onActivityResult中使用:

@OverrIDeprotected voID onActivityResult(int requestCode, int resultCode, Intent data) {    super.onActivityResult(requestCode, resultCode, data);    if (requestCode == RESulT_LOAD_IMAGE && resultCode == RESulT_OK && null != data) {        Uri selectedImage = data.getData();        String[] filePathColumn = { MediaStore.Images.Media.DATA };        Cursor cursor = getContentResolver().query(selectedImage,                filePathColumn, null, null, null);        cursor.movetoFirst();        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);        String picturePath = cursor.getString(columnIndex);        PreferenceManager.getDefaultSharedPreferences(this).edit().putString("picturePath", picturePath).commit();        cursor.close();        ImageVIEw imageVIEw = (ImageVIEw) findVIEwByID(R.ID.imgVIEw);        imageVIEw.setimageBitmap(BitmapFactory.decodefile(picturePath));    }
总结

以上是内存溢出为你收集整理的android – 保存从图库中选取的图像以备将来使用全部内容,希望文章能够帮你解决android – 保存从图库中选取的图像以备将来使用所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1106904.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存