![](/img/trans.png)
[英]How to send file to google cloud printing app via intent in android 5?
[英]Printing via Google Cloud Print
我正在嘗試打印我的應用程序通過FileWriter保存的.txt文件。
它保存的文件是/sdcard/StudentLatePass.txt
單擊打印按鈕時,將保存SD文件,然后需要打印。 我一直在關注谷歌雲打印教程 。
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時
試試這個,看看會發生什么:
Intent printIntent = new Intent(StudentActivity.this, PrintDialogActivity.class);
這應該解決第一個“紅色波浪”。
以下是您可以嘗試解決URI問題的方法。
File file = new File("/sdcard/StudentLatePass.txt");
intent.setDataAndType(Uri.fromFile(file), "text/*");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.