Android: how to open a file on a mobile phone

Source: Internet
Author: User

// File indicates the file path.

Private void openfile (File file ){

Intent intent = new intent ();
Intent. addflags (intent. flag_activity_new_task );
// Set the intent action attribute
Intent. setaction (intent. action_view );
// Obtain the MIME type of the file
String type = getmimetype (File );
// Set the data and type attributes of intent.
Intent. setdataandtype (URI. fromfile (file), type );
// Jump
Startactivity (intent );
}

 

// Method for Determining the file mimetype

Private string getmimetype (file F ){
String type = "";

String fname = f. getname ();

// Get the extension

String end = fname. substring (fname. lastindexof (".") + 1, fname. Length (). tolowercase ());

// Determine the MIME type based on the extension

If (end. equals ("m4a") | end. equals ("MP3") | end. equals ("mid") | end. equals ("xmf") | end. equals ("Ogg") | end. equals ("WAV ")){

Type = "audio ";}

Else if (end. Equals ("3GP") | end. Equals ("MP4 ")){
Type = "video ";

}

Else if (end. equals ("jpg") | end. equals ("GIF") | end. equals ("PNG") | end. equals ("Jpeg") | end. equals ("BMP ")){

Type = "image ";

}

Else if (end. Equals ("APK ")){
// Open the APK installation program and register Android. Permission. install_packages in androidmanifest.

Type = "application/vnd. Android. Package-Archive ";

}

Return type;

}

Related Article

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.