com.danielhalupka.imageviewer.ImageViewer.java Source code

Java tutorial

Introduction

Here is the source code for com.danielhalupka.imageviewer.ImageViewer.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.danielhalupka.imageviewer;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 *
 * @author Dax
 */
public class ImageViewer extends CordovaPlugin {

    public static final String ACTION_VIEW_IMAGE = "viewImageFile";

    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        try {
            if (ACTION_VIEW_IMAGE.equals(action)) {
                JSONObject arg_object = args.getJSONObject(0);
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_VIEW);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                String filePath = arg_object.getString("filepath");
                intent.setDataAndType(Uri.parse(filePath), "image/*");
                this.cordova.getActivity().startActivity(intent);
                callbackContext.success();
                return true;
            }
            callbackContext.error("Invalid action");
            return false;
        } catch (JSONException e) {
            System.err.println("Exception: " + e.getMessage());
            callbackContext.error(e.getMessage());
            return false;
        }
    }
}