2013-07-12 20 views
0

我將圖像轉換爲字符串並將該字符串存儲在sharedpreferences中。然後稍後在其他活動中,我想獲取該字符串轉換回位圖並將其顯示在圖像視圖中。如果沒有從sharedpreference中獲取任何內容,也爲precausion我想將ic_launcher設置爲我的ImageView中的默認圖像。從sharedpreference獲取字符串,轉換爲位圖,在imageview中顯示

這是我如何努力完成上述任務。

String pic = shared.getString("UserPic",""); 
    Log.i("picstring-verifydetail" , "picstring : "+pic); 
    if (pic != null && pic != "") { 
     try { 
      userpic = ImageHelper.stringToImage(pic); 
      profilepic.setImageBitmap(userpic); 
     } catch (IOException e) { 
      Log.e("picsetting", e.toString()); 
      e.printStackTrace(); 
     } 
    } 
    else 
    { 
     Bitmap defaultImage = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher); 
     profilepic.setImageBitmap(defaultImage); 
    } 

我也喜歡存儲一些名稱和值是成功抓取,但字符串圖像不從sharedpreferences越來越牽強。它總是會去其他部分,然後再次出現錯誤:profilepic.setImageBitmap(defaultImage);上的「未找到源」。我搜索了logcat,但沒有發現錯誤。

請幫助完成這2個任務。

感謝&問候,
Sourabh古普塔

+0

你是如何保存它的喜好? –

回答

1

我不認爲你想要做什麼是一個好主意。

嘗試將圖像保存在SD卡或內部存儲器中,並將文件路徑存儲在SharedPreferences中。

+0

感謝您的幫助!我使用錯誤的密鑰從共享首選項中獲取我的價值。 – user2376920

1

如果您有存儲在assets OR res文件夾這些圖像。您只需將圖像名稱存儲到SharedPreferences中,稍後您可以從中獲取圖像名稱,並通過從路徑中提取圖像名稱並將其顯示在屏幕上。

+0

感謝您的關注。這些圖像是由用戶從他們的畫廊選擇的個人資料圖片。 – user2376920

相關問題