2012-03-28 41 views
6

Tôi đang cố gắng in tệp .txt mà ứng dụng của tôi lưu qua FileWriter.In qua Google Cloud Print

File nó tiết kiệm là /sdcard/StudentLatePass.txt

Khi nút in được nhấp, các tập tin được lưu SD và sau đó nó cần phải in ra. Tôi đã theo dõi 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(); 
    } 
} 

Trong cuộc đời tôi, tôi đã cố gắng thay đổi docUri, docMimeType và docTitle thành mọi thứ dưới ánh mặt trời để in tệp này.

mã sửa đổi hiện tại của tôi là

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 

tôi vẫn nhận được squigglies đỏ dưới docUri, và khi tôi vượt qua ý định Print

+0

Di chuột qua "squigglies đỏ" và nó nói gì? – prolink007

+0

http://i.imgur.com/9RBOr.jpg – UPGRAYEDD

Trả lời

6

Hãy thử điều này và xem những gì sẽ xảy ra:

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

Điều đó sẽ giải quyết "squigglies đỏ" đầu tiên.


Đây là những gì bạn có thể thử cho vấn đề URI.

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

vwola, bây giờ bạn có biết tôi cần thay đổi docuri không? Tệp mà bạn lưu là /sdcard/StudentLatePass.txt – UPGRAYEDD

+0

Tôi đã thêm một số thông tin khác, hãy cho tôi biết nếu nó hoạt động. Hãy cho tôi +1 nếu nó hoạt động. =) – prolink007

+1

Tôi nghĩ cả hai bạn vừa mới làm xong ngày của tôi. – wHiTeHaT

Các vấn đề liên quan