文件写入手机内部存储中方法,通过输入流的形式写入手机内存中
/**
* 文件写入手机内部存储中方法
* @param fileName 文件名
* @param is 文件存储形式以输入流形式进行存储
* @return boolean 返回一个boolean型,进行判断写入手机是否成功
* 返回true表示成功写入,反之不成功出现异常
* @throws IOException IO异常
*/
public boolean writeFileFromInnerSD(Context context, String fileName, InputStream is)
{
try
{
// 创建文件,使文件可以被其他应用读取
FileOutputStream os = context.openFileOutput(fileName,
Context.MODE_WORLD_READABLE);
byte[] buffer = new byte[1024];
int len = 0;
// 将输入流保存到文件中
while ((len = is.read(buffer)) != -1)
{
os.write(buffer, 0, len);
}
// 进行IO流的关闭
os.close();
is.close();
return true;
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
/**
* 读取手机内部存储卡中方法
* @Description:通过文件名获得文件中的内容,
* 以输出流的形式返回给用户
* @param fileName 文件名
* @return OutputStream 文件中的内容以输出流的形式返回
* @throws FileNotFoundException IOException
*/
public OutputStream readFileFromInnerSD(Context context, String fileName)
{
// 声明文件输入流
FileInputStream is = null;
try
{
// 得到文件输入流
is = context.openFileInput(fileName);
}
catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
byte[] buffer = new byte[1024];
int len = 0;
OutputStream os = new ByteArrayOutputStream();
try
{
// 将输入流转化为输出流
while ((len = is.read(buffer)) != -1)
{
os.write(buffer, 0, len);
}
os.close();
is.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return os;
}
/**
* 数据写入外部存储SD卡方法
* @Description: 通过文件名创建文件,并将is输入流转化到文件中
* @param fileName 文件名
* @param is 文件内容输入流
* @return boolean 返回一个boolean型,进行判断写入SD卡是否成功
* 返回true表示成功写入,反之不成功为SD卡不存在或出现异常
* @throws FileNotFoundException IOException
*/
public boolean writeFileFromExternalSD(String fileName, InputStream is)
{
try
{
// 判断手机是否插入SD卡,并检查应用程序是否具有访问SD的权限
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{
// 获取SD卡对应的存储目录名
String sdCardDir = Environment.getExternalStorageDirectory().getName();
// 创建文件
File targetFile = new File(sdCardDir+"/"+fileName);
// 以指定文件创建RandomAccessFile对象,并具有读写功能
RandomAccessFile raf = new RandomAccessFile(targetFile, "rw");
// 将文件记录指针移到到最后
raf.seek(targetFile.length());
byte[] buffer = new byte[1024];
int len = 0;
// 将输入流中的数据写入文件
while ((len = is.read(buffer)) != -1)
{
raf.write(buffer, 0, len);
}
is.close();
raf.close();
m_Boolean = true;
}
// SD卡不存在返回false
else
{
m_Boolean = false;
}
}
catch (FileNotFoundException e)
{
e.printStackTrace();
m_Boolean = false;
}
catch (IOException e)
{
e.printStackTrace();
m_Boolean = false;
}
return m_Boolean;
}
/**
* 从外部存储SD卡中的读取方法
* @Description: 传入文件名,返回文件的一个输出流,用户需要自行转化为相应的类型
* @param fileName 文件名
* @return OutputStream 文件中的内容以输出流的形式返回
* @throws IOException FileNotFoundException
*/
public OutputStream readFileFromExternalSD(String fileName)
{
// 定义一个文件输入流
FileInputStream fis = null;
// 初始化一个字符数组输出流
OutputStream os = new ByteArrayOutputStream();
try
{
// 判断手机是否插入SD卡,并检查应用程序是否具有访问SD的权限
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{
// 获取SD卡对应的文件存储目录
File sdCardDir = Environment.getExternalStorageDirectory();
// 获取指定文件对应的输入流
fis = new FileInputStream(sdCardDir.getCanonicalPath()+"/"+fileName);
// 初始化字节数组
byte[] buffer = new byte[1024];
int len = 0;
try
{
// 将得到的文件进行转化为输出流
while ((len = fis.read(buffer)) != -1)
{
os.write(buffer, 0, len);
}
// 对输出流进行关闭
os.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
// 返回输出流
return os;
}
// 当手机无SD卡是,返回值为空
else
{
return null;
}
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
// 等待垃圾回收机制进行回收
try
{
if (fis !=null)
{
fis.close();
fis = null;
}
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null ;
}
/**
* 读取手机内部存储卡中方法
* @Description:通过文件名获得文件中的内容,
* 以输出流的形式返回给用户
* @param fileName 文件名
* @return OutputStream 文件中的内容以输出流的形式返回
* @throws FileNotFoundException IOException
*/
public OutputStream readFileFromInnerSD(Context context, String fileName)
{
// 声明文件输入流
FileInputStream is = null;
try
{
// 得到文件输入流
is = context.openFileInput(fileName);
}
catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
byte[] buffer = new byte[1024];
int len = 0;
OutputStream os = new ByteArrayOutputStream();
try
{
// 将输入流转化为输出流
while ((len = is.read(buffer)) != -1)
{
os.write(buffer, 0, len);
}
os.close();
is.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return os;
}
/**
* 数据写入外部存储SD卡方法
* @Description: 通过文件名创建文件,并将is输入流转化到文件中
* @param fileName 文件名
* @param is 文件内容输入流
* @return boolean 返回一个boolean型,进行判断写入SD卡是否成功
* 返回true表示成功写入,反之不成功为SD卡不存在或出现异常
* @throws FileNotFoundException IOException
*/
public boolean writeFileFromExternalSD(String fileName, InputStream is)
{
try
{
// 判断手机是否插入SD卡,并检查应用程序是否具有访问SD的权限
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{
// 获取SD卡对应的存储目录名
String sdCardDir = Environment.getExternalStorageDirectory().getName();
// 创建文件
File targetFile = new File(sdCardDir+"/"+fileName);
// 以指定文件创建RandomAccessFile对象,并具有读写功能
RandomAccessFile raf = new RandomAccessFile(targetFile, "rw");
// 将文件记录指针移到到最后
raf.seek(targetFile.length());
byte[] buffer = new byte[1024];
int len = 0;
// 将输入流中的数据写入文件
while ((len = is.read(buffer)) != -1)
{
raf.write(buffer, 0, len);
}
is.close();
raf.close();
m_Boolean = true;
}
// SD卡不存在返回false
else
{
m_Boolean = false;
}
}
catch (FileNotFoundException e)
{
e.printStackTrace();
m_Boolean = false;
}
catch (IOException e)
{
e.printStackTrace();
m_Boolean = false;
}
return m_Boolean;
}
/**
* 从外部存储SD卡中的读取方法
* @Description: 传入文件名,返回文件的一个输出流,用户需要自行转化为相应的类型
* @param fileName 文件名
* @return OutputStream 文件中的内容以输出流的形式返回
* @throws IOException FileNotFoundException
*/
public OutputStream readFileFromExternalSD(String fileName)
{
// 定义一个文件输入流
FileInputStream fis = null;
// 初始化一个字符数组输出流
OutputStream os = new ByteArrayOutputStream();
try
{
// 判断手机是否插入SD卡,并检查应用程序是否具有访问SD的权限
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{
// 获取SD卡对应的文件存储目录
File sdCardDir = Environment.getExternalStorageDirectory();
// 获取指定文件对应的输入流
fis = new FileInputStream(sdCardDir.getCanonicalPath()+"/"+fileName);
// 初始化字节数组
byte[] buffer = new byte[1024];
int len = 0;
try
{
// 将得到的文件进行转化为输出流
while ((len = fis.read(buffer)) != -1)
{
os.write(buffer, 0, len);
}
// 对输出流进行关闭
os.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
// 返回输出流
return os;
}
// 当手机无SD卡是,返回值为空
else
{
return null;
}
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
// 等待垃圾回收机制进行回收
try
{
if (fis !=null)
{
fis.close();
fis = null;
}
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null ;
}
调用方法代码
/**
* 测试SD卡和手机存储操作类
* 测试图片和文字的存储到SD卡和手机中
*/
public class CFileActivity extends Activity
{
private EditText m_FileName = null; // 保存的文件名
private EditText m_FileWriteContent = null; // 保存的文件内容
private EditText m_FileReadContent = null; // 读取的文件内容
private Button m_WritePhone = null; // 写入到手机按钮
private Button m_WriteSDCard = null; // 写入到SD卡内容
private Button m_ReadPhone = null; // 从手机读取按钮
private Button m_ReadSDCard = null; // 从SD卡读取按钮
private ImageView m_Image = null; // 图片
String m_FileTitle; // 文件名
String m_WriteContent; // 写入文件内容
String m_ReadContent; // 读取文件内容
CFileReadWriteUtil m_FileReadWriteUtil; // 文件存储工具类
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_file);
// 控件初始化
findView();
// 取得文本框中的值
getText();
/***********************下面两个方法只能同一个,不能同时使用**********************/
// 实现对文本文件的操作
// setListenerString();
// 实现对图片的操作
setListenerMap();
}
/**
* 控件初始化方法
*/
private void findView()
{
m_FileName = (EditText) findViewById(R.id.ID_ET_FILENAME);
m_FileWriteContent = (EditText) findViewById(R.id.ID_ET_WRITE);
m_FileReadContent = (EditText) findViewById(R.id.ID_ET_READ);
m_WritePhone = (Button) findViewById(R.id.ID_BTN_WRITEPHONE);
m_WriteSDCard = (Button) findViewById(R.id.ID_BTN_WRITESDCARD);
m_ReadPhone = (Button) findViewById(R.id.ID_BTN_READPHONE);
m_ReadSDCard = (Button) findViewById(R.id.ID_BTN_READSDCARD);
m_Image = (ImageView) findViewById(R.id.ID_IMAGE);
m_FileReadWriteUtil = new CFileReadWriteUtil();
}
/**
* 取得文本框中的内容
*/
private void getText()
{
// 取得文件名
m_FileTitle = m_FileName.getText().toString().trim();
// 取得输入的文件内容
m_WriteContent = m_FileWriteContent.getText().toString().trim();
}
/**
* 实现图片操作的控件监听方法
*/
public void setListenerMap()
{
// 写入手机
m_WritePhone.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
// 取得初始值
getText();
// 判断输入的内容是否符合标准
if (m_FileTitle == null || m_FileTitle.equals(""))
{
Toast.makeText(CFileActivity.this, "图片名不能为空!",
Toast.LENGTH_SHORT).show();
}
else
{
// 将资源文件中的图片转化为BitmapDrawable
BitmapDrawable bd = new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.phone));
// 将BitmapDrawable转化为Bitmap
Bitmap bitmap = bd.getBitmap() ;
/*************将Bitmap转化为InputStream*****************/
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
InputStream is = new ByteArrayInputStream(baos.toByteArray());
// 调用方法,返回Boolean型数据
boolean bo = m_FileReadWriteUtil.writeFileFromInnerSD(CFileActivity.this, m_FileTitle, is);
// 根据返回值进行判断操作是否成功
if (bo)
{
Toast.makeText(CFileActivity.this, "写入手机成功",
Toast.LENGTH_SHORT).show();
m_FileName.setText("");
m_FileWriteContent.setText("");
}
else
{
Toast.makeText(CFileActivity.this, "写入手机失败",
Toast.LENGTH_SHORT).show();
}
}
}
});
// 写入SD卡
m_WriteSDCard.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
// 取得初始值
getText();
// 判断输入的内容是否符合标准
if (m_FileTitle == null || m_FileTitle.equals(""))
{
Toast.makeText(CFileActivity.this, "图片名不能为空!",
Toast.LENGTH_SHORT).show();
}
else
{
// 将资源文件中的图片转化为BitmapDrawable
BitmapDrawable bd = new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.sdcard));
// 将BitmapDrawable转化为Bitmap
Bitmap bitmap = bd.getBitmap() ;
/*************将Bitmap转化为InputStream*****************/
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
InputStream is = new ByteArrayInputStream(baos.toByteArray());
// 调用方法,返回Boolean型数据
boolean bo = m_FileReadWriteUtil.writeFileFromExternalSD(m_FileTitle, is);
// 根据返回值进行判断操作是否成功
if (bo)
{
Toast.makeText(CFileActivity.this, "写入SD卡成功",
Toast.LENGTH_SHORT).show();
m_FileName.setText("");
m_FileWriteContent.setText("");
}
else
{
Toast.makeText(CFileActivity.this, "写入SD卡失败",
Toast.LENGTH_SHORT).show();
}
}
}
});
// 从手机中读取
m_ReadPhone.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
// 判断文件是否存在
if (m_FileTitle == null || m_FileTitle.equals(""))
{
Toast.makeText(CFileActivity.this, "文件不存在!",
Toast.LENGTH_SHORT).show();
}
else
{
byte[] b = null;
ByteArrayOutputStream a = null;
try
{
// 调用方法,返回OutputStream型数据,并进行强制为ByteArrayOutputStream
a = (ByteArrayOutputStream)m_FileReadWriteUtil.readFileFromInnerSD(CFileActivity.this, m_FileTitle);
}
catch (Exception e)
{
e.printStackTrace();
}
// 将ByteArrayOutputStream转化为Byte
b = a.toByteArray();
Bitmap bitmap = null;
if(b.length != 0)
{
// 将Byte型转化为Bitmap
bitmap = BitmapFactory.decodeByteArray(b, 0, b.length);
}
// 设置图片
m_Image.setImageBitmap(bitmap);
}
}
});
// 从SD卡中读取
m_ReadSDCard.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
// 根据文件名判断文件是否存在
if (m_FileTitle == null || m_FileTitle.equals(""))
{
Toast.makeText(CFileActivity.this, "文件不存在!",
Toast.LENGTH_SHORT).show();
}
else
{
ByteArrayOutputStream a = null;
byte[] b = null;
// 调用方法,返回OutputStream型数据,并进行强制为ByteArrayOutputStream
a = (ByteArrayOutputStream)m_FileReadWriteUtil.readFileFromExternalSD(m_FileTitle);
// 将ByteArrayOutputStream转化为Byte
b = a.toByteArray();
Bitmap bitmap = null;
if(b.length != 0)
{
// 将Byte型转化为Bitmap
bitmap = BitmapFactory.decodeByteArray(b, 0, b.length);
}
// 设置图片
m_Image.setImageBitmap(bitmap);
}
}
});
}
/**
* 实现文本操作的控件监听方法
*/
public void setListenerString()
{
// 写入手机
m_WritePhone.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
// 取得初始值
getText();
// 判断输入的内容是否符合标准
if (m_FileTitle == null || m_FileTitle.equals("")
|| m_WriteContent == null || m_WriteContent.equals(""))
{
Toast.makeText(CFileActivity.this, "文件名和文件内容都不能为空!",
Toast.LENGTH_SHORT).show();
}
else
{
InputStream is = new ByteArrayInputStream(m_WriteContent.getBytes());
// 调用方法,返回Boolean型数据
boolean bo = m_FileReadWriteUtil.writeFileFromInnerSD(CFileActivity.this, m_FileTitle, is);
// 根据返回值进行判断操作是否成功
if (bo)
{
Toast.makeText(CFileActivity.this, "写入手机成功",
Toast.LENGTH_SHORT).show();
m_FileName.setText("");
m_FileWriteContent.setText("");
}
else
{
Toast.makeText(CFileActivity.this, "写入手机失败",
Toast.LENGTH_SHORT).show();
}
}
}
});
// 写入SD卡
m_WriteSDCard.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
// 取得初始值
getText();
// 判断输入的内容是否符合标准
if (m_FileTitle == null || m_FileTitle.equals("")
|| m_WriteContent == null || m_WriteContent.equals(""))
{
Toast.makeText(CFileActivity.this, "文件名和文件内容都不能为空!",
Toast.LENGTH_SHORT).show();
}
else
{
InputStream is = new ByteArrayInputStream(m_WriteContent.getBytes());
// 调用方法,返回Boolean型数据
boolean bo = m_FileReadWriteUtil.writeFileFromExternalSD(m_FileTitle, is);
// 根据返回值进行判断操作是否成功
if (bo)
{
Toast.makeText(CFileActivity.this, "写入SD卡成功",
Toast.LENGTH_SHORT).show();
m_FileName.setText("");
m_FileWriteContent.setText("");
}
else
{
Toast.makeText(CFileActivity.this, "写入SD卡失败",
Toast.LENGTH_SHORT).show();
}
}
}
});
// 从手机中读取
m_ReadPhone.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
// 判断文件是否存在
if (m_FileTitle == null || m_FileTitle.equals(""))
{
Toast.makeText(CFileActivity.this, "文件不存在!",
Toast.LENGTH_SHORT).show();
}
else
{
byte[] b = null;
ByteArrayOutputStream a = null;
try
{
// 调用方法,返回OutputStream型数据,并进行强制为ByteArrayOutputStream
a = (ByteArrayOutputStream)m_FileReadWriteUtil.readFileFromInnerSD(CFileActivity.this, m_FileTitle);
}
catch (Exception e)
{
e.printStackTrace();
}
// 将ByteArrayOutputStream转化为Byte
b = a.toByteArray();
String data = new String(b);
m_FileReadContent.setText(data);
}
}
});
// 从SD卡中读取
m_ReadSDCard.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
// 根据文件名判断文件是否存在
if (m_FileTitle == null || m_FileTitle.equals(""))
{
Toast.makeText(CFileActivity.this, "文件不存在!",
Toast.LENGTH_SHORT).show();
}
else
{
ByteArrayOutputStream a = null;
byte[] b = null;
// 调用方法,返回OutputStream型数据,并进行强制为ByteArrayOutputStream
a = (ByteArrayOutputStream)m_FileReadWriteUtil.readFileFromExternalSD(m_FileTitle);
// 将ByteArrayOutputStream转化为Byte
b = a.toByteArray();
String data = new String(b);
Log.i("msg-->sd", data + "bfbfadb");
m_FileReadContent.setText(data);
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_file, menu);
return true;
}
}
源码下载地址:http://download.csdn.net/detail/xiaoduirensheng311/5165989