gpt4 book ai didi

android - 将文件发送到 WiFi 打印机

转载 作者:太空宇宙 更新时间:2023-11-03 10:50:13 30 4
gpt4 key购买 nike

我正在开发一个应用程序,在该应用程序中,我通过 IP 地址和端口号从我的手机向 WiFi 打印机发送文件,这些文件是 .txt、.png、.jpg、.doc。它们应该从打印机打印出来。我尝试了以下代码,但它只为我提供了 .txt 文件的输出。需要进行哪些更改才能获得所有类型文件的输出???请帮助...

    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"/>

最佳答案

我知道这是一个有点老的问题。 Android 4.4 为您提供了此功能。

Please check these instructions

关于android - 将文件发送到 WiFi 打印机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14452437/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com