先在res/drawable-hdpi下准备两张内容一样但大小不同的图片,小的图片名为small,大的图片名为big。
在main.xml中:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageButton
android:id="@+id/mybut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/small" />
</LinearLayout>
在主Activity(MyIntentCaseDemo.java)中:
package com.li.intentcaseproject;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
public class MyIntentCaseDemo extends Activity {
private ImageButton mybut = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
this.mybut = (ImageButton) super.findViewById(R.id.mybut);
this.mybut.setOnClickListener(new OnClickListenerImpl());
}
private class OnClickListenerImpl implements OnClickListener {
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
MyIntentCaseDemo.this.startActivity(Intent.createChooser(intent,
"请选择图片浏览工具"));
//MyIntentCaseDemo.this.startActivity(intent); 可以选择默认打开
}
}
}
在ImageViewActivity.java中:
package com.li.intentcaseproject;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
public class ImageViewActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setTitle("查看图片") ;
ImageView img = new ImageView(this) ;
img.setImageResource(R.drawable.big) ;
super.setContentView(img) ;
}
}
在AndroidManifest.xml中修改权限:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.li.intentcaseproject"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application android:icon="@drawable/ic_launcher" android:label="@string/app_name">
<activity android:name=".MyIntentCaseDemo"
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=".ImageViewActivity">
<intent-filter>
<action android:name="android.intent.action.GET_CONTENT" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.OPENABLE" />
<data android:mimeType="image/jpeg" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
</manifest>