I'm a layman of android programming.
I copied a package from Internet and want to send a picture under
drawable to my computer through TCP socket. However, I don't know how
to open the picture. This is my code:
-------------------------------------------------------------------------------------------------------------------
public class GalleryExample extends Activity {
private Gallery gallery;
private ImageView imgView;
private MediaPlayer mp;
public float azi;
public float pit;
public float rol;
public int pic_pos, curr_pic;
private static Context CONTEXT;
public static String TAG = "Stream Socket";
public static final String SERVERIP = "192.168.1.111";
public static final int SERVERPORT = 5000;
private Integer[] Imgid = {
R.drawable.a_1, R.drawable.a_2, R.drawable.a_3,
R.drawable.a_4, R.drawable.a_5, R.drawable.a_6, R.drawable.a_7
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
CONTEXT = this;
imgView = (ImageView)findViewById(R.id.ImageView01);
imgView.setImageResource(Imgid[0]);
gallery = (Gallery) findViewById(R.id.examplegallery);
gallery.setAdapter(new AddImgAdp(this));
gallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int
position, long id) {
imgView.setImageResource(Imgid[position]);
}
});
imgView.setOnClickListener (new View.OnClickListener(){
public void onClick(View v){
sendFile();
}
});
}
private void sendFile(){
try {
InetAddress serverAddr = InetAddress.getByName(SERVERIP);
Log.d(TAG, "Connecting to server at " + SERVERIP + ":" +
SERVERPORT);
final Socket socket = new Socket(serverAddr,
SERVERPORT);
OutputStream os = socket.getOutputStream();
PrintWriter out = new PrintWriter(new BufferedWriter(new
OutputStreamWriter(os)), true);
//send picture file
File myFile = new File(" ????????"); //how to get file
name?
// Log.d(TAG, "% File length:" + myFile.length() + "
exists?" + myFile.exists() + " Path:" + myFile.getPath());
byte[] buffer = new byte[(int) myFile.length()];
BufferedInputStream bis = new BufferedInputStream(new
FileInputStream(myFile));
Log.d(TAG, "% after BufferedInputStream ");
bis.read(buffer, 0, buffer.length);
Log.d(TAG, "% after bis.read" + buffer.length);
os.write(buffer, 0, buffer.length);
Log.d(TAG, "% Sent bytes out ");
os.flush();
os.close();
}catch (Exception e){
Log.e(TAG, "Try to send data to " + SERVERIP + ":" +
SERVERPORT + "-- FAILED");
Log.e(TAG, "Connection denied...");
Log.e(TAG, "Close");
}
}
public static Context getContext() {
return CONTEXT;
}
public class AddImgAdp extends BaseAdapter {
int GalItemBg;
private Context cont;
public AddImgAdp(Context c) {
cont = c;
TypedArray typArray =
obtainStyledAttributes(R.styleable.GalleryTheme);
GalItemBg =
typArray.getResourceId(R.styleable.GalleryTheme_android_galleryItemBackground,
0);
typArray.recycle();
}
public int getCount() {
return Imgid.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup
parent) {
ImageView imgView = new ImageView(cont);
imgView.setImageResource(Imgid[position]);
imgView.setLayoutParams(new Gallery.LayoutParams(80, 70));
imgView.setScaleType(ImageView.ScaleType.FIT_XY);
imgView.setBackgroundResource(GalItemBg);
return imgView;
}
}
}
----------------------------------------------------------------------------------------------------------------
can anybody help me? Thanks.
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en