andoird 图片与.dat文件之间的转化

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};
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值