Notepad for Android Projects-----View images and play recordings

Source: Internet
Author: User

This article is your own study notes, welcome reprint, but please specify the source:http://blog.csdn.net/jesson20121020

Today to realize the function of viewing pictures and recordings, when editing or browsing notes, click on the picture, open a custom activity(of course, you can also call the system's Tukulai view) to see the original picture of the added picture, instead of the scaled picture, similarly, Use the custom activity to view the recording files and realize the function of playing the recording. :

It can also be seen that we first create two activity, of course, layout files are also indispensable, as follows:

Activity_show_picture.xml

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" match_parent "    android:layout_height=" match_parent "    android:o rientation= "vertical"     >        <imageview         android:id= "@+id/iv_showpic"        android:layout_width= " Match_parent "        android:layout_height=" match_parent "        android:scaletype=" Fitcenter "        /></ Linearlayout>
Showpicture.java

public class Showpicture extends Activity {private ImageView img; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Requestwindowfeature (window.feature_ Custom_title); Setcontentview (r.layout.activity_show_picture); GetWindow (). Setfeatureint (Window.feature_custom_ Title, r.layout.title_add);//Set title TextView Tv_title = (TextView) Findviewbyid (r.id.tv_title); Tv_title.settext ("View Picture" ); Button Bt_back = (button) Findviewbyid (r.id.bt_back); Bt_back.setonclicklistener (new Onclicklistener () {@ overridepublic void OnClick (View arg0) {ShowPicture.this.finish ();}}); Button Bt_del = (button) Findviewbyid (R.id.bt_save); Bt_del.setbackgroundresource (r.drawable.paint_icon_delete); img = (ImageView) Findviewbyid (r.id.iv_showpic); Intent Intent = This.getintent (); String Imgpath = Intent.getstringextra ("Imgpath"); Bitmap BM = bitmapfactory.decodefile (Imgpath); Img.setimagebitmap (BM);}} 
      The main idea is to use ImageView to display a picture of the specified path, which was passed in from the previous activity. Here the monitoring event, only realized the function of return, as for, zoom out the picture, rotate the picture, the next section to achieve it. Activity_show_record.xml
<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Match_parent "android:layout_height=" match_parent "android:background=" @drawable/          BG "> <linearlayout android:layout_width=" match_parent "android:layout_height=" Match_parent "          android:orientation= "vertical" android:gravity= "center" android:layout_centerinparent= "true" > <linearlayout android:layout_width= "match_parent" android:layout_height= "Wrap_c          Ontent "android:orientation=" horizontal "android:gravity=" center "android:layout_margin=" 5DP "        > <imageview android:id= "@+id/iv_record_wave_left" android:layout_width= "Wrap_content"        android:layout_height= "wrap_content" android:layout_gravity= "center" android:layout_margin= "5DP" Android:background= "@anim/Record_wave_left "/> <imageview android:id=" @+id/iv_microphone "Android:layout_width=" W Rap_content "android:layout_height=" wrap_content "android:layout_gravity=" center "android:src=" @drawa Ble/record_microphone_icon "android:layout_margin=" 5DP "/> <imageview android:id=" @+id /iv_record_wave_right "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "Andro        id:layout_gravity= "center" android:layout_margin= "5DP" android:background= "@anim/record_wave_right" /> </LinearLayout> <textview android:id= "@+id/tv_recordtime" android:l Ayout_width= "Match_parent" android:layout_height= "wrap_content" android:textcolor= "#499df7" android:t Extsize= "20SP" android:text= "00:00:00" android:gravity= "center" android:layout_margin= "5DP"/&    Gt </linearlayout> </RelativeLayout> 
Showrecord.java

public class ShowRecord extends Activity {private String audiopath;private int isplaying = 0;private animationdrawable ad_ Left,ad_right;private Timer mtimer;//Voice Operation object Private MediaPlayer MPlayer = null;private ImageView iv_record_wave_left,iv _record_wave_right,iv_microphone;private TextView tv_recordtime; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate (savedinstancestate); Requestwindowfeature ( Window.feature_custom_title); Setcontentview (R.layout.activity_show_record); GetWindow (). Setfeatureint ( Window.feature_custom_title, R.layout.title_add);//Set caption TextView Tv_title = (TextView) Findviewbyid (r.id.tv_title); Tv_title.settext ("View Recording"); Button Bt_back = (button) Findviewbyid (r.id.bt_back); Bt_back.setonclicklistener (new Onclicklistener () {@ overridepublic void OnClick (View arg0) {if (isplaying = = 1) {mplayer.stop (); Mplayer.release ();} ShowRecord.this.finish ();}}); Button Bt_del = (button) Findviewbyid (R.id.bt_save); Bt_del.setbackgroundresource (R. drawable.paint_icon_delete); Intent Intent = This.getintent (); audiopath = Intent.getstringextra ("audiopath"); iv_ Microphone = (ImageView) Findviewbyid (R.id.iv_microphone); Iv_microphone.setonclicklistener (new Clickevent ()); iv_ Record_wave_left = (ImageView) Findviewbyid (r.id.iv_record_wave_left); iv_record_wave_right = (ImageView) Findviewbyid (r.id.iv_record_wave_right); ad_left = (animationdrawable) iv_record_wave_left.getbackground ();//ad_ left = (animationdrawable) iv_record_wave_left.getdrawable (); ad_right = (animationdrawable) iv_record_wave_ Right.getbackground ();//ad_right = (animationdrawable) iv_record_wave_right.getdrawable (); tv_recordTime = (TextView ) Findviewbyid (r.id.tv_recordtime);} Final Handler Handler = new Handler () {public void Handlemessage (Message msg) {switch (msg.what) {Case 1:string time [] = Tv_recordtime.gettext (). toString (). Split (":"); int hour = Integer.parseint (time[0]); int minute = Integer.parseint ( TIME[1]); int second = Integer.parseint (time[2]); if (second <) {second++;} else if (second = = && minute <) {Minute++;second = 0;} if (second = = && minute = = && Hour < 98) {Hour++;minute = 0;second = 0;} Time[0] = hour + ""; time[1] = minute + ""; time[2] = second + "";//Adjust the format to display on the screen if (second <) time[2] = "0" + second;if (mi Nute <] time[1] = "0" + minute;if (Hour <) time[0] = "0" + hour;//shown in TextView tv_recordtime.settext (time[0]+ ":" +t ime[1]+ ":" +time[2]); break;}}; Class Clickevent implements onclicklistener{@Overridepublic void OnClick (View arg0) {//TODO auto-generated method stub// Try to hear if (isplaying = = 0) {isplaying = 1;mplayer = new MediaPlayer (); Tv_recordtime.settext ("00:00:00"); Mtimer = new Timer (); Mplayer.setoncompletionlistener (New Mediacompletion ()); try {mplayer.setdatasource (audiopath); MPlayer.prepare (); Mplayer.start ();} catch (IllegalArgumentException e) {//TODO auto-generated catch Blocke.printstacktrace ();} catch (SecurityException e) { TODO auto-generated catch Blocke.printstacktrace ();} catch (IllEgalstateexception e) {//Todo auto-generated catch Blocke.printstacktrace ();} catch (IOException e) {//Todo Auto-generat Ed catch Blocke.printstacktrace ();} Mtimer.schedule (New TimerTask () {@Overridepublic void run () {Message message = new Message (); message.what = 1;handler.sen dmessage (Message),}}, 1000,1000);//Play Animation Ad_left.start (); Ad_right.start ();} End Audition else{isplaying = 0;mplayer.stop (); Mplayer.release (); mPlayer = Null;mtimer.cancel (); mtimer = null;//Stop animation ad_ Left.stop (); Ad_right.stop ();}}} Class Mediacompletion implements oncompletionlistener{@Overridepublic void Oncompletion (MediaPlayer MP) { Mtimer.cancel (); mtimer = null;isplaying = 0;//Stop animation ad_left.stop (); Ad_right.stop (); Toast.maketext (Showrecord.this, "play Complete", Toast.length_short). Show (); Tv_recordtime.settext ("00:00:00");}}
When viewing the recording, the display processing of the playback time, as well as the playback and stop of the animation, a little bit more complex, these are described in the previous "Add Recording" section.


With these two activities, the rest of the work is to start the two activity in the event of clicking on a picture or recording. However, there is a problem, how to judge in the edittext of the picture and text mixed row is the image, recording, or writing?? This needs to be identified from the EditText, those are the pictures, those are the words, and then further to the image analysis is exactly the click of the picture, so as to achieve the view of specific pictures and recording functions, as follows:

1. Record the location and source path of each picture in the EditText

In order to be able to view the original picture and the recording file at any time while editing and browsing, after each add picture or recording, use a list to remember the location and path of each newly added picture or recording, of course, if you are browsing a note that already exists in the database, while loading the data, Also use the ListView to remember the location and path of all pictures and recordings. The main code is as follows:

Record the picture in EditText and click on it to determine which one is clicked. Private list<map<string,string>> imglist = new arraylist<map< String,string>> ();

Each time you click on a note list item, go to the view notes, while loading the data, the location and path of all pictures and recordings are recorded, specifically add the following code in the Loaddate () method:

Use list to record the location and path of the recording for clicking event map<string,string> Map = new hashmap<string,string> (); Map.put ("Location", M.start () + "-" +m.end ()); Map.put ("path", path); Imglist.add (map);
In the same vein, add the corresponding code to the Insertbitmap () function by adding the following code after each picture recording:

Use list to record the location and path of the recording for clicking event        map<string,string> Map = new hashmap<string,string> ();        Map.put ("Location", selectionindex+ "-" + (Selectionindex+spannablestring.length ()));        Map.put ("path", Imgpath);        Imglist.add (map);

2. Add a click event to EditText

Private Lineedittext et_notes; Et_notes.setonclicklistener (New Textclickevent ());

3. determine whether you clicked on a picture or plain text

In order to determine whether the click is a picture or ordinary text, the use of Spanned,imagespan, the main idea is to determine if the location of the current click is within the location of the picture, the main code is as follows:

spanned s = Et_notes.gettext (); imagespan[] Imagespans;imagespans = S.getspans (0, S.length (), imagespan.class); int SelectionStart = Et_notes.getselectionstart (); for (Imagespan span:imagespans) {int start = S.getspanstart (span); int end                                     = S.getspanend (span);//Find picture if (SelectionStart >= start && SelectionStart < end) { ... ...                                 }}
Hit the picture, the next thing to determine the click is the Picture it?

4. Decision click on the specific picture

This uses the first step to record the location and path of the picture, obviously, is to use the location to determine the exact click of the picture, the main code is as follows:

Find which picture is currently clicked//system.out.println (start+ "-----------" +end); String path = null;for (int i = 0;i < Imglist.size (); i++) {Map map = Imglist.get (i);//Found if (Map.get ("location"). Equals (S tart+ "-" +end)) {path = Imglist.get (i). Get ("path"); break;}}

5. Determine whether it is a recording or a picture, initiate the corresponding activity, and pass the path

Look at the picture, there are two ways, one is to call the system's library to open the picture, the other is to customize, here, I have implemented, open the recording with a custom activity, as follows:

Then determine whether the current picture is a recording, if it is recorded, then jump to the activity of the audition recording, if not, then jump to view the image of the interface//recording, then jump to the audition recording Activityif (Path.substring (Path.length ()- 3, Path.length ()). Equals ("Amr")) {Intent Intent = new Intent (addactivity.this,showrecord.class); Intent.putextra (" Audiopath ", path); startactivity (intent);} Picture, then jump to view the image of the interface else{//there are two ways to view the picture, the first is to directly call the system's library to view the picture, the second is to customize the activity//call system library to view the picture/*intent Intent = new Intent ( Intent.action_view); File File = new file (path); Uri uri = uri.fromfile (file), Intent.setdataandtype (URI, "image/*"), *///using custom activityintent intent = new Intent ( Addactivity.this,showpicture.class); Intent.putextra ("Imgpath", Path); startactivity (intent);}
Above, the 3,4,5 step is actually implemented in the listener of the click, the complete code is as follows:

       //Set Listener class Textclickevent implements onclicklistener{@Overridepublic void for Eidttext OnClick (View v) {spanned s = et_notes.gettext (); imagespan[] Imagespans;imagespans = S.getspans (0, S.length (), imagespan.class); int selectionstart = Et_notes.getselectionstart (); for (Imagespan span:imagespans) {int start = S.getspanstart (span); int end = S.getspanend (span);//Find picture if (SelectionStart >= start && SelectionStart < End) {//bitmap Bitmap = ((bitmapdrawable) span.getdrawable ()). Getbitmap ();//Find the picture that is currently clicked//system.out.println ( start+ "-----------" +end); String path = null;for (int i = 0;i < Imglist.size (); i++) {Map map = Imglist.get (i);//Found if (Map.get ("location"). Equals (S tart+ "-" +end)) {path = Imglist.get (i). Get ("path"); break;}} Then determine whether the current picture is a recording, if it is recorded, then jump to the activity of the audition recording, if not, then jump to view the image of the interface//recording, then jump to the audition recording Activityif (Path.substring (Path.length ()- 3, Path.length ()). Equals ("Amr")) {Intent Intent = new Intent (addactivity.this,showrecord.class); Intent.putextra (" Audiopath ", pATH); startactivity (intent);} Picture, then jump to view the image of the interface else{//there are two ways to view the picture, the first is to directly call the system's library to view the picture, the second is to customize the activity//call system library to view the picture/*intent Intent = new Intent ( Intent.action_view); File File = new file (path); Uri uri = uri.fromfile (file), Intent.setdataandtype (URI, "image/*"), *///using custom activityintent intent = new Intent ( Addactivity.this,showpicture.class); Intent.putextra ("Imgpath", Path); startactivity (intent);}} else//If you click Blank out or text, you get the focus, that is, open the soft keyboard imm.showsoftinput (et_notes, 0);}}

At this point, the ability to view pictures and play recordings is realized.








Notepad for Android Projects-----View images and play recordings

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.