Android project notepad (13) ----- view pictures and play recordings

Source: Internet
Author: User

Android project notepad (13) ----- view pictures and play recordings

This article is my learning notes, welcome to reprint, but please note the Source: http://blog.csdn.net/jesson20121020

Today, we will implement the function of viewing images and recordings. When editing or browsing the notebook, click an image to open a custom Activity (of course, you can also call the image library of the system to view it) to view the original image of the added image, rather than the scaled image. Similarly, you can use a custom Activity to view the recording file to play the recording. :

We can also see that we must first create two activities. Of course, layout files are also indispensable, as shown below:

Activity_show_picture.xml

 
         
  
 
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 the title TextView TV _title = (TextView) findViewById (R. id. TV _title); TV _title.setText ("view image"); 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 the image of the specified path, which was passed in from the previous Activity. Here, only the returned function is implemented for the listener event. As for how to zoom in and out the image and rotate the image, we will implement it in the next section. Activity_show_record.xml
 
   
              
         
            
             
             
              
       
     
 
ShowRecord. java

Public class ShowRecord extends Activity {private String audioPath; private int isPlaying = 0; private AnimationDrawable ad_left, ad_right; private Timer mTimer; // audio operation object private MediaPlayer mPlayer = null; private ImageView outputs, 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 the title 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. required); ad_left = (AnimationDrawable) lost (); // ad_left = (AnimationDrawable) lost (); ad_right = (AnimationDrawable) lost (); // 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 <59) {second ++;} else if (second = 59 & minute <59) {minute ++; second = 0;} if (second = 59 & minute = 59 & 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 <10) time [2] = "0" + second; if (minute <10) time [1] = "0" + minute; if (hour <10) time [0] = "0" + hour; // display TV _recordTime.setText (time [0] + ":" + time [1] + ":" + time [2]); break ;}} in TextView ;}}}; class ClickEvent implements OnClickListener {@ Overridepublic void onClick (View arg0) {// TODO Auto-generated method stub // test 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-generated catch blocke. printStackTrace ();} mTimer. schedule (new TimerTask () {@ Overridepublic void run () {Message message = new Message (); message. what = 1; handler. sendMessage (message) ;}},); // play the animation ad_left.start (); ad_right.start () ;}// end the test else {isPlaying = 0; mPlayer. stop (); mPlayer. release (); mPlayer = null; mTimer. cancel (); mTimer = null; // stop the animation seek (); ad_right.stop () ;}} class MediaCompletion implements OnCompletionListener {@ Overridepublic void onCompletion (MediaPlayer mp) {mTimer. cancel (); mTimer = null; isPlaying = 0; // stop the animation ad_left.stop (); ad_right.stop (); Toast. makeText (ShowRecord. this, "playing completed", Toast. LENGTH_SHORT ). show (); TV _recordTime.setText ("00:00:00 ");}}}
When viewing the recording, it is a little complicated to use the display and processing of the playing time, as well as the playing and stopping of the animation. These are described in the previous section "add recording.


With these two activities, the rest of the work is to start the two activities in the event of clicking an image or recording. However, how can we determine whether an image, recording, or text is clicked in the EditText in a text-and-text mix ?? In this case, you need to identify the images and texts in EditText, and then further analyze the images and click the images to view the specific images and recordings, the details are as follows:

1. Record the location and Source Path of each image in EditText

To view the original image and Recording files at any time during editing and browsing, use a List to remember the location and path of each newly added image or recording. Of course, if you are viewing records that already exist in the database, while loading data, use ListView to record the location and path of the pictures and recordings. The main code is as follows:

// Record the image in editText, which is used to determine the private List of the image clicked upon clicking
 
  
> ImgList = new ArrayList
  
   
> ();
  
 

Each time you click the recording list, you can view the recording. when loading the data, you can record the positions and paths of all the images and recordings. Specifically, you can add the following code to the loadDate () method:

// Use List to record the recording position and path, and click event Map
 
  
Map = new HashMap
  
   
(); Map. put ("location", m. start () + "-" + m. end (); map. put ("path", path); imgList. add (map );
  
 
Similarly, you also need to add the corresponding code after each image recording operation. Add the following code to the InsertBitmap () function:

// Use List to record the recording position and path, and click event Map
 
  
Map = new HashMap
  
   
(); 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 an image or plain text is clicked.

To determine whether an image or a common text is clicked, the main idea of Spanned and ImageSpan is to determine whether the current clicked position is within the range of the image. 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 the image if (selectionStart> = start & selectionStart <end ){......}}
After the image is hit, determine which image is clicked?

4. Click the image.

The position and path of the image recorded in step 1 are used. Obviously, the position is used to determine the image to be clicked. The main code is as follows:

// Find the image you are currently clicking. // System. out. println (start + "-----------" + end); String path = null; for (int I = 0; I 

5. Determine the recording or image, start the corresponding Activity, and pass the path

There are two ways to view images. One is to call the image library of the system to open the image, and the other is to customize the image, A custom Activity is used to open a recording, as shown below:

// Then judge whether the current image is a recording. If it is a recording, the Activity of the recording will jump to. If not, the Activity will jump to the interface for viewing the image // recording, then jump to the Activityif (path. substring (path. length ()-3, path. length ()). equals ("amr") {Intent intent = new Intent (AddActivity. this, ShowRecord. class); intent. putExtra ("audioPath", path); startActivity (intent) ;}// for the image, you can jump to the image display page else {// you can view the image in either of the following ways, the first method is to directly call the image library of the system to view images, and the second method is to customize the Activity // call the system image library to view images/* Intent intent = new Intent (Intent. ACTION_VIEW); File file = new File (path); Uri uri = Uri. fromFile (file); intent. setDataAndType (uri, "image/*"); * // use custom ActivityIntent intent = new Intent (AddActivity. this, ShowPicture. class); intent. putExtra ("imgPath", path); startActivity (intent );}
The above steps, 3, 4, and 5 are actually implemented in the click listener. The complete code is as follows:

// Set the class TextClickEvent implements OnClickListener {@ Overridepublic void 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 the image if (selectionStart> = start & selectionStart <end) {// Bitmap bitmap = (BitmapDrawable) span. getDrawable ()). getBitmap (); // find the image you are currently clicking. // System. out. println (start + "-----------" + end); String path = null; for (int I = 0; I  

Now, you can 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.