【Android】图片/文件的处理方法

30 篇文章 0 订阅


    public static Bitmap rotateBitmap(Bitmap bmp, float degree) {
        Matrix matrix = new Matrix();
        matrix.postRotate(degree);  //旋转
        matrix.postScale(-1, 1);  //镜像水平翻转
        return Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
    }

    private void copyBigDataToSD(String strOutFileName) throws IOException
    {
        File fData = new File(strOutFileName);
        try{
            if (!fData.exists()){   //not exist, copy
                InputStream myInput;
                OutputStream myOutput = new FileOutputStream(strOutFileName);
                myInput = this.getAssets().open("data.bin");
                byte[] buffer = new byte[1024];
                int length = myInput.read(buffer);
                while(length > 0)
                {
                    myOutput.write(buffer, 0, length);
                    length = myInput.read(buffer);
                }

                myOutput.flush();
                myInput.close();
                myOutput.close();
            }
        }catch (Exception e ){
            Log.e(TAG, "file data.bin ERROR!");
        }
    }

    private void copyBigDataToSD1(String strOutFileName) throws IOException
    {
        File fData = new File(strOutFileName);
        try{
            if (!fData.exists()){   //not exist, copy
                InputStream myInput;
                OutputStream myOutput = new FileOutputStream(strOutFileName);
                myInput = this.getAssets().open("data1.bin");
                byte[] buffer = new byte[1024];
                int length = myInput.read(buffer);
                while(length > 0)
                {
                    myOutput.write(buffer, 0, length);
                    length = myInput.read(buffer);
                }

                myOutput.flush();
                myInput.close();
                myOutput.close();
            }
        }catch (Exception e ){
            Log.e(TAG, "file data1.bin ERROR!");
        }
    }

    //创建文件夹
    private void CreateFolder(String foldername) throws IOException{
        try{
            if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
                File sd = Environment.getExternalStorageDirectory();
                String path = sd.getPath() + "/App/" + foldername;
                File file = new File(path);
                if (!file.exists()){
                    file.mkdir();
                }
            }
        }catch (Exception e ){
            Log.e(TAG, "create folder ERROR!");
        }
    }

    public void saveBitmap(Bitmap bm, String dirPath, String picName) {
        File f = new File( dirPath , picName);
        if (f.exists()) {
            f.delete();
        }
        try {
            FileOutputStream out = new FileOutputStream(f);
            bm.compress(Bitmap.CompressFormat.PNG, 90, out);
            out.flush();
            out.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    //length用户要求产生字符串的长度
    public static String getRandomString(int length){
        String str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random=new Random();
        StringBuffer sb=new StringBuffer();
        for(int i=0;i<length;i++){
            int number=random.nextInt(62);
            sb.append(str.charAt(number));
        }
        return sb.toString();
    }

    public static Bitmap getScaledBitmap(Bitmap m_img, int sx, int sy) {
        Matrix matrix = new Matrix();
        int imgW = m_img.getWidth();
        int imgH = m_img.getHeight();

        float fx = (float)imgW / sx;
        float fy = (float)imgH / sy;
        matrix.postScale(fx, fy);
        Bitmap rst = Bitmap.createBitmap(m_img, 0, 0, m_img.getWidth(), m_img.getHeight(), matrix, true);
        return rst;
    }

    // 获取当前目录下所有的mp4文件
    public static Vector<String> getJPGFileName(String fileAbsolutePath) {
        Vector<String> vecFile = new Vector<String>();
        File file = new File(fileAbsolutePath);
        File[] subFile = file.listFiles();

        for (int iFileLength = 0; iFileLength < subFile.length; iFileLength++) {
            // 判断是否为文件夹
            if (!subFile[iFileLength].isDirectory()) {
                String filename = subFile[iFileLength].getName();
                // 判断是否为JPG结尾
                if (filename.trim().toLowerCase().endsWith(".jpg")) {
                    vecFile.add(filename);
                }
            }
        }
        return vecFile;
    }

    private Bitmap getDiskBitmap(String pathString) {
        Bitmap bitmap = null;
        try {
            File file = new File(pathString);
            if (file.exists()) {
                bitmap = BitmapFactory.decodeFile(pathString);
            }
        } catch (Exception e) {
            // TODO: handle exception
        }
        return bitmap;
    }


// 获取当前目录下所有的jpg文件
    public static Vector<String> getJPGFileName(String fileAbsolutePath, Boolean isFolder) {
        Vector<String> vecFile = new Vector<String>();
        File file = new File(fileAbsolutePath);
        File[] subFile = file.listFiles();

        for (int iFileLength = 0; iFileLength < subFile.length; iFileLength++) {
            if ( !isFolder ){
                // 判断是否为文件夹
                if (!subFile[iFileLength].isDirectory()) {
                    String filename = subFile[iFileLength].getName();
                    // 判断是否为JPG结尾
                    if (filename.trim().toLowerCase().endsWith(".jpg")) {
                        vecFile.add(filename);
                    }
                }
            }else{
                if (subFile[iFileLength].isDirectory()) {
                    String filename = subFile[iFileLength].getName();
                    vecFile.add(filename);
                }
            }

        }
        return vecFile;
    }



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值