yiyayiyayou

android 隐式intent 图片浏览器

hi 贵有恒何必三更起5更眠。 早上的心情也是相当的happy可惜木有小表情哈,也是遗憾了。

这篇内容就是标题所示,用intent 创建一个图片浏览器。主要功能:浏览本地图片的可以唤起自己的app,网络下载的时候也能被唤起, 功能效果上图(前3位本地,后1为浏览器下载打开)。

  

 

文字描述实现效果:查看选择图片查看activity,选择自己的可以进行正常显示,同时查看网络下载下来的图片也可以唤起app之后正常显示。问题就来了,要解决重点什么时候被唤起,如何被唤起。对应方法就是用intent的隐式写法和对应的标签进行搭配,直接上代码了~~ 

//主界面 

public class MainActivity extends Activity implements OnClickListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

findViewById(R.id.btnStart).setOnClickListener(this);
}

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub

Intent i = new Intent(Intent.ACTION_VIEW);
// i.setAction(action)
//把创建的好的文件放入intent
i.setDataAndType(Uri.fromFile(GetPhoto.getPhoto(this)), "image/*");
startActivity(i);
}

 

//创建好本地图片准备被调用

public class GetPhoto {
public static File getPhoto(Context context) {
File sdCard = Environment.getExternalStorageDirectory();
File file = new File(sdCard.getAbsolutePath(), "www.jpg");
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.test);

try {
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return file;
}
}

 

//图片浏览显示页面

public class PhotoBrower extends Activity{
private ImageView iv;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);

iv = new ImageView(this);
iv.setImageURI(getIntent().getData());
setContentView(iv);
}
}

 

//重要的配置文件

 直接上代码果然是最简单的哈~。~。 


<!-- 向sd写入权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="18" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.photobrower.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>


<activity android:name="com.example.photobrower.PhotoBrower">
<intent-filter >
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.BROWSABLE"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="image/*"/>
</intent-filter>




</activity>
</application>

注意点:

1、MainActivity里面需要重点的注意的就是启动intent的时候 一定要添加 Intent.ACTION_VIEW这个action(不添加这个哪怕是 直接定义启动 图片浏览器 Action也不好使,还会是后面这个异常),如果没有添加这句,就会报 ActivityNotFound异常(这和被启动的activity没有添加 category 的 DEFAULT 属性是同样的错误);

2、图片会被创建在本地内存卡中,这个需要权限 WRITE_EXTERNAL_STORAGE  write_external_storage;

3、图片能够在 浏览器被打开,需要category的  category.BROWSABLE  ;

4、只要是想被隐式启动就需要 category category.DEFAULT 。

今天先写到这,下次再见么么哒~~.~~

 

分类:

技术点:

相关文章: