Home>

The internal storage method is used to transfer the bitmap object and other java objects that support serialization between the two activities. The key points are as follows:

1. http client download images,Display via imageview object

2. Pass the bitmap object on the imageview from the current activity to another activity and display it

3. Passing java object data based on serialization

First look at how I implemented the http client to download pictures,The http client downloads the image through the asynchronous task interface and updates the imageview through the handler. The code is as follows:

package com.example.sharedemo;
import java.io.ioexception;
import java.io.inputstream;
import org.apache.http.httpentity;
import org.apache.http.httpresponse;
import org.apache.http.httpstatus;
import org.apache.http.client.clientprotocolexception;
import org.apache.http.client.httpclient;
import org.apache.http.client.methods.httpget;
import org.apache.http.impl.client.defaulthttpclient;
import android.graphics.bitmap;
import android.graphics.bitmapfactory;
import android.os.asynctask;
import android.os.handler;
import android.os.message;
import android.util.log;
public class imageloadtask extends asynctask<string, void, bitmap>{
  private handler handler;
  public imageloadtask (handler handler) {
    this.handler=handler;
  }
  protected void onpostexecute (bitmap bitmap) {
    message msg=new message ();
    msg.obj=bitmap;
    handler.sendmessage (msg);
  }
  @override
  protected bitmap doinbackground (string ... urls) {
    bitmap bitmap=null;
    // create http client
    httpclient httpclient=new defaulthttpclient ();
    try {
      // get request
      log.i ("image-url", urls [0]);
      httpget httprequest=new httpget (urls [0]);
      httpresponse httpresponse=httpclient.execute (httprequest);
      if (httpresponse.getstatusline (). getstatuscode () == httpstatus.sc_ok) {
        // get entity from response
        httpentity httpentity=httpresponse.getentity ();
        // read stream
        inputstream is=httpentity.getcontent ();
        bitmap=bitmapfactory.decodestream (is);
        is.close ();
        log.i ("image", "already get the image by url:" + urls [0]);
      }
    } catch (clientprotocolexception e) {
      e.printstacktrace ();
    } catch (ioexception e) {
      e.printstacktrace ();
    } finally {
      httpclient.getconnectionmanager (). shutdown ();
    }
    return bitmap;
  }
}

In the current activity, the event response on the button is used to switch the view to the corresponding activity, and at the same time, Java serialized data transfer is implemented.The mainactivity code is as follows:

package com.example.sharedemo;
import java.io.bytearrayoutputstream;
import java.io.fileoutputstream;
import android.app.activity;
import android.content.context;
import android.content.intent;
import android.graphics.bitmap;
import android.graphics.matrix;
import android.graphics.drawable.bitmapdrawable;
import android.os.bundle;
import android.os.handler;
import android.os.message;
import android.util.log;
import android.view.view;
import android.view.view.onclicklistener;
import android.widget.button;
import android.widget.imageview;
public class mainactivity extends activity implements onclicklistener {
  public final static string share_bitmap_command="share-image";
  public final static string share_text_data_command="share-text-data";
  private handler handler;
  @override
  protected void oncreate (bundle savedinstancestate) {
    super.oncreate (savedinstancestate);
    setcontentview (r.layout.activity_main);
    setuponclicklistener ();
  }
  private void setuponclicklistener () {
    button bitmapbtn=(button) this.findviewbyid (r.id.bitmapsharebtn);
    bitmapbtn.settag (share_bitmap_command);
    bitmapbtn.setonclicklistener (this);
    button textdatabtn=(button) this.findviewbyid (r.id.mapsharebtn);
    textdatabtn.settag (share_text_data_command);
    textdatabtn.setonclicklistener (this);
    final imageview imageview=(imageview) this.findviewbyid (r.id.imageview1);
    handler=new handler ()
    {
      public void handlemessage (message msg) {
        bitmap bitmap=(bitmap) msg.obj;
        if (bitmap!=null)
        {
          / *
          // To prevent the original image from being too large and causing memory overflow,First zoom out of the original image here,Then release the memory occupied by the original bitmap
          bitmap smallbitmap=zoombitmap (bitmap, bitmap.getwidth ()/5, bitmap.getheight ()/5);
          // release resources
          bitmap.recycle ();
          // display image
          imageview.setimagebitmap (smallbitmap);
          imageview.invalidate ();
          * /
          imageview.setimagebitmap (bitmap);
        }
      }
    };
    imageloadtask task=new imageloadtask (handler);
    task.execute ("http://img.blog.csdn.net/20150607143208238");
  }
  public static bitmap zoombitmap (bitmap bitmap, int width, int height) {
    int w=bitmap.getwidth ();
    int h=bitmap.getheight ();
    matrix matrix=new matrix ();
    float scalewidth=((float) width/w);
    float scaleheight=((float) height/h);
    matrix.postscale (scalewidth, scaleheight);// Does not change the original image size
    bitmap newbmp=bitmap.createbitmap (bitmap, 0, 0, w, h, matrix, true);
    return newbmp;
  }
  @override
  public void onclick (view v) {
    object tag=v.gettag ();
    log.i ("command", tag.tostring ());
    if (share_bitmap_command.equals (tag))
    {
      intent intent=new intent (this.getapplicationcontext (), imageprocessactivity.class);
      imageview imageview=(imageview) this.findviewbyid (r.id.imageview1);
      bitmap bitmap=((bitmapdrawable) imageview.getdrawable ()). getbitmap ();
      intent.putextra ("selectedimage", bitmap);
      intent.putextra ("name", "lena");
      intent.putextra ("description", "Supergirl");
      this.startactivity (intent);
    }
    else if (share_text_data_command.equals (tag))
    {
      intent intent=new intent (this.getapplicationcontext (), imageprocessactivity.class);
      imageview imageview=(imageview) this.findviewbyid (r.id.imageview1);
      bitmap bitmap=((bitmapdrawable) imageview.getdrawable ()). getbitmap ();
      // save it first then pass uri
      imageinfobean dto=new imageinfobean ();
      string uri=createimagefrombitmap (bitmap);
      dto.setdescription ("Supergirl");
      dto.setname ("lena");
      dto.seturi (uri);
      intent.putextra ("tiger", dto);
      this.startactivity (intent);
    }
  }
  public string createimagefrombitmap (bitmap bitmap) {
    string filename="myimage";
    try {
      bytearrayoutputstream bytes=new bytearrayoutputstream ();
      bitmap.compress (bitmap.compressformat.jpeg, 100, bytes);
      fileoutputstream fo=openfileoutput (filename, context.mode_private);
      fo.write (bytes.tobytearray ());
      fo.close ();
    } catch (exception e) {
      e.printstacktrace ();
      filename=null;
    }
    log.i ("filename", filename);
    return filename;
  }
}

The code corresponding to reading and assembling the bitmap object display in another activity is as follows:

package com.example.sharedemo;
import java.io.filenotfoundexception;
import android.app.activity;
import android.graphics.bitmap;
import android.graphics.bitmapfactory;
import android.os.bundle;
import android.widget.imageview;
import android.widget.textview;
public class imageprocessactivity extends activity {
  @override
  protected void oncreate (bundle savedinstancestate) {
    super.oncreate (savedinstancestate);
    setcontentview (r.layout.share_info);
    backfilldata ();
  }
  private void backfilldata () {
    object obj=this.getintent (). getextras (). get ("tiger");
    imageview imageview=(imageview) this.findviewbyid (r.id.imageview1);
    textview text1=(textview) this.findviewbyid (r.id.textview1);
    textview text2=(textview) this.findviewbyid (r.id.textview2);
    try {
      if (obj!=null&&obj instanceof imageinfobean)
      {
        imageinfobean dto=(imageinfobean) obj;
        bitmap bitmap=bitmapfactory.decodestream (this.openfileinput (dto.geturi ()));
        imageview.setimagebitmap (bitmap);
        imageview.invalidate ();// refresh
        text1.settext ("name:" + dto.getname ());
        text2.settext ("Description:" + dto.getdescription ());
        return;
      }
    } catch (filenotfoundexception e) {
      e.printstacktrace ();
    }
    bitmap bitmap=(bitmap) this.getintent (). getparcelableextra ("selectedimage");
    string name=this.getintent (). getextras (). getstring ("name");
    string description=this.getintent (). getextras (). getstring ("description");
    if (bitmap!=null)
    {
      imageview.setimagebitmap (bitmap);
      imageview.invalidate ();// refresh
    }
    if (name!=null)
    {
      text1.settext ("name:" + name);
    }
    if (description!=null)
    {
      text2.settext ("Description:" + description);
    }
  }
}

The corresponding java serialization object class code is as follows:

package com.example.sharedemo;
import java.io.serializable;
public class imageinfobean implements serializable {
  / **
   *
   * /
  private static final long serialversionuid=1l;
  public string getname () {
    return name;
  }
  public void setname (string name) {
    this.name=name;
  }
  public string getdescription () {
    return description;
  }
  public void setdescription (string description) {
    this.description=description;
  }
  public string geturi () {
    return uri;
  }
  public void seturi (string uri) {
    this.uri=uri;
  }
  private string name;
  private string description;
  private string uri;
}

Final statement:

Don't forget to add network access permissions to the manifest file

<uses-permission android:name="android.permission.internet" />

The first button [Pass Picture] will demonstrate that I encountered an error,The second button [pass text data]

Will demonstrate the correct processing results,Shows as follows:

  • Previous Easily implement the jquery accordion effect
  • Next Explain the role and usage of the Address-of operator&in C ++ programming