我目前正在尝试将可绘制对象转换为字符串,并使用此方法通过共享首选项将其传递给另一个活动。loadIcon是一个Drawable。
活动一
try {
ai = pm.getApplicationInfo(pInfo.packageName, PackageManager.GET_META_DATA);
}
catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
loadIcon = ai.loadIcon(pm);
Log.i("Icon drawable", loadIcon.toString());
SharedPreferences preferences = getSharedPreferences("ANALYZE_PREF", MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("drawable", loadIcon.toString());
editor.commit();
这就是我在 Logcat 中得到的:
11-06 10:13:28.995: I/Drawable shared(16786): android.graphics.drawable.BitmapDrawable@417f2aa8
试图在其他活动中检索它
活动二
SharedPreferences settings = getSharedPreferences("ANALYZE_PREF", MODE_PRIVATE);
String drawableString = settings.getString("drawable", "");
Log.i("Drawable shared", drawableString);
Drawable drawableImage = getDrawable(drawableString);
imageView.setBackgroundDrawable(drawableImage);
public Drawable getDrawable(String bitmapUrl) {
try {
URL url = new URL(bitmapUrl);
Drawable d =new BitmapDrawable(BitmapFactory.decodeStream(url.openConnection().getInputStream()));
Log.i("d value", "d value " + d);
return d;
}
catch(Exception ex) {
Log.i("Null", "Drawable returns null");
return null;
}
}
尝试将其转换回可绘制对象时得到一个空值
日志猫:
11-06 10:13:28.995: I/Null(16786): Drawable returns null