A base class for selecting photos

Source: Internet
Author: User
Tags dateformat


/**
* Created by PC on 2016/5/10.
* Select the base class for the photo
*/
public class Photoactivity extends Baseactivity implements View.onclicklistener {


private static final int photo_request_takephoto = 1;//photo
private static final int photo_request_gallery = 2;//Select from album
private static final int photo_request_cut = 3;//result
Private File tempfile;
Private String Savebmptosd_path;
/**
* Screen cut ratio
*/
private int scale = 1;

/**
* Save Picture Path
*/
Private hashmap<string,string> Pathmap = new hashmap<> ();
Private ImageView Iv_photo;
Private String Mapkey;

protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);

InitDialog ();
}


Private Dialog Dialog;
private void InitDialog () {
Dialog = New dialog (this, r.style.dialog);
Dialog.setcontentview (r.layout.dialog_layout);
Window Dialogwindow = Dialog.getwindow ();
Dialog.setcanceledontouchoutside (TRUE);//Set Tap Screen dialog disappear
Dialogwindow.setlayout (LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
Windowmanager.layoutparams LP = Dialogwindow.getattributes ();
Dialogwindow.setgravity (Gravity.bottom);
Dialogwindow.setwindowanimations (R.style.dialogwindowanim); Setting the window Popup animation
TextView Tv_photo = (TextView) Dialog.findviewbyid (R.id.tv_photo);
Tv_photo.setonclicklistener (this);
TextView Tv_camera = (TextView) Dialog.findviewbyid (R.id.tv_camera);
Tv_camera.setonclicklistener (this);
TextView tv_close = (TextView) Dialog.findviewbyid (r.id.tv_close);
Tv_close.setonclicklistener (this);


String DIR = environment.getexternalstoragedirectory () + "/dcim/camera";
File Dirpath = new file (DIR);
if (!dirpath.exists ()) {
Dirpath.mkdirs ();
}
String name = Getphotofilename ();
Photopath = dir+ "/" +name;


Tempfile = new File (environment.getexternalstoragedirectory () + "/dcim/camera", Getphotofilename ());
}


@Override
public void OnClick (View v) {
Switch (V.getid ()) {
Case R.id.tv_photo:
Intent = new Intent (Intent.action_pick, NULL);
Intent.setdataandtype (MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
Startactivityforresult (Intent, photo_request_gallery);
Dialog.dismiss ();
Break
Case R.id.tv_camera:
Intent = new Intent (mediastore.action_image_capture);//Open camera
Intent.putextra (Mediastore.extra_output, Uri.fromfile (tempfile));
Startactivityforresult (Intent, photo_request_takephoto);///mnt/sdcard/dcim//img_20151127_143744.jpg
Dialog.dismiss ();
Break
Case R.id.tv_close:
Dialog.dismiss ();
Break


Default
Break
}
}


@Override
protected void Onactivityresult (int requestcode, int resultcode, Intent data) {
LOG.E ("onactivityresult==>", "" "+" Running ");
Switch (requestcode) {
Case Photo_request_takephoto:
Startphotozoom (Uri.fromfile (tempfile), 150);
Break

Case Photo_request_gallery:
if (data! = NULL)
Startphotozoom (Data.getdata (), 150);
Break
Case Photo_request_cut:
if (data! = NULL)
Setpictoview (data);
Break
Default
Break
}
}


private void Startphotozoom (URI uri, int size) {
Intent Intent = new Intent ("Com.android.camera.action.CROP");
Intent.setdataandtype (URI, "image/*");
Crop true is set to set the displayed view in the open intent can be clipped
Intent.putextra ("Crop", "true");

Aspectx Aspecty is the ratio of width to height
Intent.putextra ("Aspectx", 1);
Intent.putextra ("Aspecty", scale);


Intent.putextra ("Nofacedetection", true);//Remove face recognition (directly put the Intercept box on the face)
Outputx,outputy is the width height of the cropped picture
Intent.putextra ("Outputx", size);
Intent.putextra ("Outputy", size);
Intent.putextra ("Return-data", true);
Open an activity with a return value, the request code is Photo_request_cut
Startactivityforresult (Intent, photo_request_cut);
}

Displays the cropped picture to the UI interface
private void Setpictoview (Intent picdata) {
Bundle bundle = Picdata.getextras ();
if (bundle! = null) {
Bitmap photo = bundle.getparcelable ("Data");
if (photo = = null) {
String url = bundle.getstring ("Croppedpath");
if (imagehelper.exist (URL)) {
Photo = Imagehelper.getimage (URL, 100);
}

}
if (photo! = null) {

Imgs[index].setimagebitmap (photo);
Tvphotos[index].setvisibility (View.gone);

Compress pictures
Bitmap Sbitmap = imagehelper.compressimage (photo, 50);
Bitmap Sbitmap = bitmaputil.comp (photo);
Iv_photo.setimagebitmap (SBITMAP);
Save picture
String filename = Getphotofilename ();
Savebmptosd_path = IMAGEHELPER.SAVEBMPTOSD (sbitmap, filename, 100);
if (Imagehelper.exist (Savebmptosd_path)) {
Pathmap.put (Mapkey, Savebmptosd_path);
Photo.recycle ();
photo = null;
Sbitmap.recycle ();
Sbitmap = null;
}
}else {
Showtoase.showtoast (This, "picture truncation anomaly");
}
}
}


Use the system's current date to adjust the name of the photo
Private String Getphotofilename () {
Date date = new Date (System.currenttimemillis ());
SimpleDateFormat DateFormat = new SimpleDateFormat ("' IMG ' _yyyymmdd_hhmmss");
return Dateformat.format (date) + ". jpg";
}

/**
* Upload is called
* @return
*/
Protected hashmap<string,string> Getmap () {
return pathmap;
};

/**
* Display
* @param img
* Key field required by @param key server
*/
protected void ShowDialog (ImageView img, String key) {
iv_photo= img;
mapkey= key;
Dialog.show ();
}


}




<!--General Dialog-->
<style name= "Dialog" >
<item name= "Android:windowframe" > @null </item>
<!--dialog Windowframe box is none---
<item name= "Android:windowisfloating" >true</item>
< whether!--is on the activity--
<item name= "Android:windowistranslucent" >true</item>
< is!--Translucent---
<item name= "Android:windownotitle" >true</item>
<item name= "android:backgrounddimenabled" >true</item>
<item name= "Android:windowbackground" > @android:color/transparent</item>
</style>



<?xml version= "1.0" encoding= "Utf-8"?>
<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:id= "@+id/layout_root"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
>

<linearlayout
Android:id= "@+id/ll_dialog"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:orientation= "Vertical"
android:gravity= "Center"
android:background= "@color/blue" >
<textview
Android:id= "@+id/tv_photo"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "Albums"
Android:textcolor= "@color/white"
Android:padding= "15dip"
android:gravity= "Center"/>
<view
Android:layout_width= "Fill_parent"
android:layout_height= "0.5dip"
android:background= "#c2c2c2"/>
<textview
Android:id= "@+id/tv_camera"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "Taking pictures"
Android:textcolor= "@color/white"
Android:padding= "15dip"
android:gravity= "Center"/>
<view
Android:layout_width= "Fill_parent"
android:layout_height= "0.5dip"
android:background= "#c2c2c2"/>
<textview
Android:id= "@+id/tv_close"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
Android:text= "Cancel"
Android:textcolor= "@color/white"
Android:padding= "15dip"
android:gravity= "Center"/>
</LinearLayout>


</RelativeLayout>





A base class for selecting photos

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.