通过socket从android发送图像到pc

时间:2018-01-19 12:09:31

标签: java android

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

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

当我尝试将图像发送到PC时,我得到一个java.lang.NullPointerException,它在SendImageTask.java第25行和第14行引起。 我无法理解异常。有人能帮助我吗? 这是代码: MainActivity:

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

SendImageTask

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 个答案:

答案 0 :(得分:0)

取消getPath()

FileInputStream fis = new FileInputStream(myFile);

将其更改为

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

并像以前一样使用该流。

答案 1 :(得分:0)

正如您可以看到您的应用在此行崩溃

  FileInputStream fis = new FileInputStream(myFile);

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

这是因为你的路径不正确,你的getpath函数不正确。 如果您想从图库中获取图像的路径,则需要将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;
}