我正在开发中,从我的手机,我将文件发送到打印机的WiFi通过IP地址和端口号,这是txt文件,巴纽,JPG格式,名为.doc的应用程序。它们应该从打印机打印图像。我曾尝试以下code,但它给我的输出只有.txt文件。需要什么样的变化让所有类型的文件输出???请帮助...
进口java.io.BufferedInputStream中;
进口的java.io.File;
进口java.io.FileInputStream中;
进口java.io.IOException异常;
进口java.io.OutputStream中;
进口的java.net.Socket;
进口的java.net.UnknownHostException;
进口android.app.Activity;
进口android.os.Bundle;
进口android.view.View;
进口android.widget.Button;
进口android.widget.EditText;
进口android.widget.TextView;
公共类MainActivity延伸活动{
私人客户端的Socket;
私人的FileInputStream的FileInputStream;
私人的BufferedInputStream的BufferedInputStream;
私人的OutputStream的OutputStream;
私人Button按钮;
私人TextView的文字;
私人的EditText ETIP,etPort;
INT端口= 0;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
按钮=(按钮)findViewById(R.id.button1); //参照发送按钮
文=(TextView的)findViewById(R.id.textView1); //参考文本视图
ETIP =(EditText上)findViewById(R.id.editText1);
etPort =(EditText上)findViewById(R.id.editText2);
//按钮preSS事件监听器
button.setOnClickListener(新View.OnClickListener(){
公共无效的onClick(视图v){
//创建文件实例
端口=的Integer.parseInt(etPort.getText()的toString());
档案文件=新的文件(到/ mnt / SD卡/蓝牙/ AnyFile.txt);
尝试
{
客户端=新的Socket(etIp.getText()的toString(),口);
byte []的mybytearray =新的字节[(INT)file.length()]; //创建一个字节数组到文件
的FileInputStream =新的FileInputStream(文件);
的BufferedInputStream =新的BufferedInputStream(的FileInputStream);
bufferedInputStream.read(mybytearray,0,mybytearray.length); //读取文件
的OutputStream = client.getOutputStream();
outputStream.write(mybytearray,0,mybytearray.length); //写文件以由字节输出流字节
outputStream.flush();
bufferedInputStream.close();
outputStream.close();
client.close();
text.setText(文件已发送);
}赶上(UnknownHostException异常E){
e.printStackTrace();
}赶上(IOException异常E){
e.printStackTrace();
}
}
});
}
}
清单文件
<使用-权限的Android:名称=android.permission.INTERNET对/>
<使用-权限的Android:名称=android.permission.ACCESS_NETWORK_STATE/>
<使用-权限的Android:名称=android.permission.ACCESS_WIFI_STATE/>
<使用-权限的Android:名称=android.permission.CHANGE_WIFI_STATE/>
解决方案
我知道这是一个有点老问题 Android 4.4系统为您提供此功能
请检查这些说明
我希望这将帮助别人:)
I am developing an application in which from my mobile I am sending files to the WiFi printer by IP address and port number, which are .txt, .png, .jpg, .doc. They should be printed from the printer. I have tried following code but it is giving me output for .txt file only. What changes are required to get output of all type of files??? Please help...
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
private Socket client;
private FileInputStream fileInputStream;
private BufferedInputStream bufferedInputStream;
private OutputStream outputStream;
private Button button;
private TextView text;
private EditText etIp, etPort;
int port=0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button1); //reference to the send button
text = (TextView) findViewById(R.id.textView1); //reference to the text view
etIp = (EditText) findViewById(R.id.editText1);
etPort = (EditText) findViewById(R.id.editText2);
//Button press event listener
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//create file instance
port=Integer.parseInt(etPort.getText().toString());
File file = new File("/mnt/sdcard/bluetooth/AnyFile.txt");
try
{
client = new Socket(etIp.getText().toString(), port);
byte[] mybytearray = new byte[(int) file.length()]; //create a byte array to file
fileInputStream = new FileInputStream(file);
bufferedInputStream = new BufferedInputStream(fileInputStream);
bufferedInputStream.read(mybytearray, 0, mybytearray.length); //read the file
outputStream = client.getOutputStream();
outputStream.write(mybytearray, 0, mybytearray.length); //write file to the output stream byte by byte
outputStream.flush();
bufferedInputStream.close();
outputStream.close();
client.close();
text.setText("File Sent");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}
Manifest file
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
解决方案
I know it is a bit old Question Android 4.4 provides you this functionality
Please check these instructions
I hope it will help someone :)