Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 文档丢失-谷歌云打印_Android_Eclipse_Android Studio_Google Cloud Print - Fatal编程技术网

Android 文档丢失-谷歌云打印

Android 文档丢失-谷歌云打印,android,eclipse,android-studio,google-cloud-print,Android,Eclipse,Android Studio,Google Cloud Print,我正试图在android studio上的项目中使用google cloud print打印pdf文件。我将文件存储在项目的assets文件夹中,当我打印它时,会说Document Missing。这是我的java代码: public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstan

我正试图在android studio上的项目中使用google cloud print打印pdf文件。我将文件存储在项目的assets文件夹中,当我打印它时,会说Document Missing。这是我的java代码:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

}

public void onPrintClick(View v) {


    String path = Environment.getExternalStorageDirectory().getAbsolutePath()
            + "/monsoon.pdf";
    File file = new File(path);

    if (file.exists()) {
        Uri uri = Uri.fromFile(file);
        Intent printIntent = new Intent(this, PrintDialogActivity.class);
        printIntent.setDataAndType(uri, "application/pdf");
        printIntent.putExtra("title", "Gaurang");
        startActivity(printIntent);
    } else {
        Toast.makeText(getApplicationContext(), "No file",
                Toast.LENGTH_SHORT).show();
    }
}
}
这是谷歌云打印的Android代码,对PrintDialogJavaScriptInterface类做了一些修改…ie。在JavascriptInterface中应用注释

public class PrintDialogActivity extends Activity {
private static final String PRINT_DIALOG_URL = "https://www.google.com/cloudprint/dialog.html";
private static final String JS_INTERFACE = "AndroidPrintDialog";
private static final String CONTENT_TRANSFER_ENCODING = "base64";

private static final String ZXING_URL = "http://zxing.appspot.com";
private static final int ZXING_SCAN_REQUEST = 65743;

/**
 * Post message that is sent by Print Dialog web page when the printing
 * dialog needs to be closed.
 */
private static final String CLOSE_POST_MESSAGE_NAME = "cp-dialog-on-close";

/**
 * Web view element to show the printing dialog in.
 */
private WebView dialogWebView;

/**
 * Intent that started the action.
 */
Intent cloudPrintIntent;

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);

    setContentView(R.layout.activity_print_dialog);
    dialogWebView = (WebView) findViewById(R.id.webview);
    cloudPrintIntent = this.getIntent();

    WebSettings settings = dialogWebView.getSettings();
    settings.setJavaScriptEnabled(true);

    dialogWebView.setWebViewClient(new PrintDialogWebClient());
    dialogWebView.addJavascriptInterface(
            new PrintDialogJavaScriptInterface(), JS_INTERFACE);

    dialogWebView.loadUrl(PRINT_DIALOG_URL);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if (requestCode == ZXING_SCAN_REQUEST && resultCode == RESULT_OK) {
        dialogWebView.loadUrl(intent.getStringExtra("SCAN_RESULT"));
    }
}

final class PrintDialogJavaScriptInterface {

    @JavascriptInterface
    public String getType() {
        return cloudPrintIntent.getType();
    }

    @JavascriptInterface
    public String getTitle() {
        return cloudPrintIntent.getExtras().getString("title");
    }

    @JavascriptInterface
    public String getContent() {
        try {
            ContentResolver contentResolver = getContentResolver();
            InputStream is = contentResolver
                    .openInputStream(cloudPrintIntent.getData());
            ByteArrayOutputStream baos = new ByteArrayOutputStream();

            byte[] buffer = new byte[4096];
            int n = is.read(buffer);
            while (n >= 0) {
                baos.write(buffer, 0, n);
                n = is.read(buffer);
            }
            is.close();
            baos.flush();

            return Base64
                    .encodeToString(baos.toByteArray(), Base64.DEFAULT);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

    @JavascriptInterface
    public String getEncoding() {
        return CONTENT_TRANSFER_ENCODING;
    }

    @JavascriptInterface
    public void onPostMessage(String message) {
        if (message.startsWith(CLOSE_POST_MESSAGE_NAME)) {
            finish();
        }
    }
}

private final class PrintDialogWebClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url.startsWith(ZXING_URL)) {
            Intent intentScan = new Intent(
                    "com.google.zxing.client.android.SCAN");
            intentScan.putExtra("SCAN_MODE", "QR_CODE_MODE");
            try {
                startActivityForResult(intentScan, ZXING_SCAN_REQUEST);
            } catch (ActivityNotFoundException error) {
                view.loadUrl(url);
            }
        } else {
            view.loadUrl(url);
        }
        return false;
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        if (PRINT_DIALOG_URL.equals(url)) {
            // Submit print document.
            view.loadUrl("javascript:printDialog.setPrintDocument(printDialog.createPrintDocument("
                    + "window."
                    + JS_INTERFACE
                    + ".getType(),window."
                    + JS_INTERFACE
                    + ".getTitle(),"
                    + "window."
                    + JS_INTERFACE
                    + ".getContent(),window."
                    + JS_INTERFACE
                    + ".getEncoding()))");

            // Add post messages listener.
            view.loadUrl("javascript:window.addEventListener('message',"
                    + "function(evt){window." + JS_INTERFACE
                    + ".onPostMessage(evt.data)}, false)");
        }
    }
}
}

我认为有两件事可能是错的

在getContent方法中有return; 在onPrintClick方法中发送PDF时,您没有将PDF转换为base64。 我希望我发现了一些有趣的东西: