我只是从我的SD卡打印pdf文件。但是当我点击我的打印按钮时没有任何反应。
main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Print" />
main.java
public class MainActivity extends AppCompatActivity {
//to declare a button
Button btnPrint;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnPrint = (Button)findViewById(R.id.button1);
//set OnclickListener
btnPrint.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//check the network is available or not
if(isNetworkAvailable() == false){
Toast.makeText(MainActivity.this, "Network Connection Not Available Please Try Again Later", Toast.LENGTH_LONG).show();
}else{
File file = new File(Environment.getExternalStorageDirectory().getPath() + "personal/Brain Stuff.pdf");
Intent printIntent = new Intent(MainActivity.this,PrintDialogActivity.class);
printIntent.setDataAndType(Uri.fromFile(file),"application/pdf");
printIntent.putExtra("Title","Test Print");
}
}
});
}
public boolean isNetworkAvailable(){
ConnectivityManager cm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
//if no network is available means network ingo 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","No Network Available");
return false;
}
}
print_dialog.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<WebView
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
清单
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
当我点击我的按钮时,没有任何事情发生同样的屏幕就在那里。我还添加 PrintDialogActivity 。我犯了什么错误?指导我
答案 0 :(得分:1)
您需要使用
开始活动startActivity(printIntent);