Taking photos and selecting photo albums

Source: Internet
Author: User

1. Photo Album selection

Private voidSetalbumimg (inttype2) {        //TODO auto-generated Method StubPw.dismiss (); //get pictures from albumsIntent Intent =NewIntent (intent.action_get_content);        Intent.settype (Image_type);        Startactivityforresult (Intent, type2); //convert resource images to bitmap//bm= Bitmapfactory.decoderesource (MainActivity.this.getResources (),//r.drawable.ic_launcher); //Img.setimagebitmap (BM);    } 

2. Taking pictures

Private void setcameraimg (int  type2) {        //  TODO auto-generated method Stub         Pw.dismiss ();         New Intent ();        Intent.setaction (mediastore.action_image_capture);        Startactivityforresult (Intent, type2+1);    }

3 Onactivityresult: Where Img_shipper represents the photo that Imgview is going to show

    protected voidOnactivityresult (intRequestcode,intResultCode, Intent data) {        if(ResultCode = =RESULT_OK) {            //the data provided by the external program Access ContentProvider can be accessed via the Contentresolver interfaceContentresolver resolver =Getcontentresolver (); //This is the one you want to use to determine if the activity you're receiving            if(Requestcode = =type) {                Try{Uri Originaluri= Data.getdata ();//get the URI of the pictureBM =MediaStore.Images.Media.getBitmap (resolver, Originaluri); //proportional scaling of picturesBitmap Bitmap =commonutils.scaleimg (BM, Commonutils.getviewwith (img_shipper) [0], Commonutils.getviewwith (img_shipper) [1]); //scaleimg (); //appear to bitmap pictures//cutimg (Originaluri);IconPath = "Touxiang.png"; //saveimg ();Img_shipper.setimagebitmap (bitmap); } Catch(IOException e) {LOG.E ("Tag-->error", e.tostring ()); }            }            if(Requestcode ==type+1) {Bundle bundle=Data.getextras (); BM= (Bitmap) bundle.get ("Data");//get the data returned by the camera and convert it to bitmap picture format//proportional scaling of picturesBitmap bitmap= commonutils.scaleimg (BM, Commonutils.getviewwith (Img_shipper) [0], Commonutils.getviewwith (Img_shipp ER) [1]); //scaleimg (); //Picture ClippingIconPath = "Touxiang.png"; //uri uri = Uri.fromfile (FI);//cutimg (URI); //appear to bitmap picturesImg_shipper.setimagebitmap (bitmap); }            if(Requestcode = = 200) {                //get the cut data .BM = Data.getparcelableextra ("Data")); //Show clipped images//Img.setimagebitmap (BM);            }        }    }

4.

/*** Get View width high * *@return0, wide; 1, high*/     Public Static int[] Getviewwith (FinalView view) {        int[] dpi =New int[2]; intw = View.MeasureSpec.makeMeasureSpec (0, View.MeasureSpec.UNSPECIFIED); inth = View.MeasureSpec.makeMeasureSpec (0, View.MeasureSpec.UNSPECIFIED);        View.measure (W, h); dpi[1] =view.getmeasuredheight (); dpi[0] =view.getmeasuredwidth (); returndpi; }
Private voidsaveimg (Bitmap bitmap,string path, Runnable r) {//TODO auto-generated Method StubSavemybitmap (path, bitmap,r);//saving an image to an SD card    }     Public voidSavemybitmap (String bitname, Bitmap Mbitmap, Runnable r) {//first create the folder where you want to save the picture, and then create the pictureFF =NewFile (Environment.getexternalstoragedirectory (). Getabsolutefile ()+ File.separator + "Savebitmap"); if(!ff.exists ())        {Ff.mkdirs (); } fi=NewFile (FF, bitname); //FileOutputStream FOut =NULL; Try{FOut=NewFileOutputStream (FI); } Catch(Exception e) {e.printstacktrace (); }        //image CompressionBitmap bitmap=Comp (mbitmap); //Save the bitmap to fileBitmap.compress (Bitmap.CompressFormat.PNG, 100, FOut); //image UploadLoadBitmap (bitname); Try{Fout.flush (); } Catch(IOException e) {e.printstacktrace (); }        Try{fout.close (); } Catch(IOException e) {e.printstacktrace (); }    }

5. Upload Method

/*** Upload image * **/     Public voidLoadBitmap (String bitname) {FF=NewFile (Environment.getexternalstoragedirectory (). Getabsolutefile ()+ File.separator + "Savebitmap"); if(!ff.exists ())        {Ff.mkdirs (); } fi=NewFile (FF, bitname); LOG.D ("File", Fi.getabsolutepath ()); Try {            //Image Upload MethodHttpfile U =NewHttpfile (URL); U.addfileparameter ("File", FI); U.addtextparameter ("UserID", "userid")); byte[] B =U.send (); String result=NewString (b);                  SYSTEM.OUT.PRINTLN (result); Gson Gson=NewGson (); Carurl Carurl=gson.fromjson (result, Carurl.class); LOG.D ("Carurl", Carurl.url); Message msg=Mhandler.obtainmessage (); Msg.what=100; Msg.obj=R;        Mhandler.sendmessage (msg); } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); }            }

Taking photos and selecting photo albums

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.