可以结合浏览文件的代码让用户可以选文件
/** * 判断sd卡是否存在 Environment.getExternalStorageState() 得到sd卡当前的状态 * * getExternalStorageState() returns MEDIA_MOUNTED if the media is * present and mounted at its mount point with read/write access. 如果返回 * MEDIA_MOUNTED表示外部存储设备存在。并且有读写的权限(因为sd卡有写保护 如果写保护关闭也是没有权限读写的) */ String fileNameString = "espeak-data/example.txt"; int info; if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { if ("".equals(fileNameString)) { info = R.string.sdNotExit; } else { FileInputStream fileInputStream = null; try { /** * Environment.getExternalStorageDirectory(); 得到外存储设备的路径 * */ File file = new File(Environment.getExternalStorageDirectory(), fileNameString); fileInputStream = new FileInputStream(file); String contentString = DataManager.readDate(fileInputStream); mEditText.setText(contentString); } catch (Exception e) { info = R.string.infor; } finally { try { if (fileInputStream != null) { fileInputStream.close(); } } catch (IOException e) { info = R.string.infor; } } } } else { info = R.string.infor; }
package com.tcl.easybuy.utils;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class PictureToStreamTransfer
{
/**
* 生成Byte流
* TODO
* @history
* @knownBugs
* @param
* @return
* @exception
*/
public static byte[] getBytesFromFile(File file){
byte[] ret=null;
try{
if (file==null){
// log.error("helper:the file is null!");
return null;
}
FileInputStream in=new FileInputStream(file);
ByteArrayOutputStream out=new ByteArrayOutputStream(4096);
byte[] b=new byte[4096];
int n;
while ((n= in.read(b))!=-1){
out.write(b,0, n);
}
in.close();
out.close();
ret= out.toByteArray();
}catch (IOException e){
// log.error("helper:get bytes from file process error!");
e.printStackTrace();
}
return ret;
}
/**
* 把流生成图片
* TODO
* @history
* @knownBugs
* @param
* @return
* @exception
*/
public static File getFileFromBytes(byte[] b, String outputFile){
File ret=null;
BufferedOutputStream stream=null;
try{
ret=new File(outputFile);
FileOutputStream fstream=new FileOutputStream(ret);
stream=new BufferedOutputStream(fstream);
stream.write(b);
}catch (Exception e){
// log.error("helper:get file from byte process error!");
e.printStackTrace();
}finally{
if (stream!=null){
try{
stream.close();
}catch (IOException e){
// log.error("helper:get file from byte process error!");
e.printStackTrace();
}
}
}
return ret;
}
/**
*@param args
*@throws IOException*/
public static void main(String[] args)throws IOException{
// int byteread = 0;
// File file = new File("2.GIF");
// InputStream is = new FileInputStream(file);
// byte[] buffer = new byte[1024];
// while ((byteread = is.read(buffer)) != -1) {//一次读1444个字节放入buffer数组中
// System.out.println(is.read(buffer));
// }
File file=new File("2.GIF");
byte[] b= getBytesFromFile(file);
getFileFromBytes(b,"22.jpg");
}
}
在Android手机界面最上方有一条显示时间,信号强度和电池状态等信息的区域,这是Android手机的状态栏Status Bar,当系统有一些重要的信息要通知手机用户时,例如收到新短信,或者是收到新邮件,或者是有未接电话等等,系统通常会把信息显示在状态栏中,有的仅显 示小图片,有的则显示文字和小图片,用手指按住状态栏往下拉,还可以展开状态栏,查看所有系统发出的信息。
在本范例中,要如何把提示信息放入状态栏,又要如何显示小图标呢??Android API为了管理提示信息,定义了一个NotificationManager(Android.app.NotificationManager),只要 将Notification添加到NotificationManager,即可将信息显示在状态栏中。
本范例将模拟添加MSN在线状态的切换,在切换在线状态的同时,改变状态栏上显示的在线状态小图标,并以文字提示目前的状态为何。
test.java
package com.allove;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
public class Test extends Activity {
private Spinner mSpinner;
private NotificationManager mNotificationManager;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mNotificationManager = (NotificationManager) this
.getSystemService(Service.NOTIFICATION_SERVICE);
mSpinner = (Spinner) this.findViewById(R.id.Spinner01);
mSpinner.setAdapter(new ArrayAdapter<String>(Test.this,
android.R.layout.simple_spinner_item, getResources()
.getStringArray(R.array.spiner_string)));
mSpinner
.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
switch (arg2) {
case 0:
setNotiType(R.drawable.msn, "在线");
break;
case 1:
setNotiType(R.drawable.away, "离开");
break;
case 2:
setNotiType(R.drawable.busy, "忙碌");
break;
case 3:
setNotiType(R.drawable.min, "马上回来");
break;
case 4:
setNotiType(R.drawable.offine, "离线");
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
protected void setNotiType(int iconId, String s) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(this, Test2.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent appIntent = PendingIntent.getActivity(this, 0, intent, 0);
Notification myNoti = new Notification();
myNoti.icon = iconId;
myNoti.tickerText = s;
myNoti.defaults = Notification.DEFAULT_SOUND;
myNoti.setLatestEventInfo(this, "MSN登陆状态", s, appIntent);
mNotificationManager.notify(0, myNoti);
}