gpt4 book ai didi

java - 通过套接字将图像从 Android 发送到 PC

转载 作者:太空宇宙 更新时间:2023-11-04 10:46:35 25 4
gpt4 key购买 nike

我已经尝试过这些问题 at StackOverflow 中的代码.

发送例程位于 AsyncTask 中。其他代码与问题中的代码类似。

当我尝试将图像发送到 PC 时,在 SendImageTask.java 第 25 行和第 14 行处出现 java.lang.NullPointerException。我无法理解异常。有人可以帮助我吗?这是代码:主要 Activity :

public class MainActivity extends Activity {
/** Called when the activity is first created. */

private static final int SELECT_PICTURE = 1;

public static String selectedImagePath;
private ImageView img;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
System.out.println("34");
img = (ImageView) findViewById(R.id.ivPic);
System.out.println("36");
((Button) findViewById(R.id.bBrowse))
.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
System.out.println("40");
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(
Intent.createChooser(intent, "Select Picture"),
SELECT_PICTURE);
System.out.println("47");
}
});
;
System.out.println("51");
Button send = (Button) findViewById(R.id.bSend);
final TextView status = (TextView) findViewById(R.id.tvStatus);

send.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {

new SendImageTask().execute();

}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
TextView path = (TextView) findViewById(R.id.tvPath);
path.setText("Image Path : " + selectedImagePath);
img.setImageURI(selectedImageUri);
}
}
}

public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
}

发送图像任务

class SendImageTask extends AsyncTask<Void, Void, Void> {


@Override
protected Void doInBackground(Void... voids) {
Socket sock;
try {
sock = new Socket("myip", 8000);
System.out.println("Connecting...");

// sendfile
File myFile = new File (MainActivity.selectedImagePath);
byte [] mybytearray = new byte [(int)myFile.length()];
FileInputStream fis = new FileInputStream(myFile);
BufferedInputStream bis = new BufferedInputStream(fis);
bis.read(mybytearray,0,mybytearray.length);
OutputStream os = sock.getOutputStream();
System.out.println("Sending...");
os.write(mybytearray,0,mybytearray.length);
os.flush();

sock.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}

最佳答案

取消getPath()

FileInputStream fis = new FileInputStream(myFile);

将其更改为

InputStream is = getContentResolver().openInputStream(data.getData());

并像以前一样使用该流。

关于java - 通过套接字将图像从 Android 发送到 PC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48340792/

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