gpt4 book ai didi

android - 将可绘制对象转换为字符串以传递给另一个 Activity

转载 作者:行者123 更新时间:2023-11-29 21:56:03 25 4
gpt4 key购买 nike

我目前正在尝试将可绘制对象转换为字符串,并使用此方法通过共享首选项将其传递给另一个 Activity 。 loadIcon 是一个Drawable

Activity 1

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

尝试在其他 Activity 中检索它

Activity 2

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

最佳答案

i too face the same problem i do like this way..

Bitmap photo = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bao = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 90, bao);
byte [] ba = bao.toByteArray();
String ba1=Base64.encodeBytes(ba);

Then send this string to other activity. then decode this Base64 code...

关于android - 将可绘制对象转换为字符串以传递给另一个 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13243445/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com