当前位置: 编程技术>移动开发
本页文章导读:
▪从相册或者相机获得uri转为bit地图 从相册或者相机获得uri转为bitmap
package com.amani.main;import java.io.ByteArrayOutputStream;import java.io.InputStream;import android.app.Activity;import android.app.AlertDialog;import android.content.ContentResolver;import android.cont.........
▪ BIG5转UTF-八与UTF-8转BIG5 BIG5转UTF-8与UTF-8转BIG5
自定义BIG5转UTF-8
public String big52unicode(String strBIG5)
{
String strReturn="";
try
{
strReturn = new String(strBIG5.getBytes("big5"), "UTF-8");
}
catch (Exception e)
{
.........
▪ 第三章:任务管理器在运行的程序 第三章:任务管理器正在运行的程序
效果:mian.xnl
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://s.........
[1]从相册或者相机获得uri转为bit地图
来源: 互联网 发布时间: 2014-02-18
从相册或者相机获得uri转为bitmap
package com.amani.main;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentResolver;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
public class chooser extends Activity {
private ImageView imageView;
private OnClickListener imgViewListener;
Bitmap myBitmap;
private byte[] mContent;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageView = (ImageView) findViewById(R.id.ivPic);
imgViewListener = new OnClickListener() {
public void onClick(View v) {
final CharSequence[] items = { "相册", "拍照" };
AlertDialog dlg = new AlertDialog.Builder(chooser.this).setTitle("选择图片").setItems(items,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int item) {
//这里item是根据选择的方式, 在items数组里面定义了两种方式,拍照的下标为1所以就调用拍照方法
if(item==1){
Intent getImageByCamera= new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(getImageByCamera, 1);
}else{
Intent getImage = new Intent(Intent.ACTION_GET_CONTENT);
getImage.addCategory(Intent.CATEGORY_OPENABLE);
getImage.setType("image/jpeg");
startActivityForResult(getImage, 0);
}
}
}).create();
dlg.show();
}
};
//给imageView控件绑定点点击监听器
imageView.setOnClickListener(imgViewListener);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
ContentResolver resolver = getContentResolver();
/**
* 因为两种方式都用到了startActivityForResult方法,这个方法执行完后都会执行onActivityResult方法,
* 所以为了区别到底选择了那个方式获取图片要进行判断,这里的requestCode跟startActivityForResult里面第二个参数对应
*/
if (requestCode == 0) {
try {
//获得图片的uri
Uri originalUri = data.getData();
//将图片内容解析成字节数组
mContent=readStream(resolver.openInputStream(Uri.parse(originalUri.toString())));
//将字节数组转换为ImageView可调用的Bitmap对象
myBitmap = getPicFromBytes(mContent, null);
////把得到的图片绑定在控件上显示
imageView.setImageBitmap(myBitmap);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}else if(requestCode ==1){
try {
super.onActivityResult(requestCode, resultCode, data);
Bundle extras = data.getExtras();
myBitmap = (Bitmap) extras.get("data");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
myBitmap.compress(Bitmap.CompressFormat.JPEG , 100, baos);
mContent=baos.toByteArray();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//把得到的图片绑定在控件上显示
imageView.setImageBitmap(myBitmap);
}
}
public static Bitmap getPicFromBytes(byte[] bytes, BitmapFactory.Options opts) {
if (bytes != null)
if (opts != null)
return BitmapFactory.decodeByteArray(bytes, 0, bytes.length,opts);
else
return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
return null;
}
public static byte[] readStream(InputStream inStream) throws Exception {
byte[] buffer = new byte[1024];
int len = -1;
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
while ((len = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
byte[] data = outStream.toByteArray();
outStream.close();
inStream.close();
return data;
}
}
package com.amani.main;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentResolver;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
public class chooser extends Activity {
private ImageView imageView;
private OnClickListener imgViewListener;
Bitmap myBitmap;
private byte[] mContent;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageView = (ImageView) findViewById(R.id.ivPic);
imgViewListener = new OnClickListener() {
public void onClick(View v) {
final CharSequence[] items = { "相册", "拍照" };
AlertDialog dlg = new AlertDialog.Builder(chooser.this).setTitle("选择图片").setItems(items,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int item) {
//这里item是根据选择的方式, 在items数组里面定义了两种方式,拍照的下标为1所以就调用拍照方法
if(item==1){
Intent getImageByCamera= new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(getImageByCamera, 1);
}else{
Intent getImage = new Intent(Intent.ACTION_GET_CONTENT);
getImage.addCategory(Intent.CATEGORY_OPENABLE);
getImage.setType("image/jpeg");
startActivityForResult(getImage, 0);
}
}
}).create();
dlg.show();
}
};
//给imageView控件绑定点点击监听器
imageView.setOnClickListener(imgViewListener);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
ContentResolver resolver = getContentResolver();
/**
* 因为两种方式都用到了startActivityForResult方法,这个方法执行完后都会执行onActivityResult方法,
* 所以为了区别到底选择了那个方式获取图片要进行判断,这里的requestCode跟startActivityForResult里面第二个参数对应
*/
if (requestCode == 0) {
try {
//获得图片的uri
Uri originalUri = data.getData();
//将图片内容解析成字节数组
mContent=readStream(resolver.openInputStream(Uri.parse(originalUri.toString())));
//将字节数组转换为ImageView可调用的Bitmap对象
myBitmap = getPicFromBytes(mContent, null);
////把得到的图片绑定在控件上显示
imageView.setImageBitmap(myBitmap);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}else if(requestCode ==1){
try {
super.onActivityResult(requestCode, resultCode, data);
Bundle extras = data.getExtras();
myBitmap = (Bitmap) extras.get("data");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
myBitmap.compress(Bitmap.CompressFormat.JPEG , 100, baos);
mContent=baos.toByteArray();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//把得到的图片绑定在控件上显示
imageView.setImageBitmap(myBitmap);
}
}
public static Bitmap getPicFromBytes(byte[] bytes, BitmapFactory.Options opts) {
if (bytes != null)
if (opts != null)
return BitmapFactory.decodeByteArray(bytes, 0, bytes.length,opts);
else
return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
return null;
}
public static byte[] readStream(InputStream inStream) throws Exception {
byte[] buffer = new byte[1024];
int len = -1;
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
while ((len = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
byte[] data = outStream.toByteArray();
outStream.close();
inStream.close();
return data;
}
}
[2] BIG5转UTF-八与UTF-8转BIG5
来源: 互联网 发布时间: 2014-02-18
BIG5转UTF-8与UTF-8转BIG5
自定义BIG5转UTF-8
public String big52unicode(String strBIG5) { String strReturn=""; try { strReturn = new String(strBIG5.getBytes("big5"), "UTF-8"); } catch (Exception e) { e.printStackTrace(); } return strReturn; }
自定义UTF-8转BIG5
public String unicode2big5(String strUTF8) { String strReturn=""; try { strReturn = new String(strUTF8.getBytes("UTF-8"), "big5"); } catch (Exception e) { e.printStackTrace(); } return strReturn; } }
[3] 第三章:任务管理器在运行的程序
来源: 互联网 发布时间: 2014-02-18
第三章:任务管理器正在运行的程序
效果:
mian.xnl
simple_list_item.xml
AndroidManifest.xml
strings.xml
效果:
mian.xnl
<?xml version="1.0" encoding="utf-8"?> <AbsoluteLayout android:id="@+id/widget0" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" > <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="取得系统正在运行的程序" android:layout_x="78px" android:layout_y="370px" > </Button> <TextView android:id="@+id/texts" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="取得系统正在运行的程序" android:layout_x="11px" android:layout_y="10px" > </TextView> <ListView android:id="@+id/show" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_x="11px" android:layout_y="37px" > </ListView> </AbsoluteLayout>
simple_list_item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:id="@+id/widget0" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" > <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" > </TextView> </LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="runing.task.info.test" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".RuningTaskInfoTest" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-permission android:name="android.permission.GET_TASKS"></uses-permission> </manifest>
strings.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, RuningTaskInfoTest!</string> <string name="app_name">RuningTaskInfoTest</string> <string name="nulls">没有任务运行!</string> <string name="permission">没有权限!</string> </resources>
package runing.task.info.test; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.app.ActivityManager; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ListView; import android.widget.Toast; public class RuningTaskInfoTest extends Activity { private Button mButton1; private ListView mListView01; private ArrayAdapter<String> aryAdapter1; private ArrayList<String> arylistTask; private int inGetTastCounter=30; private ActivityManager mActivityManager; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /** 载入main.xml*/ setContentView(R.layout.main); /**通过id找到Button组件*/ mButton1=(Button)findViewById(R.id.button); mListView01=(ListView)findViewById(R.id.show); /**设置按钮点击事件*/ mButton1.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View v) { try{ /** ActivityManager 对象向系统取得ACTIVITY_SERVICE*/ mActivityManager=(ActivityManager)RuningTaskInfoTest.this.getSystemService(ACTIVITY_SERVICE); arylistTask=new ArrayList<String>(); /** 以getRunningTasks方法取得任务名称与ID*/ List<ActivityManager.RunningTaskInfo> mRunningTasks=mActivityManager.getRunningTasks(inGetTastCounter); int i=1; for(ActivityManager.RunningTaskInfo amTask:mRunningTasks){ /** 将取得的任务名字添加到list集合里*/ arylistTask.add(""+(i++)+":"+amTask.baseActivity.getClassName()+"(ID"+amTask.id+")"); } aryAdapter1=new ArrayAdapter<String>(RuningTaskInfoTest.this, R.layout.simple_list_item, R.id.text, arylistTask); if(aryAdapter1.getCount()==0){ /**当没有任何任务运行,则提示细信息*/ mMakeTextToast(getResources().getText(R.string.nulls).toString(),true); }else{ /**将后台运行的信息以ListView条列呈现*/ mListView01.setAdapter(aryAdapter1); } }catch (SecurityException e) { e.printStackTrace(); /**当无GET_TASKS权限时提示信息*/ mMakeTextToast(getResources().getText(R.string.permission).toString(),true); } } }); /**当User在运行任务选择的事件处理*/ mListView01.setOnItemSelectedListener(new ListView.OnItemSelectedListener(){ @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { /**由于将运行任务以数组的方式存放,以ID取出数组元素名*/ mMakeTextToast(arylistTask.get(arg2).toString(),false); } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } }); /**当User在运行任务单击的事件处理*/ mListView01.setOnItemClickListener(new ListView.OnItemClickListener(){ @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { /**由于将运行任务以数组的方式存放,以ID取出数组元素名*/ mMakeTextToast(arylistTask.get(arg2).toString(),false); } }); } public void mMakeTextToast(String str,boolean isLong){ if(isLong==true){ Toast.makeText(RuningTaskInfoTest.this, str, Toast.LENGTH_LONG).show(); }else{ Toast.makeText(RuningTaskInfoTest.this, str, Toast.LENGTH_SHORT).show(); } } }
最新技术文章: