Android之旅第三站——Android默认打开各种文件格式DataAndType…

来源:互联网 发布:sql sever设置主键 编辑:程序博客网 时间:2024/06/06 00:38

在你手机中,打开文件的方式有这么几种:

1、通过要打开的这个文件的类型找到对应的应用程序app打开

2、通过已知文件类型打开应用程序,找到对应的这个文件打开

简单点说,通过文件找对应app,通过app找对应的文件。

那么在安卓中是怎样实现这一效果的呢?

咱们来实现这一一个程序:

通过点击按钮,实现播放歌曲、打开浏览器等的功能。

效果图:

这里写图片描述

当点击播放歌曲时,会自动调用本地播放器播放指定歌曲,(事先必须存入模拟器当中)。

这里写图片描述

将文件存入模拟器方法:

在模拟器启动状态下,在右上角平台切换到DDMS中,左边Devices设备下面会有模拟器信息线程。

在右边窗口中切换到file Explorer中,选择/mnt/media_rw/sdcard/中,
这里写图片描述

点击DDMS下面的第二个图标,将文件导入到模拟器,注意名字的合理,纯小写英文保险。

这里写图片描述

会在/mnt/media_rw/sdcard/下面出现倒入的文件。

当点击打开浏览器,会调用本地浏览器跳转到指定网站(设置的百度)。

这里写图片描述

接下来,附下代码:

Mainactivity:

package com.example.datat;import android.app.Activity;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends Activity implements OnClickListener {    private Button bt, bt1;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initViews();        registerListeners();    }    private void registerListeners() {        // TODO Auto-generated method stub        bt.setOnClickListener(this);        bt1.setOnClickListener(this);    }    private void initViews() {        // TODO Auto-generated method stub        bt = (Button) findViewById(R.id.bt);        bt1 = (Button) findViewById(R.id.bt1);    }    @Override    public void onClick(View v) {        // TODO Auto-generated method stub        switch (v.getId()) {        case R.id.bt:            Intent intent = new Intent();            intent.setAction(Intent.ACTION_VIEW);            intent.setDataAndType(Uri.parse("file://mnt/sdcard/inLove.mp3"), "audio/mp3");            startActivity(intent);            break;        case R.id.bt1:            Intent intent1 = new Intent();            intent1.setAction(Intent.ACTION_VIEW);            intent1.setData(Uri.parse("http://www.baidu.com"));            startActivity(intent1);            break;        default:            break;        }    }}

界面的xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.example.datat.MainActivity"     android:gravity="center"    >   <Button        android:id="@+id/bt"       android:layout_width="match_parent"       android:layout_height="wrap_content"       android:text="点我播放歌曲"       android:textSize="20sp"       />    <Button        android:id="@+id/bt1"       android:layout_width="match_parent"       android:layout_height="wrap_content"       android:text="点我打开浏览器网站"       android:textSize="20sp"       android:layout_below="@id/bt"       android:layout_marginTop="20dp"       /></RelativeLayout>

与之前的不同之处就是,

1、在Intent对象intent创建好之后,需要setaction,参数是Intent.ACTION_VIEW。

引用本地的View,

2、需要setdataAndtype,将网址或者资源文件转换成URI(统一资源标识)。浏览器的跳转一般是协议http,可以不用写type。

Intent-Filter中的data有一个mimeType . 它的作用是告诉Android系统本Activity可以处理的文件的类型。如设置为 “text/plain”表

示可以处理“.txt”文件。不同格式的文件,对应不同的type,例如Image的格式有gif, jpg等。

个人理解,URL是URI的虚拟路径,方便好记忆,例如www.baidu.com并不是真实地址,其实也是有一堆数字好比

192.168.1.0(其实这个是路由器地址)。

歌曲有歌曲的类型(例如mp3),网址有网址的类型(例如http),每一个类型对应自己的type(例如audio/mp3)。

下面附上完整的mime type的参考手册链接:MIME 参考手册

0 0
原创粉丝点击