2012-03-28 116 views
6

我试图打印一个.txt文件,我的应用程序通过FileWriter保存。通过Google云打印打印

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

当点击打印按钮时,SD文件被保存,则需要进行打印。我一直在关注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改变一切在阳光下打印此文件。

我现在修改后的代码是

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下红squigglies,当我传球意图Print

+0

将鼠标悬停在“红色曲线”上,它是什么意思? – prolink007 2012-03-28 16:38:17

+0

http://i.imgur.com/9RBOr.jpg – UPGRAYEDD 2012-03-28 16:40:04

回答

6

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

Intent printIntent = new Intent(StudentActivity.this, PrintDialogActivity.class);

这应该解决第一个“红色小丑”。


这是你可以尝试的URI问题。

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

vwola,现在你知道我需要更改docUri吗?它保存的文件位于/sdcard/StudentLatePass.txt – UPGRAYEDD 2012-03-28 16:51:17

+0

我添加了一些信息,让我知道它是否有效。如果有效,请给我一个+1。 =) – prolink007 2012-03-28 18:15:24

+1

我认为你们都是我的一天。 – wHiTeHaT 2012-04-04 20:57:27