1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
|
public class MainActivity extends Activity {
Button imgsel,upload, radio1 ,radio2;
ImageView img;
String path;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = (ImageView)findViewById(R.id.img);
Ion.getDefault(this).configure().setLogging("ion-sample", Log.DEBUG);
imgsel = (Button)findViewById(R.id.selimg);
upload =(Button)findViewById(R.id.uploadimg);
upload.setVisibility(View.INVISIBLE);
//-->2 bUTTON checkBox ------
radio1= (Button)findViewById(R.id.r1);
radio2= (Button)findViewById(R.id.r2);
//-/------
upload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
File f = new File(path);
Future uploading = Ion.with(MainActivity.this)
.load("http://xxx.xxx.xx.x:xxxx/upload")
.setMultipartFile("image",f)
//asJsonObject()
.asString()
.withResponse()
.setCallback(new FutureCallback<Response<String>>() {
@Override
public void onCompleted(Exception e, Response<String> result) {
try {
JSONObject jobj = new JSONObject(result.getResult());
Toast.makeText(getApplicationContext(), jobj.getString("response"), Toast.LENGTH_SHORT).show();
} catch (JSONException e1) {
e1.printStackTrace();
}
}
});
}
});
imgsel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent fintent = new Intent(Intent.ACTION_GET_CONTENT);
fintent.setType("image/*");
try {
startActivityForResult(fintent, 100);
} catch (ActivityNotFoundException e) {
}
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data == null)
return;
switch (requestCode) {
case 100:
if (resultCode == RESULT_OK) {
path = getPathFromURI(data.getData());
img.setImageURI(data.getData());
upload.setVisibility(View.VISIBLE);
}
}
}
private String getPathFromURI(Uri contentUri) {
String[] proj = { MediaStore.Images.Media.DATA };
CursorLoader loader = new CursorLoader(getApplicationContext(), contentUri, proj, null, null, null);
Cursor cursor = loader.loadInBackground();
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
}
android node.js |
Partager