package com.example.zzb;
import android.support.v7.app.ActionBarActivity;
import android.text.format.Time;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import com.example.zzb.MainActivity;
import android.app.Activity;
import android.content.Intent;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView imageView = null;
private Bitmap bmp = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.imageView1);
Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Thread t = new Thread() {
@Override
public void run() {
super.run();
Socket socket = null;
try {
socket = new Socket("192.168.191.1", 33336);
DataInputStream dataInput = new DataInputStream(
socket.getInputStream());
int size = dataInput.readInt();
byte[] data = new byte[size];
// dataInput.readFully(data);
int len = 0;
while (len < size) {
len += dataInput.read(data, len, size - len);
}
ByteArrayOutputStream outPut = new ByteArrayOutputStream();
bmp = BitmapFactory.decodeByteArray(data, 0,
data.length);
bmp.compress(CompressFormat.PNG, 100, outPut);
/*AssetManager am = null;
am = getAssets();
InputStream is = am.open("R.raw.a");
//imageView.setImageBitmap(); */
savePicture(bmp);
myHandler.obtainMessage().sendToTarget();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
};
t.start();
}
});
}
public void savePicture(Bitmap bitmap)
{
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{
try
{
File sdcardDir = Environment
.getExternalStorageDirectory();
Time t = new Time();
t.setToNow();
String filename = sdcardDir.getCanonicalPath()
+ "/DCIM/camera"
+ String.format(
"/ReeCam%04d%02d%02d%02d%02d%02d.jpg",
t.year, t.month + 1, t.monthDay,
t.hour, t.minute, t.second);
File file = new File(filename);
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
private Handler myHandler = new Handler(){
public void handleMessage(android.os.Message msg) {
imageView.setImageBitmap(bmp);
};
};
}
这是一个从网上找的代码,然后自己改了就能用了。
我想把这个变一下,变成不用onclick,就是电脑上通过socket发送图片过来,手机自动就能接收并保存到手机上,不用再来一张图片必须点击一下button按钮。
不知道怎么改,试过去掉OnClickListener,然后就发现只能接收第一张图片,后面想接收第二张,必须把应用后台一下,再进入才能接收第二张。
本人安卓小白,希望能得到解决方案。谢谢大家了