绘制转换为字符串传递给另一个活动 [英] Converting drawable to string to pass to another activity

查看:182
本文介绍了绘制转换为字符串传递给另一个活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我正在试图将绘制转换为字符串并将它传递给通过使用这种方法共享preferences另一个活动。 loadIcon 可绘制

活动1

  {尝试
    AI = pm.getApplicationInfo(pInfo.packageName,PackageManager.GET_META_DATA);
}
赶上(ē的NameNotFoundException){
    // TODO自动生成catch块
    e.printStackTrace();
}    loadIcon = ai.loadIcon(下午);    Log.i(图标绘,loadIcon.toString());
                共享preferences preferences = getShared preferences(ANALYZE_ preF,MODE_PRIVATE);
                共享preferences.Editor编辑器= preferences.edit();
                editor.putString(可拉伸,loadIcon.toString());
                editor.commit();

这是我在LogCat中得到:

  10月11日至6日:13:28.995:I /共享可绘制(16786):android.graphics.drawable.BitmapDrawable@417f2aa8

试图在其他活动来检索

活动2

 共享preferences设置= getShared preferences(ANALYZE_ preF,MODE_PRIVATE);
字符串drawableString = settings.getString(绘制,);
Log.i(绘制对象共享,drawableString);
可绘制drawableImage = getDrawable(drawableString);
imageView.setBackgroundDrawable(drawableImage);       公众可绘制getDrawable(字符串bitmapUrl){
              尝试{
                网址URL =新的URL(bitmapUrl);
                可绘制D =新BitmapDrawable(BitmapFactory.de codeStream(url.openConnection()的getInputStream()));
                Log.i(D值,D值+ D);
                返回D组;
              }
              赶上(例外前){
                  Log.i(空,绘制对象返回null);
                  返回null;
              }
       }

试图将其转换回绘制,当我得到一个空

logcat的:

  10月11日至6日:13:28.995:I /空(16786):可绘制返回null


解决方案

  

我也面临着同样的问题,我不喜欢这种方式。


 位图照片=(位图)data.getExtras()获得(数据)。
ByteArrayOutputStream宝=新ByteArrayOutputStream();
photo.com preSS(Bitmap.Com pressFormat.JPEG,90,包);
字节[] BA = bao.toByteArray();
字符串BA1 = Base64.en codeBytes(BA);


  

然后发送此字符串到其他活动。
    然后去code本的Base64 code ...


I'm currently trying to convert a drawable to a string and pass it to another activity via shared preferences using this method. loadIcon is a 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();

This is what i get in Logcat:

11-06 10:13:28.995: I/Drawable shared(16786): android.graphics.drawable.BitmapDrawable@417f2aa8

Trying to retrieve it in the other 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;
              }
       }

I get a null when trying to convert it back to a drawable

Logcat:

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...

这篇关于绘制转换为字符串传递给另一个活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆