Android Download picture/Call system camera to take pictures, display and save to local

Source: Internet
Author: User

Package Com.example.testhttpget;import Java.io.bufferedreader;import Java.io.filenotfoundexception;import Java.io.fileoutputstream;import Java.io.inputstream;import Java.io.inputstreamreader;import Org.apache.http.httpentity;import Org.apache.http.httpresponse;import Org.apache.http.client.httpclient;import Org.apache.http.client.methods.httpget;import Org.apache.http.impl.client.defaulthttpclient;import Android.app.activity;import Android.content.intent;import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.os.bundle;import Android.os.environment;import Android.os.Handler; Import Android.os.message;import android.provider.mediastore;import Android.view.menu;import Android.view.MenuItem ; Import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.imageview;import Android.widget.textview;public class Mainactivity extends Activity {ImageView iamgeview = null; Handler MyHandler = new Handler () {public void Handlemessage (MessaGE msg) {switch (msg.what) {case 1:iamgeview.setimagebitmap (Bit                       MAP) msg.obj);                Break           } super.handlemessage (msg);      }      };        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);                Setcontentview (R.layout.activity_main);        Final TextView hello = (TextView) Findviewbyid (R.id.hello);                Iamgeview = (ImageView) Findviewbyid (R.id.imageview); Child thread network download picture new thread () {public void run () {String Imgurl = "Http://photocdn.sohu.com/20111123/Img3        26603573.jpg ";        HttpGet mhttpget = new HttpGet (Imgurl); HttpGet mhttpget = new HttpGet ("Http://image.baidu.com/search/detail?ct=503316480&z=0&ipn=d&word=%E 7%86%8a%e7%8 "+//" c%ab&step_word=&hs=0&pn=0&spn=0&di=28270682580&pi=&rn=1&tn= Baiduimagedetail&is=0%2c0&istype=0&ie=utf-8&oe=utf-8&in=&cl=2&lm=-1&st=undefined&cs= 404637145%2c3982267385&os=3438506744%2c3869227355&simid=0%2c0&adpicid=0&ln=1975&fr=& fmq=1475036112689_r&fm=&ic=undefined&s=undefined&se=&sme=&tab=0&width=&height= &face=undefined&ist=&jit=&cg=&bdtype=11&oriquery=&objurl=http%3a%2f%         2fpic4.40017.cn%2fscenery%2fdestination%2f2016%2f07%2f17%2f16%2fnyldjq.jpg ");         HttpClient mhtppclient = new Defaulthttpclient ();        try{//Send GET request and get corresponding object HttpResponse httprespose = Mhtppclient.execute (mhttpget);         Get message entity httpentity Entrie = httprespose.getentity ();        Gets the input stream from the message entity InputStream InputStream = Entrie.getcontent (); Convert the input stream to BufferedReader bufferedreader bufferreader = new BufferedReader (New InputStreamReader (InputStream        ));        String result = "";        String line = "";  //      for (;(line = Bufferreader.readline ())!=null;) {//result + = line;//}//Hello.settext (Result);                  Bitmap Bitmap = Bitmapfactory.decodestream (InputStream);                Saveimg2sdcard (Bitmap, "/webtakephone.jpg");         Message msg = new Message ();        Msg.obj = bitmap;        Msg.what = 1;         Myhandler.sendmessage (msg);         }catch (Exception e) {//Hello.settext ("Request Error" +e.tostring ());        };                }}.start (); Call system camera to take pictures and save pictures, show Pictures Findviewbyid (R.id.takephotos). Setonclicklistener (New Onclicklistener () {@Overridepublic void        OnClick (View v) {//Call system camera Intent Intent = new Intent (mediastore.action_image_capture);    Startactivityforresult (intent,1);}}); } @Override protected void Onactivityresult (int requestcode, int resultcode, Intent data) {Super.onactivityresult (   Requestcode, ResultCode, data); if (ResultCode = = ACTIVITY.RESULT_OK) {Bundle bundle = (bundle) Data.getextras();      Bitmap Bitmap = (Bitmap) bundle.get ("Data");   Saveimg2sdcard (Bitmap, "/systemtakephone.jpg"); }} private void Saveimg2sdcard (Bitmap bitmap,string fileName) {FileOutputStream fos;try {if (environment.get Externalstoragestate (). Equals (environment.media_mounted)) {fos = new FileOutputStream ( Environment.getexternalstoragedirectory (). Getabsolutefile () +filename);} Else{fos = new FileOutputStream (Getfilesdir () +filename); Bitmap.compress (Bitmap.CompressFormat.JPEG, N, Fos); Iamgeview.setimagebitmap (bitmap);} catch (FileNotFoundException e) {e.printstacktrace ();}}  @Override public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu, this adds items to the action Bar        If it is present.        Getmenuinflater (). Inflate (R.menu.main, menu);    return true; } @Override public boolean onoptionsitemselected (MenuItem Item) {//Handle Action Bar item clicks here. The action bar would//automatically handle clicks on the Home/up button, So long/As you specify a the parent activity in Androidmanifest.xml.        int id = item.getitemid ();        if (id = = r.id.action_settings) {return true;    } return super.onoptionsitemselected (item); }}

  

Android Download picture/Call system camera to take pictures, display and save to local

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.