package com.lmy;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
//将图片或者其他文件写入.dat文件中
public class BtimapAndDat {
private InputStream in;//输入流
private OutputStream out;//输出流
public BtimapAndDat(){
}
//图片----->输入流----->字节---->文件输出流(对应文件)
/*new File(Environment.getExternalStorageDirectory().toString()
+ "/360/11.png")
*/
/*
* fromFile 被读的文件 其他文件
* toFile 被写的文件 .dat文件
*/
public void fileToDat(File fromFile,File toFile){
try {
in = new FileInputStream(fromFile);
out=new FileOutputStream(toFile);
int num = -1;
byte[] bytes = new byte[4096];
while((num = in.read(bytes)) != -1) {
out.write(bytes, 0, num);
out.flush();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//输入流----->字节---->文件输出流(对应文件)
//in = getAssets().open("b/v0.dat") in = getAssets().open("b/12.png");;
/**
* inStream 输入流 其他文件
* toFile 被写的文件 .dat文件
*/
public void inToDat(InputStream inStream,File toFile){
try {
out=new FileOutputStream(toFile);
int num = -1;
byte[] bytes = new byte[4096];
while((num = inStream.read(bytes)) != -1) {
out.write(bytes, 0, num);
out.flush();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//将.dat文件中的文件读出
//.dat文件----》输入流----》字节---》Bitmap、Drawable(输入流转化成字节的时候要按图片的原始字节进行拆分)
public static Bitmap datToBitmap(Context ctx, int pngIndex){
InputStream in = null;
try {
in = ctx.getAssets().open("cp/v.dat");
in.skip(PNG_INDEXS[pngIndex - 1]);
byte[] bytes = new byte[PNG_INDEXS[pngIndex] - PNG_INDEXS[pngIndex - 1]];
in.read(bytes, 0, bytes.length);
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
return bitmap;
}catch (OutOfMemoryError e) {
if(true) {
e.printStackTrace();
}
System.gc();
return null;
}
catch (IOException e) {
return null;
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
if(true) {
e.printStackTrace();
}
}
}
}
}
//将Bitmap保存为png或者jpg格式的文件,地址为
public void saveMyBitmap(String bitName,Bitmap bitmap) throws IOException {
File f = new File("/sdcard/android/" + bitName + ".png");
f.createNewFile();
FileOutputStream fOut = null;
try {
fOut = new FileOutputStream(f);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//将图片写入文件的输出流
bitmap.compress(Bitmap.CompressFormat.PNG, 0, fOut);
try {
fOut.flush();//确保全部写入
} catch (IOException e) {
e.printStackTrace();
}
try {
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private static final int[] PNG_INDEXS = {0, 3023, 4096, 5153, 8702, 12477, 18198, 24041, 29714, 35228, 39604, 42726, 45935, 48945, 54797, 60456, 61611, 62657, 63989, 64590, 65173, 66230, 67270, 68349, 69501, 70574, 71631};
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
//将图片或者其他文件写入.dat文件中
public class BtimapAndDat {
private InputStream in;//输入流
private OutputStream out;//输出流
public BtimapAndDat(){
}
//图片----->输入流----->字节---->文件输出流(对应文件)
/*new File(Environment.getExternalStorageDirectory().toString()
+ "/360/11.png")
*/
/*
* fromFile 被读的文件 其他文件
* toFile 被写的文件 .dat文件
*/
public void fileToDat(File fromFile,File toFile){
try {
in = new FileInputStream(fromFile);
out=new FileOutputStream(toFile);
int num = -1;
byte[] bytes = new byte[4096];
while((num = in.read(bytes)) != -1) {
out.write(bytes, 0, num);
out.flush();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//输入流----->字节---->文件输出流(对应文件)
//in = getAssets().open("b/v0.dat") in = getAssets().open("b/12.png");;
/**
* inStream 输入流 其他文件
* toFile 被写的文件 .dat文件
*/
public void inToDat(InputStream inStream,File toFile){
try {
out=new FileOutputStream(toFile);
int num = -1;
byte[] bytes = new byte[4096];
while((num = inStream.read(bytes)) != -1) {
out.write(bytes, 0, num);
out.flush();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//将.dat文件中的文件读出
//.dat文件----》输入流----》字节---》Bitmap、Drawable(输入流转化成字节的时候要按图片的原始字节进行拆分)
public static Bitmap datToBitmap(Context ctx, int pngIndex){
InputStream in = null;
try {
in = ctx.getAssets().open("cp/v.dat");
in.skip(PNG_INDEXS[pngIndex - 1]);
byte[] bytes = new byte[PNG_INDEXS[pngIndex] - PNG_INDEXS[pngIndex - 1]];
in.read(bytes, 0, bytes.length);
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
return bitmap;
}catch (OutOfMemoryError e) {
if(true) {
e.printStackTrace();
}
System.gc();
return null;
}
catch (IOException e) {
return null;
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
if(true) {
e.printStackTrace();
}
}
}
}
}
//将Bitmap保存为png或者jpg格式的文件,地址为
public void saveMyBitmap(String bitName,Bitmap bitmap) throws IOException {
File f = new File("/sdcard/android/" + bitName + ".png");
f.createNewFile();
FileOutputStream fOut = null;
try {
fOut = new FileOutputStream(f);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//将图片写入文件的输出流
bitmap.compress(Bitmap.CompressFormat.PNG, 0, fOut);
try {
fOut.flush();//确保全部写入
} catch (IOException e) {
e.printStackTrace();
}
try {
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private static final int[] PNG_INDEXS = {0, 3023, 4096, 5153, 8702, 12477, 18198, 24041, 29714, 35228, 39604, 42726, 45935, 48945, 54797, 60456, 61611, 62657, 63989, 64590, 65173, 66230, 67270, 68349, 69501, 70574, 71631};
}