ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2019-08-31 15:24:41  阅读:181  来源: 互联网

标签:android-gallery android gallery


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

@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);
        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中使用:

@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).commit();
        cursor.close();

        ImageView imageView = (ImageView) findViewById(R.id.imgView);
        imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

    }

标签:android-gallery,android,gallery
来源: https://codeday.me/bug/20190831/1776558.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有