且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

绘制转换为字符串传递给另一个活动

更新时间:2022-12-19 10:28:02


  

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


块引用>
 位图照片=(位图)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...