android 把图片存到图库中,android – 保存从图库中选取的图像以备将...

编程入门 行业动态 更新时间:2024-10-26 18:25:31

android 把图片存到<a href=https://www.elefans.com/category/jswz/34/1769407.html style=图库中,android – 保存从图库中选取的图像以备将..."/>

android 把图片存到图库中,android – 保存从图库中选取的图像以备将...

用户唯一挑选的是图片的路径.因此,如果您将路径保存到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中使用:

@Override

protected 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)mit();

cursor.close();

ImageView imageView = (ImageView) findViewById(R.id.imgView);

imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

}

更多推荐

android 把图片存到图库中,android – 保存从图库中选取的图像以备将...

本文发布于:2024-02-27 14:38:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1706932.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图库   以备   图像   图片   android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!