有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java通过socket将图像从安卓发送到pc

我试过这些问题的代码at StackOverflow

发送例程位于异步任务中。另一个代码与问题中的代码类似

当我试图向电脑发送图像时,我得到了一个java。在SendImageTask上导致lang.NullPointerException。java第25行和第14行。 我不能理解这个例外。有人能帮我吗? 代码如下: 主要活动:

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;
}
}

共 (2) 个答案

  1. # 1 楼答案

    正如你可以看到你的应用程序在这一行崩溃

      FileInputStream fis = new FileInputStream(myFile);
    

    您可以使用调试器进行测试,并尝试使用您的路径创建FileInputStream,您将得到“找不到文件”

    这是因为路径不正确,getpath函数不正确。 如果要从gallery中获取图像的路径,需要将getPath函数更改为:

    public String getPath(Uri uri) {
        String[] projection = {MediaStore.Images.Media.DATA};
        Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        int columnIndex = cursor.getColumnIndex(projection[0]);
        String filePath = cursor.getString(columnIndex);
        cursor.close();
        return filePath;
    }
    
  2. # 2 楼答案

    废除getPath()

    FileInputStream fis = new FileInputStream(myFile);
    

    换成

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

    像以前一样使用那条流