通过谷歌云打印打印

6
我正在尝试通过FileWriter保存的.txt文件进行打印。
它保存的文件是/sdcard/StudentLatePass.txt。
当单击打印按钮时,SD文件将被保存,然后需要打印。我一直在遵循Google Cloud打印教程
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更改为各种可能性,以打印此文件。

我目前修改后的代码是:

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

我仍然在docUri下看到红色波浪线,当我将意图传递给Print时。

将鼠标悬停在“红色波浪线”上,它会显示什么? - prolink007
1个回答

6
尝试这个并查看结果: Intent printIntent = new Intent(StudentActivity.this, PrintDialogActivity.class); 这应该解决第一个“红色波浪线”。
以下是您可以尝试解决URI问题的方法。
File file = new File("/sdcard/StudentLatePass.txt");
intent.setDataAndType(Uri.fromFile(file), "text/*");

现在,你知道我需要将docUri更改为什么吗?它保存的文件位于/sdcard/StudentLatePass.txt。 - UPGRAYEDD
我添加了一些更多的信息,请让我知道是否有效。如果有效,请给我一个+1。=) - prolink007
1
我觉得你们俩刚刚让我的一天都变得美好了。 - wHiTeHaT
如果这段内容对你有帮助,别忘了为我们点赞 +1 =) - prolink007

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接