Android Open Source - u2020-v2 I O Utils






From Project

Back to project page u2020-v2.

License

The source code is released under:

Apache License

If you think the Android project u2020-v2 listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.gabor.common.io;
//from w  ww  . j  av a2s  . c om
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class IOUtils {

  // well, this one is not am IOUtil :)
    public static String getDate(long val) {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(val);
        Date d = c.getTime();
        SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
        return format.format(d);
    }

    public static void openPdf(InputStream is, String name, Context context) {
        if (! isExternalStorageWritable()) {
            Toast.makeText(context, "Neither SD card nor non-removable external storage is available", Toast.LENGTH_SHORT).show();
            return;
        }
        File dir;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            File dirs[] = context.getExternalCacheDirs();// try the sdcard
            dir = (dirs[0].exists() && dirs[0].canWrite()) ? dirs[0] : dirs[1];
        } else
            dir = context.getExternalCacheDir();

        Uri uri = getUri(is, dir, name);

        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(uri, "application/pdf");
        intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
        try {
            context.startActivity(intent);
        } catch (ActivityNotFoundException e) {
            Toast.makeText(context, e.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }

    // writes a stream to a file and gets the uri of the result
    @SuppressWarnings("EmptyCatchBlock")
    private static Uri getUri(InputStream is, File dest, String name) {
        File file = new File(dest, name);
        OutputStream out = null;
        try {
            out = new BufferedOutputStream(new FileOutputStream(file));
            byte[] buffer = new byte[8192];
            int length;
            while ((length = is.read(buffer)) > 0)
                out.write(buffer, 0, length);
        } catch (IOException e) {
            Log.d("getUri", e.getMessage());
        } finally {
            try { is.close(); } catch (Exception e) {}
            if (out != null) try { out.close(); } catch (Exception e) {}
        }
        return Uri.fromFile(file);
    }

    // Checks if external storage is available for read and write
    private static boolean isExternalStorageWritable() {
        String state = Environment.getExternalStorageState();
        return Environment.MEDIA_MOUNTED.equals(state);
    }
}




Java Source Code List

com.gabor.common.io.IOUtils.java
com.gabor.database.CarT.java
com.gabor.database.Car.java
com.gabor.database.Car_.java
com.gabor.database.DbHelper.java
com.gabor.database.ImagesT.java
com.gabor.recyclerview.CustomAdapter.java
com.gabor.recyclerview.MainActivityTest.java
com.gabor.recyclerview.MainActivity.java
com.gabor.recyclerview.RecyclerViewFragment.java