我不断通过Socket
从java服务器接收图像并将其显示在ImageView
中。
但是Gargabe收藏家导致超过10帧。有没有办法避免这种情况。
public class ScreenCastActivity extends Activity {
PrintWriter printwriter;
static ImageView iv;
static Drawable ob;
Bitmap bitmap;
public static Socket client;
public static int port_add;
public static String port_addr;
public static String ip_add;
public static SendEvent task;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(R.layout.activity_screen_cast);
ip_add=getIntent().getStringExtra("Ip");
port_addr=getIntent().getStringExtra("Port");
Log.i("IP and Port in SC",ip_add+","+port_addr);
Serializer ma=new Serializer(4,"ScreenShot");
String json=ObjectSerializer(ma);
task=new SendEvent(json);
task.execute();
}
class SendEvent extends AsyncTask<Void, Void, Bitmap>{
String json;
public SendEvent(String json) {
this.json=json;
}
@Override
protected Bitmap doInBackground(Void... arg0){
try{
port_add=Integer.parseInt(port_addr);
client=new Socket(ip_add,port_add);
ObjectOutputStream oos=new ObjectOutputStream(client.getOutputStream());
Log.i("JSON",""+json);
oos.writeObject(json);
ObjectInputStream ois=new ObjectInputStream(client.getInputStream());
Log.i("OIS","Inside MyTask "+ois);
byte[] buffer=(byte[]) ois.readObject();
BitmapFactory.Options options=new BitmapFactory.Options();
options.inMutable=true;
bitmap = BitmapFactory.decodeByteArray(buffer, 0, buffer.length,options);
oos.flush();
ois.close();
client.close();
return bitmap;
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return null;
}
@SuppressWarnings("deprecation")
@Override
protected void onPostExecute(Bitmap result) {
if(result!=null){
iv=(ImageView) findViewById(R.id.screenView);
ob=new BitmapDrawable(getResources(),result);
iv.setBackgroundDrawable(ob);
if(!new SendEvent(null).isCancelled()){
Serializer ma=new Serializer(4,"ScreenShot");
String json=ObjectSerializer(ma);
task=new SendEvent(json);
task.execute();
}
}else{
Log.d("Bitmap null","Empty");
}
}
}
public String ObjectSerializer(Serializer serialize){
Gson gs=new Gson();
String gs_serial=gs.toJson(serialize);
return gs_serial;
}
@Override
protected void onDestroy() {
task.cancel(true);
task.isCancelled();
super.onDestroy();
}
}
logcat的
03-09 17:51:19.600: D/dalvikvm(31906): GC_CONCURRENT freed 86K, 9% free 12462K/13575K, paused 40ms+32ms, total 94ms
03-09 17:51:19.775: D/dalvikvm(31906): GC_FOR_ALLOC freed 2K, 9% free 12459K/13575K, paused 13ms, total 13ms
03-09 17:51:19.785: I/dalvikvm-heap(31906): Grow heap (frag case) to 17.113MB for 4196368-byte allocation
03-09 17:51:19.805: D/dalvikvm(31906): GC_CONCURRENT freed 0K, 7% free 16557K/17735K, paused 11ms+2ms, total 23ms
03-09 17:51:20.585: D/dalvikvm(31906): GC_FOR_ALLOC freed 181K, 8% free 16560K/17927K, paused 21ms, total 21ms
03-09 17:51:20.600: I/dalvikvm-heap(31906): Grow heap (frag case) to 21.118MB for 4196368-byte allocation
03-09 17:51:20.630: D/dalvikvm(31906): GC_CONCURRENT freed <1K, 7% free 20657K/22087K, paused 12ms+2ms, total 27ms
03-09 17:51:21.555: D/dalvikvm(31906): GC_FOR_ALLOC freed 4279K, 26% free 16561K/22087K, paused 29ms, total 29ms
03-09 17:51:21.555: I/dalvikvm-heap(31906): Grow heap (frag case) to 21.119MB for 4196368-byte allocation
03-09 17:51:21.595: D/dalvikvm(31906): GC_CONCURRENT freed <1K, 7% free 20659K/22087K, paused 13ms+4ms, total 41ms
03-09 17:51:22.150: D/dalvikvm(31906): GC_FOR_ALLOC freed 4279K, 26% free 16562K/22087K, paused 21ms, total 21ms
03-09 17:51:22.155: I/dalvikvm-heap(31906): Grow heap (frag case) to 21.120MB for 4196368-byte allocation
03-09 17:51:22.180: D/dalvikvm(31906): GC_CONCURRENT freed <1K, 7% free 20660K/22087K, paused 12ms+1ms, total 25ms
03-09 17:51:22.515: D/dalvikvm(31906): GC_FOR_ALLOC freed 4279K, 26% free 16559K/22087K, paused 12ms, total 12ms