我尝试在我的应用程序中集成谷歌云打印,但每次我尝试打印文档时,都会显示“文档缺失”!

时间:2013-11-05 13:31:50

标签: android printing cloud

这是我的MainActivity.java -

package com.example.printer;

import java.io.File;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import android.view.Menu;

public class MainActivity extends Activity {
    Button btnPrint;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_main);
           btnPrint=(Button)findViewById(R.id.button1);

           btnPrint.setOnClickListener(new OnClickListener() {

                  @Override
                  public void onClick(View v) {
                        // TODO Auto-generated method stub

                         if (isNetworkAvailable() == false) {
                                      Toast.makeText(MainActivity.this,
                                                    "Network connection not available, Please try later",
                                                    Toast.LENGTH_SHORT).show();
                               } else {
                                      File file = new File("/storage/emulated/0/Download/kpeng.pdf");
                                      Intent printIntent = new Intent(MainActivity.this, PrintDialogActivity.class);
                                      printIntent.setDataAndType(Uri.fromFile(file), "application/pdf");
                                      printIntent.putExtra("title", "Android print demo");
                                      startActivity(printIntent);
                               }
                  }
           });
    }

    public boolean isNetworkAvailable() {

           ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
           NetworkInfo networkInfo = cm.getActiveNetworkInfo();
           // if no network is available networkInfo will be null
           // otherwise check if we are connected
           if (networkInfo != null && networkInfo.isConnected()) {
                  Log.e("Network Testing", "***Available***");
                  return true;
           }
           Log.e("Network Testing", "***Not Available***");
           return false;
    }

}

我在三星s4上运行此应用程序,提供的位置(硬编码)是正确的! 要制作此应用,我已按照http://www.androidhub4you.com/2013/10/android-printer-integration-google.html中的说明操作。但是他们使用了File file = new File(Environment.getExternalStorageDirectory()。getAbsolutePath()+“/personal/xyz.pdf”);这对我不起作用,所以我硬编码了pdf文件的位置。有人可以帮帮我吗? :(

1 个答案:

答案 0 :(得分:1)

试试这个

    String path= Environment.getExternalStorageDirectory().getPath();
    File file = new File(path+"/Download/kpeng.pdf");
    printIntent.setDataAndType(Uri.fromFile(file), "application/pdf");