通过谷歌云打印打印 [英] Printing via Google Cloud Print

查看:155
本文介绍了通过谷歌云打印打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打印我的申请通过的FileWriter保存一个txt文件。

I am trying to print a .txt file that my application saves via FileWriter.

它保存的文件是/sdcard/StudentLatePass.txt

The file it saves is /sdcard/StudentLatePass.txt

当点击打印按钮,SD文件被保存,然后它需要打印。我一直遵循谷歌云打印教程

When the print button is clicked, the SD file is saved and then it needs to print. I've been following the google cloud print tutorial.

package com.android.upgrayeddapps.latepass;



import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Button;
import android.widget.Toast;



public class StudentActivity extends Activity 
{
    EditText txtData;
    Button btnPrintTardy;

    public void onCreate(Bundle savedInstanceState) 
    {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.student);

        //Displays the Custom dialog for student login
        AlertDialog.Builder builder;
        AlertDialog alertDialog;

        Context mContext = this;
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE) ;
        View layout = inflater.inflate(R.layout.studentlogin, null);

        builder = new AlertDialog.Builder(mContext);
        builder.setView(layout);            

        alertDialog = builder.create();
        alertDialog.show();

        txtData = (EditText) findViewById(R.id.editText1);

        btnPrintTardy = (Button) findViewById(R.id.btnPrintTardy);
        btnPrintTardy.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // write on SD card file data in the text box
            try {
                File myFile = new File("/sdcard/StudentLatePass.txt");
                myFile.createNewFile();
                FileOutputStream fOut = new FileOutputStream(myFile);
                OutputStreamWriter myOutWriter = 
                                        new OutputStreamWriter(fOut);
                myOutWriter.append(txtData.getText());
                myOutWriter.close();
                fOut.close();
                Toast.makeText(getBaseContext(),
                        "Done writing SD 'StudentLatePass.txt'",
                        Toast.LENGTH_SHORT).show();
            } catch (Exception e) {
                Toast.makeText(getBaseContext(), e.getMessage(),
                        Toast.LENGTH_SHORT).show();
            }


        }// onClick
        }); // btnWriteSDFile


        btnPrintTardy.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            //Print using Google Cloud Print
            Intent printIntent = new Intent(this, PrintDialogActivity.class);
                printIntent.setDataAndType(docUri, "text/plain");
                printIntent.putExtra("title", "Student Late Pass");
                startActivity(printIntent);

            }// onClick
    });// btnPrintSDFile




    }

    // Clear all activities on the top of the stack and deliver the intent to (on top now) MainActivity with a new Intent
    public void onGotoLatePassActiviy(View View)
    {
        Intent intent = new Intent(View.getContext(), LatePassActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        StudentActivity.this.finish();
    }
}

有关我的生活中,我曾尝试到docUri,docMimeType和docTitle改变一切在阳光下打印文件。

For the life of me I have tried to change the docUri, docMimeType and docTitle to everything under the sun to print this file.

我目前的修改code是

My current modified code is

btnPrintTardy.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            //Print using Google Cloud Print
            Intent printIntent = new Intent(this, PrintDialogActivity.class);
                printIntent.setDataAndType(docUri, "text/plain");
                printIntent.putExtra("title", "Student Late Pass");
                startActivity(printIntent);

            }// onClick
    });// btnPrintSDFile

我仍然得到红squigglies下docUri,当我传球意图打印

推荐答案

试试这个,看看会发生什么:

Try this and see what happens:

意图printIntent =新的意图(StudentActivity.this,PrintDialogActivity.class);

这应该解决的第一个红色squigglies。

That should resolve the first "red squigglies".

下面是你可以尝试的URI的问题。

Here is what you could try for the URI problem.

File file = new File("/sdcard/StudentLatePass.txt");
intent.setDataAndType(Uri.fromFile(file), "text/*");

这篇关于通过谷歌云打印打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆