Java 无法使用自定义Cordova Apache插件在本地打开PDF文件

Java 无法使用自定义Cordova Apache插件在本地打开PDF文件,java,android,cordova,cordova-plugins,Java,Android,Cordova,Cordova Plugins,我尝试创建一个Cordova插件,在混合应用程序中打开PDF文件,下面是一些示例。在Android模拟器上运行应用程序时,我遇到了这个错误 日志: [2015-10-25 20:43:10 - CordTestProjectCordTestAndroid] Conversion to Dalvik format failed with error 1 [2015-10-25 20:58:56 - CordTestProjectCordTestAndroid] Dx Uncaught transl

我尝试创建一个Cordova插件,在混合应用程序中打开PDF文件,下面是一些示例。在Android模拟器上运行应用程序时,我遇到了这个错误

日志:

[2015-10-25 20:43:10 - CordTestProjectCordTestAndroid] Conversion to Dalvik format failed with error 1
[2015-10-25 20:58:56 - CordTestProjectCordTestAndroid] Dx Uncaught translation error: java.util.concurrent.ExecutionException: java.lang.OutOfMemoryError: GC overhead limit exceeded
[2015-10-25 20:58:56 - CordTestProjectCordTestAndroid] Dx Uncaught translation error: java.util.concurrent.ExecutionException: java.lang.OutOfMemoryError: GC overhead limit exceeded
`enter code here`[2015-10-25 20:58:56 - CordTestProjectCordTestAndroid] Dx Uncaught translation error: java.util.concurrent.ExecutionException: java.lang.OutOfMemoryError: GC overhead limit exceeded
[2015-10-25 20:58:56 - CordTestProjectCordTestAndroid] Dx 3 errors; aborting
[2015-10-25 20:58:56 - CordTestProjectCordTestAndroid] Conversion to Dalvik format failed with error 1
代码如下:

config.xml:

 <feature name="FileOpenerPlugin">
    <param name="android-package" value="FileOpenerPlugin" />
</feature>
FileOpenerPlugin.Java:

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

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

public class FileOpenerPlugin extends CordovaPlugin {

@Override
public boolean execute(String action, JSONArray args,
                CallbackContext callbackContext) throws JSONException {

            PluginResult.Status status = PluginResult.Status.OK;
        String result = "";

        try {
            if (action.equals("openFile")) {
                openFile(args.getString(0));
            }
            else {
                status = PluginResult.Status.INVALID_ACTION;
            }

            return true;
        } catch (JSONException e) {
            return false;
        } catch (IOException e) {
            return false;
        }
}

private void openFile(String url) throws IOException {
    // Create URI
    Uri uri = Uri.parse(url);

    Intent intent = null;
    // Check what kind of file you are trying to open, by comparing the url with extensions.
    // When the if condition is matched, plugin sets the correct intent (mime) type,
    // so Android knew what application to use to open the file

    if (url.contains(".doc") || url.contains(".docx")) {
        // Word document
        intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(uri, "application/msword");
    } else if(url.contains(".pdf")) {
        // PDF file
        intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(uri, "application/pdf");
    } else if(url.contains(".ppt") || url.contains(".pptx")) {
        // Powerpoint file
        intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(uri, "application/vnd.ms-powerpoint");
    } else if(url.contains(".xls") || url.contains(".xlsx")) {
        // Excel file
        intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(uri, "application/vnd.ms-excel");
    } else if(url.contains(".rtf")) {
        // RTF file
        intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(uri, "application/rtf");
    } else if(url.contains(".wav")) {
        // WAV audio file
        intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(uri, "audio/x-wav");
    } else if(url.contains(".gif")) {
        // GIF file
        intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(uri, "image/gif");
    } else if(url.contains(".jpg") || url.contains(".jpeg")) {
        // JPG file
        intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(uri, "image/jpeg");
    } else if(url.contains(".png")) {
        // PNG file
        intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(uri, "image/png");
    } else if(url.contains(".txt")) {
        // Text file
        intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(uri, "text/plain");
    } else if(url.contains(".mpg") || url.contains(".mpeg") ||    url.contains(".mpe") || url.contains(".mp4") || url.contains(".avi")) {
        // Video files
        intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(uri, "video/*");
    }

    //if you want you can also define the intent type for any other file

    //additionally use else clause below, to manage other unknown extensions
    //in this case, Android will show all applications installed on the device
    //so you can choose which application to use


    else {            intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(uri, "*/*");
    }

    this.cordova.getActivity().startActivity(intent);
  }
}

已弃用。见第12页,共页。请使用a。请跟随。给我们实际的错误信息,并仅从几行代码中生成它;你发布的大部分内容都不是问题的一部分。我使用的是Mobilefirst平台。请问我能得到任何帮助吗?事实上,你使用的是MFP并不意味着它与MFP有关。“提供代码和错误”中没有任何内容表明这一点。我只是想说清楚。
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

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

public class FileOpenerPlugin extends CordovaPlugin {

@Override
public boolean execute(String action, JSONArray args,
                CallbackContext callbackContext) throws JSONException {

            PluginResult.Status status = PluginResult.Status.OK;
        String result = "";

        try {
            if (action.equals("openFile")) {
                openFile(args.getString(0));
            }
            else {
                status = PluginResult.Status.INVALID_ACTION;
            }

            return true;
        } catch (JSONException e) {
            return false;
        } catch (IOException e) {
            return false;
        }
}

private void openFile(String url) throws IOException {
    // Create URI
    Uri uri = Uri.parse(url);

    Intent intent = null;
    // Check what kind of file you are trying to open, by comparing the url with extensions.
    // When the if condition is matched, plugin sets the correct intent (mime) type,
    // so Android knew what application to use to open the file

    if (url.contains(".doc") || url.contains(".docx")) {
        // Word document
        intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(uri, "application/msword");
    } else if(url.contains(".pdf")) {
        // PDF file
        intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(uri, "application/pdf");
    } else if(url.contains(".ppt") || url.contains(".pptx")) {
        // Powerpoint file
        intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(uri, "application/vnd.ms-powerpoint");
    } else if(url.contains(".xls") || url.contains(".xlsx")) {
        // Excel file
        intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(uri, "application/vnd.ms-excel");
    } else if(url.contains(".rtf")) {
        // RTF file
        intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(uri, "application/rtf");
    } else if(url.contains(".wav")) {
        // WAV audio file
        intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(uri, "audio/x-wav");
    } else if(url.contains(".gif")) {
        // GIF file
        intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(uri, "image/gif");
    } else if(url.contains(".jpg") || url.contains(".jpeg")) {
        // JPG file
        intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(uri, "image/jpeg");
    } else if(url.contains(".png")) {
        // PNG file
        intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(uri, "image/png");
    } else if(url.contains(".txt")) {
        // Text file
        intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(uri, "text/plain");
    } else if(url.contains(".mpg") || url.contains(".mpeg") ||    url.contains(".mpe") || url.contains(".mp4") || url.contains(".avi")) {
        // Video files
        intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(uri, "video/*");
    }

    //if you want you can also define the intent type for any other file

    //additionally use else clause below, to manage other unknown extensions
    //in this case, Android will show all applications installed on the device
    //so you can choose which application to use


    else {            intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(uri, "*/*");
    }

    this.cordova.getActivity().startActivity(intent);
  }
}