【问题标题】:How to store and retrieve bitmap in sharedPreferences in Android?如何在 Android 的 sharedPreferences 中存储和检索位图?
【发布时间】:2011-09-06 05:58:03
【问题描述】:

我是 Android 新手。我想将我的位图存储在 sharedPreferences 中。谁能告诉我这怎么可能?实际上我的要求是,我从图库中获取图像以及从相机拍照,然后在我的 ImageView 中设置该位图。这些都正常工作。但是当我单击后退按钮时,所有 ImageView 都将为空。

所以我想在整个应用程序中存储这些位图。

谁能帮帮我?我非常坚持这一点。

谢谢。

【问题讨论】:

标签: android imageview


【解决方案1】:

嘿朋友们,我在这里找到了我的问题的解决方案我发布了我的代码,以便其他人可以使用这个解决方案..

1)。点击按钮 - 打开相机以捕捉图像

ContentValues values = new ContentValues();  
values.put(MediaStore.Images.Media.TITLE, fileName);  
mCapturedImageURI = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);  

Intent cameraIntent = new Intent("android.media.action.IMAGE_CAPTURE");
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);  
startActivityForResult(cameraIntent, CAMERA_REQUEST);

2)。单击按钮 - 打开图库以选择图像

Intent galleryintent = new Intent(Intent.ACTION_GET_CONTENT);
galleryintent.setType("image/*");
startActivityForResult(galleryintent, IMAGE_PICK);

3)。静态变量

private static final int CAMERA_REQUEST = 0; 
    private static final int IMAGE_PICK = 1;

4)。活动结果

    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
        {
            switch(requestCode) 
            { 
                case CAMERA_REQUEST:
                    if(resultCode == RESULT_OK)
                    {
                        String[] projection = { MediaStore.Images.Media.DATA}; 
                        Cursor cursor = managedQuery(mCapturedImageURI, projection, null, null, null); 
                        int column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
                        cursor.moveToFirst(); 
                        String capturedImageFilePath = cursor.getString(column_index_data);
                        Log.d("photos*******"," in camera take int  "+capturedImageFilePath);

                        Bitmap photo_camera = BitmapFactory.decodeFile(capturedImageFilePath, options);

                        if(data != null)
                        {
    img_1.setImageBitmap(photo_camera);
                                prefsEditor.putString(Global.PHOTO_1,capturedImageFilePath);
    }
    }
case IMAGE_PICK:
                if(resultCode == RESULT_OK)
                {  
                    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 filePath = cursor.getString(columnIndex);
                    cursor.close();

//                  Bitmap photo = BitmapFactory.decodeFile(filePath);
                   Bitmap photo_gallery = BitmapFactory.decodeFile(filePath,options);
                   img_1.setImageBitmap(photo_gallery);
                        prefsEditor.putString(Global.PHOTO_1, filePath);
}

}
        prefsEditor.commit();
}

5)。在 onDestroy() 您必须销毁您设置的所有位图。

@Override
    public void onDestroy()
    {
        super.onDestroy();
        if(photo_camera != null)
        {
            photo_camera.recycle();
        }
        if(photo_gallery != null)
        {
            photo_gallery.recycle();
        }
}

6)。当您从 sharedPrefrences 获取数据时,您必须将字符串转换为位图,然后您可以在 ImageView 中设置位图。 例如,位图 bit1 = BitmapFactory.decodeFile(strimg1); 然后设置,imageView.setImageBitmap

【讨论】:

  • 感谢您发布问题的解决方案。将您的答案设置为已选择以将其从未回答列表中删除。
【解决方案2】:

不要将位图存储在共享首选项中。如果您需要在应用程序的生命周期内保留它,可以将其分配给 static 字段。如果您想在设备重新启动后保留它,请将其放入文件或数据库中。

欲了解更多信息,请阅读http://developer.android.com/resources/faq/framework.html#3

【讨论】:

    【解决方案3】:

    您可以像这样在SharedPreference 中添加值:

    SharedPreferences pref = getSharedPreferences("abc", 0);
    Editor edit = pref.edit();
    edit.putBoolean(arg0, arg1);
    edit.putFloat(arg0, arg1);
    edit.putInt(arg0, arg1);
    edit.putLong(arg0, arg1);
    edit.putString(arg0, arg1);
    edit.commit();
    

    您只能在SharedPreference 中添加Boolean, Float, Int, Long, String 值。

    要存储图像,您应该使用设备的外部或内部存储器。

    【讨论】:

    • 谢谢,我按照你说的使用了。但现在我想通过显示我的图像将字符串转换为位图。你有什么将String转换为Bitmap的想法吗?
    【解决方案4】:

    假设位图图像为bitmapImg,您可以将位图图像转换为base64String后存储,如下所示

    SharedPreferences mSharedPreferences = context.getSharedPreferences("MyPreferences", Context.MODE_PRIVATE);
    
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmapImg.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    
    byte[] compressImage = baos.toByteArray();
    String sEncodedImage = Base64.encodeToString(compressImage, Base64.DEFAULT);
    
    mSharedPreferences.edit().putString("keyStoredImage",sEncodedImage);
    mSharedPreferences.edit().commit();
    

    并从 SharedPreference 中检索存储的图像,如下所示

    if(mSharedPreferences.contains("keyStoredImage"))
    {
    
    String encodedImage = mSharedPreferences.getString("keyStoredImage",null);
    
    byte[] b = Base64.decode(encodedImage, Base64.DEFAULT);
    
    Bitmap bitmapImage = BitmapFactory.decodeByteArray(b, 0, b.length);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-10
      • 1970-01-01
      • 2015-04-02
      • 1970-01-01
      • 2012-09-02
      • 2015-06-16
      • 2012-08-01
      • 2014-04-20
      相关资源
      最近更新 更多