package com.example.grayalphatranstest;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import java.io.File; 
import java.util.ArrayList; 
import java.util.Iterator; 
public class MainActivity extends Activity {

    private static final String TAG = "GrayAlphaTest";
    private ArrayList nameList = new ArrayList();
    int nameNum = 0;
   // private static String dirName = "D:";
    private String pngFilePath = Environment.getExternalStorageDirectory().getAbsolutePath()+"/srx2/";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //setContentView(new SampleView(this));
  //      readFileByDirect();
        Log.v(TAG,"oncreate==");
        getPngFileList(pngFilePath);
       // getNameList();
        readFileByDirect();
    }
    
    private void getPngFileList(String FileName) {
        File parentF = new File(FileName);
        String fileTest = parentF.getAbsoluteFile().getPath();
        if (!parentF.exists()) {
            Log.v(TAG,"file not exits");
            return;
        }
        if (parentF.isFile()) {//>10K file 
            //if (parentF.isFile() && (parentF.length()>10240)) {//>10K file     
            Log.v(TAG,"fileTestPath=="+fileTest);
            //nameList.add(parentF.getAbsoluteFile());//
        //    if(parentF.length()>10240) {
                nameList.add(fileTest);
        //    }
            return;
        }
        
        String[] subFiles = parentF.list();
        for (int i = 0; i < subFiles.length; i++) {
            getPngFileList(fileTest + "/" + subFiles[i]);
        }
    }

    public ArrayList getNameList() {
        //ArrayList mList = getNameList();
        Iterator it1 = nameList.iterator();
        while (it1.hasNext()) {
            Log.v(TAG,"element="+it1.next());
        }
        return nameList;
    }

    private void readFileByDirect() {
        //ArrayList mList = getNameList();
        Iterator it1 = nameList.iterator();
        String allFilePath = "";
        
        while (it1.hasNext()) {
            allFilePath = it1.next().toString();
            if (allFilePath != null) {
                findARGBFile(allFilePath);
            }
        }
    }

    private void findARGBFile(String filePath) {
        byte[] b = new byte[1];
        byte[] b1 = new byte[30];
        String fileoutPath = Environment.getExternalStorageDirectory()
                .getAbsolutePath() + "/srxout/";
        try {

            FileInputStream fin = new FileInputStream(filePath);
            // int len = fin.read(b,0,4);
            int inputNum = fin.available();
            byte[] outBuffer = new byte[inputNum];
            for (int i = 0; i < 26; i++) {
                fin.read(b);
            }
            // fin.skip(20);
            // fin.read(b);
            // if (b.equals("-119 80 78 71")) {
            // Log.v(TAG, "This is png...");
            // fin.read(b1, 25, 1);
            // int mm = (int)b1[26];
            int bb = (int) b[0];
            if (bb == 6) {
                fin.close();
                Log.v(TAG, "this is ARGB png");
                FileInputStream finargb = new FileInputStream(filePath);
                finargb.read(outBuffer);
                // finargb.
                // int len = fin.read(b,0,4);
                // int inputNumargb = finargb.available();
                String houzhui = seperateName(filePath);
            /*    if (houzhui.endsWith(".9.png")) {// 9png返回
                    finargb.close();
                    return;
                }*/
                String fileoutPath1 = fileoutPath + seperateName(filePath);
                if (judgeFileNameExit(fileoutPath1)) {
                    // seperateName(filePath);
                    fileoutPath1 = fileoutPath + changeSameName(filePath);
                }
                // if(fileoutPath.)
                // fileoutPath += "/" + nameNum + ".png";
                Log.v(TAG, "fileoutpath==" + fileoutPath1);
                FileOutputStream fout = new FileOutputStream(fileoutPath1);
                // if()
                // for (int i = 1; i <= inputNum; i++) {
                fout.write(outBuffer, 0, inputNum);
                fout.close();
                finargb.close();
            } else {
                fin.close();
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    private String seperateName(String filePath) {
        //String filePath = "c:\\image\\product\\001.png";
        String[] aa = filePath.split("/");
        String name = aa[aa.length - 1];
        Log.v(TAG,"realname ="+name);
        return name;
    }
    
    
    private String changeSameName(String filePath){
        String name = seperateName(filePath);
        String[] aa = name.split(".png");
        String nameout = aa[0]+"_" + nameNum + ".png";
        return nameout;
    }
    private boolean judgeFileNameExit(String path){
        File file = new File(path);
        if(file.exists()){
            nameNum ++;
            return true;
        }
        return false;
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    

1.过滤.9.png

2.过滤大小<10K

 

 

读取一个文件的第x个字节

        private void readSpecialByte(String filePath) {
            try {

                FileInputStream fin = new FileInputStream(filePath);
                fin.skip(25);
                int by = fin.read();
                Log.v(TAG, "by=" + by);
                fin.close();
            } catch (Exception e) {
            }
        }

 

 

posted on 2014-04-02 10:13  snowdrop  阅读(1677)  评论(0编辑  收藏  举报