Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: LGPL 

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

import android.content.Context;

import android.os.Environment;

public class Main {
    public static int fileSize = 0;

    public static String makeFile(Context context, String filePath) {
        String fileName;
        if (!filePath.startsWith("/")) {
            if (filePath.contains("/")) {
                fileName = filePath.substring(filePath.lastIndexOf("/") + 1);
            } else {
                fileName = filePath;
            }
            return assetToSd(context, filePath, fileName);
        }
        return null;
    }

    public static String assetToSd(Context ctx, String inFileName, String outFileName) {

        try {
            InputStream is = ctx.getAssets().open(inFileName);
            if (is == null) {
                return null;
            }

            if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
                String sd = Environment.getExternalStorageDirectory().getAbsolutePath();
                String path = sd + "/uexTencent/";
                File filePath = new File(path);
                if (!filePath.exists()) {
                    filePath.mkdirs();
                }
                String fileName = path + outFileName;
                FileOutputStream fos = new FileOutputStream(fileName);

                byte[] buf = new byte[1024];
                int len = 0;
                int total = 0;
                while ((len = is.read(buf)) != -1) {
                    fos.write(buf, 0, len);
                    total++;
                }
                is.close();
                fos.close();
                fileSize = total;
                return fileName;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}