Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * Copyright (c) 2015 Samsung Electronics, Co. Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.File;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import android.content.Context;

import android.content.res.AssetManager;
import android.net.Uri;

import android.util.Log;

public class Main {
    private static final String STORAGE_FOLDER = "/localnotification";
    private static final String TAG = "RichNotificationHelper";

    private static Uri getUriFromAsset(Context mContext, String path) {
        File dir = mContext.getExternalCacheDir();

        if (dir == null) {
            Log.e(TAG, "Missing external cache dir");
            return Uri.EMPTY;
        }
        String resPath = path.replaceFirst("file:/", "www");
        String fileName = resPath.substring(resPath.lastIndexOf('/') + 1);
        String storage = dir.toString() + STORAGE_FOLDER;

        if (fileName == null || fileName.isEmpty()) {
            Log.e(TAG, "Filename is missing");
            return Uri.EMPTY;
        }

        File file = new File(storage, fileName);
        FileOutputStream outStream = null;
        InputStream inputStream = null;

        try {
            File fileStorage = new File(storage);
            if (!fileStorage.mkdir())
                Log.e(TAG, "Storage directory could not be created: " + storage);

            AssetManager assets = mContext.getAssets();
            outStream = new FileOutputStream(file);
            inputStream = assets.open(resPath);

            copyFile(inputStream, outStream);
            outStream.flush();
            outStream.close();
            return Uri.fromFile(file);
        } catch (FileNotFoundException e) {
            Log.e(TAG, "File not found: assets/" + resPath);
        } catch (IOException ioe) {
            Log.e(TAG, "IOException occured");
        } catch (SecurityException secEx) {
            Log.e(TAG, "SecurityException: directory creation denied");
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
                if (outStream != null) {
                    outStream.flush();
                    outStream.close();
                }
            } catch (IOException ioe) {
                Log.e(TAG, "IOException occured while closing/flushing streams");
            }
        }
        return Uri.EMPTY;
    }

    private static void copyFile(InputStream in, OutputStream out) throws IOException {
        byte[] buffer = new byte[1024];
        int read;

        while ((read = in.read(buffer)) != -1) {
            out.write(buffer, 0, read);
        }
    }
}