0

这是我的 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 文件的位置。有人可以帮我吗?:(

4

1 回答 1

1

尝试这个

    String path= Environment.getExternalStorageDirectory().getPath();
    File file = new File(path+"/Download/kpeng.pdf");
    printIntent.setDataAndType(Uri.fromFile(file), "application/pdf");
于 2014-01-01T10:52:47.553 回答