[Size = medium] [/size] I have been studying and developing images for android systems and browsing audio and video over the past few days. This is the first android system (not in java)
It took me several days to browse and select images, audios, and videos.
I organized it so that I could help my classmates and guard against their forgetting.
<1> code of the select button
// Select an image and click the event
Public void click_xuanqutupian (View source ){
Intent intent = new Intent ();
/* Enable Pictures image Type to set to image */
Intent. setType ("image /*");
// Intent. setType ("audio/*"); // select the audio
// Intent. setType ("video/*"); // select a video (mp4 3gp is a video format supported by android)
/* Use the Intent. ACTION_GET_CONTENT Action */
Intent. setAction (Intent. ACTION_GET_CONTENT );
/* Return to the current screen after obtaining the photo */
StartActivityForResult (intent, 1 );
} <2> place to be processed after obtaining the selected item
Java code
@ Override
Protected void onActivityResult (int requestCode, int resultCode, Intent data ){
// Select the image Return Value
If (requestCode = 1 ){
//
If (resultCode = RESULT_ OK ){
Uri uri = data. getData ();
Cursor cursor = getContentResolver (). query (uri, null, null,
Null, null );
Cursor. moveToFirst ();
// String imgNo = cursor. getString (0); // Image number
ImgPath = cursor. getString (1); // image file path
String imgSize = cursor. getString (2); // image size
String imgName = cursor. getString (3); // Image File Name
FileName = imgName;
FileSize = imgSize;
// Log. e ("uri", uri. toString ());
ContentResolver cr = this. getContentResolver ();
Try {
Bitmap bitmap = BitmapFactory. decodeStream (cr
. OpenInputStream (uri ));
ImageView imageView = (ImageView) findViewById (R. id. imview );
/* Set Bitmap to ImageView */
ImageView. setImageBitmap (bitmap );
// ImageView. setImageURI (uri); // you can directly display the image;
} Catch (FileNotFoundException e ){
// Log. e ("Exception", e. getMessage (), e );
}
}
}
// Response value
If (requestCode = 2 ){
If (resultCode = RESULT_ OK ){
//
ImgPath = data. getStringExtra ("filePath ");
FileName = data. getStringExtra ("fileName ");
FileSize = data. getStringExtra ("fileSize ");
// Read the file obtained from the photo
Try {
Bitmap bitmap = this. getLoacalBitmap (imgPath );
ImageView imageView = (ImageView) findViewById (R. id. imview );
ImageView. setImageBitmap (bitmap );
} Catch (Exception e ){
// TODO: handle exception
}
//
}
}
Super. onActivityResult (requestCode, resultCode, data );
Uri introduction:
Uri indicates the data to be operated. Uri mainly contains two parts: 1. ContentProvider to be operated; 2. what data is operated on in ContentProvider, a Uri consists of the following parts:
L
ContentProvider (content provider)'s scheme has been specified by Android, and scheme is: content ://
The Host Name (or Authority) is used to uniquely identify the ContentProvider. External callers can find it based on this identifier.
The path can be used to indicate the data to be operated. The path construction depends on the business, as shown below:
To operate records with the id of 10 in the person table, you can build the path:/person/10.
Name field of the record whose id is 10 in the person table to be operated, person/10/name
To operate on all records in the person table, you can create a path like this:/person
To operate records in the xxx table, you can build the path:/xxx
Of course, the data to be operated may not come from the database, but may also be stored in files, as follows:
To operate on the name node under the person node in the xml file, you can build the path:/person/name
To convert a string to a Uri, you can use the parse () method in the Uri class as follows:
Uri uri = Uri. parse ("content: // cn. itcast. provider. personprovider/person ")
Author "dennies211"