Java 在android服务器端存储图像?
我尝试将图像存储在服务器和SQLite数据库上,但出现了异常。所有其他参数值都存储在数据库中,但图像未存储。请说明我的错误所在 LogIn.javaJava 在android服务器端存储图像?,java,android,image,web-services,bitmap,Java,Android,Image,Web Services,Bitmap,我尝试将图像存储在服务器和SQLite数据库上,但出现了异常。所有其他参数值都存储在数据库中,但图像未存储。请说明我的错误所在 LogIn.java private class Images extends AsyncTask<Bitmap[],Bitmap,Bitmap[]> { private Exception exception; protected Bitmap[] doInBackground(Bitmap[]...urls) {
private class Images extends AsyncTask<Bitmap[],Bitmap,Bitmap[]> {
private Exception exception;
protected Bitmap[] doInBackground(Bitmap[]...urls) {
try {
bitmap1 = DownloadImage(mimagelicense);
bitmap2 = DownloadImage(mauthcard);
bitmap3= DownloadImage(mpic);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap1.compress(Bitmap.CompressFormat.JPEG, 100, stream);
imageInByte1 = stream.toByteArray();
System.out.print("DB storing image " + imageInByte1);
bitmap2.compress(Bitmap.CompressFormat.JPEG, 100, stream);
imageInByte2 = stream.toByteArray();
bitmap3.compress(Bitmap.CompressFormat.JPEG, 100, stream);
imageInByte3 = stream.toByteArray();
System.out.println(" ****************************");
for(int i = 0; i < driver_details.length; i++)
// System.out.println(driver_details[i]);
Log.v("Async variables",driver_details[i]);
}
catch(Exception e)
{
e.printStackTrace();
}
db = new PasswordDB(getApplicationContext());
try
{
db.insert(mfname, msname, mun, mstreet, msuburb,mstate, mpostcode, mlicense, mid, mmobile, memail, imageInByte1, imageInByte2, imageInByte3);
}
catch (Exception e) {
// TODO: handle exception
}
/**
* CRUD Operations
* */
// Inserting Contacts
//db.close();
return bitmap;
}
private java.io.InputStream OpenHttpConnection(String urlString)
throws IOException
{
java.io.InputStream in = null;
int response = -1;
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
if (!(conn instanceof HttpURLConnection))
throw new IOException("Not an HTTP connection");
try{
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
response = httpConn.getResponseCode();
if (response == HttpURLConnection.HTTP_OK) {
in = httpConn.getInputStream();
}
}
catch (Exception ex)
{
ex.printStackTrace();
throw new IOException("Error connecting");
}
return in;
}
private Bitmap DownloadImage(String URL)
{
Bitmap bitmap = null;
java.io.InputStream in = null;
try {
in = OpenHttpConnection(URL);
bitmap = BitmapFactory.decodeStream(in);
in.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return bitmap;
}
@Override
protected void onPostExecute(Bitmap[] result)
{
super.onPostExecute(result);
}
}
private void checkNotNull(Object reference, String name) {
if (reference == null) {
throw new NullPointerException("Config error "+name);
}
}
private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent)
{
}
};
public void gcmService()
{
checkNotNull(SERVER_URL, "SERVER_URL");
checkNotNull(SENDER_ID, "SENDER_ID");
GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
registerReceiver(mHandleMessageReceiver, new IntentFilter(
DISPLAY_MESSAGE_ACTION));
final String regId = GCMRegistrar.getRegistrationId(this);
if (regId.equals(""))
{
GCMRegistrar.register(this, SENDER_ID);
} else {
if (GCMRegistrar.isRegisteredOnServer(this)) {
} else {
final Context context = this;
mRegisterTask = new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
boolean registered = ServerUtilities.register(context,
regId);
if (!registered) {
GCMRegistrar.unregister(context);
}
return null;
}
@Override
protected void onPostExecute(Void result) {
mRegisterTask = null;
}
};
mRegisterTask.execute(null, null, null);
}
}
}
@Override
protected void onDestroy() {
try{
if (mRegisterTask != null) {
mRegisterTask.cancel(true);
}
if(mHandleMessageReceiver!=null)
{
unregisterReceiver(mHandleMessageReceiver);
GCMRegistrar.onDestroy(this);
}
}
catch (Exception e)
{
e.printStackTrace();
}
super.onDestroy();
}
}
public class Registration extends Activity implements OnClickListener{
EditText fname,sname,un,street,suburb,password,confirm_password,postcode,license,id,mobile,email,imagelicense,authcard,pic;
String mfname,msname,mun,mstreet,msuburb,mstate,mpassword,mconfirm_password,mpostcode,mlicense,mid,mmobile,memail,mimagelicense,mauthcard,mpic;
Button submit , upload_license, upload_authority,upload_face;
TextView rt;
private Spinner state;
private static MyDialog dialog1;
private Typeface ftype;
private PasswordDB dp;
static final int DIALOG_ID = 0;
Uri imageUri;
private Bitmap license_img,authority_img,face_img ;
private static final int PICK_Camera_IMAGE = 2;
private WebView webview1;
private static final int REQUEST_CODE1 = 1;
private static final int REQUEST_CODE2 = 2;
private static final int REQUEST_CODE3 = 3;
String regid;
private Context mContext;
static String device_token="";
public static final String EXTRA_MESSAGE = "message";
public static final String PROPERTY_REG_ID = "registration_id";
String SENDER_ID = "960152798745";
AsyncTask<Void, Void, Void> mRegisterTask;
static final String TAG = "GCMDemo";
//GoogleCloudMessaging gcm;
ImageView license_image,authority_image,face_image;
InputStream iss;
BitmapFactory.Options bfo;
Bitmap bitmapOrg;
ByteArrayOutputStream bao ;
private Bitmap bitmap;
String deviceId;
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.registration);
deviceId = Secure.getString(this.getContentResolver(),Secure.ANDROID_ID);
Toast.makeText(this, deviceId, Toast.LENGTH_SHORT).show();
System.out.print("Device Id"+deviceId);
Toast.makeText(getApplicationContext(), "ID is"+deviceId, Toast.LENGTH_LONG).show();
Log.v("debiceID",deviceId);
if (android.os.Build.VERSION.SDK_INT > 9)
{
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
// ftype=Typeface.createFromAsset(getAssets(),"font/Futura Koyu.ttf");
fname = (EditText)findViewById(R.id.efirst_name);
sname = (EditText)findViewById(R.id.esur_name);
un = (EditText)findViewById(R.id.eunit_no);
street = (EditText)findViewById(R.id.estreet_name);
suburb = (EditText)findViewById(R.id.esuburb);
state = (Spinner)findViewById(R.id.state);
password = (EditText)findViewById(R.id.epassword);
confirm_password = (EditText)findViewById(R.id.econfirm_password);
postcode = (EditText)findViewById(R.id.epost_code);
license = (EditText)findViewById(R.id.edriving_license);
id = (EditText)findViewById(R.id.eauthority_id);
mobile = (EditText)findViewById(R.id.emobile_no);
email = (EditText)findViewById(R.id.eemail);
submit = (Button)findViewById(R.id.submit);
upload_license = (Button)findViewById(R.id.license);
upload_authority = (Button)findViewById(R.id.authority);
upload_face = (Button)findViewById(R.id.face);
license_image =(ImageView)findViewById(R.id.imageView_pic);
authority_image =(ImageView)findViewById(R.id.imageView_pic2);
face_image =(ImageView)findViewById(R.id.imageView_pic3);
submit.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent intent = new Intent(Registration.this, webview.class);
startActivity(intent);
new DriverRegistration().execute();
System.out.println("called ");
}
});
String[] mystate= new String[]{"New South Wales","Victoria","Queensland","Northern Territory","Western Australia","South Australia"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(Registration.this, R.layout.listrow, mystate);
state.setAdapter(adapter);
state.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,int pos, long arg3) {
mstate = state.getSelectedItem().toString();
String s1=arg0.getItemAtPosition(pos).toString();
if(s1.equals("New South Wales"))
suburb.setText("Sydney");
else if(s1.equals("Victoria"))
suburb.setText("Melbourne");
else if(s1.equals("Queensland"))
suburb.setText("Brisbane");
else if(s1.equals("Northern Territory"))
suburb.setText("Darwin");
else if(s1.equals("Western Australia"))
suburb.setText("Perth");
else if(s1.equals("South Australia"))
suburb.setText("Adelaide");
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
}
public void pickImage(View View) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(intent, REQUEST_CODE1);
}
public void pickImage2(View View) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(intent, REQUEST_CODE2);
}
public void pickImage3(View View) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(intent, REQUEST_CODE3);
}
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
class RegisterBackground extends AsyncTask<String,String,String>{
private final Context _context;
public RegisterBackground(Context context){
_context = context;
}
@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
String msg = "";
return regid;
}
@Override
protected void onPostExecute(String msg) {
//mDisplay.append(msg + "\n");
Registration test =(Registration) _context ;
device_token=msg;
super.onPostExecute(msg);
}
private void sendRegistrationIdToBackend(String regid) {
//String url = "http://suntechwebsolutions.com/clients/mobileapp_now/webservice.php";
String url = "http://suntechwebsolutions.com/clients/DGCapp/webservice.php";
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("device_token", regid));
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
try {
httpPost.setEntity(new UrlEncodedFormEntity(params));
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
HttpResponse httpResponse = httpClient.execute(httpPost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private class DriverRegistration extends AsyncTask<String, String, String[]> {
ProgressDialog pDialog = new ProgressDialog(Registration.this);
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@Override
protected String[] doInBackground(final String... params)
{
ConnectivityManager conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if (conMgr.getActiveNetworkInfo() != null
&& conMgr.getActiveNetworkInfo().isAvailable()
&& conMgr.getActiveNetworkInfo().isConnected())
{
HttpClient httpclient = new DefaultHttpClient();
try
{
System.out.println("driver ");
pDialog.setMessage("Please wait DriverRegistration...");
runOnUiThread(new Runnable()
{
public void run()
{
pDialog.setCanceledOnTouchOutside(false);
pDialog.show();
}
});
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
JSONObject job= new JSONObject();
mfname = fname.getText().toString();
msname = sname.getText().toString();
mun= un.getText().toString();
mstreet = street.getText().toString();
msuburb = suburb.getText().toString();
mstate= state.getSelectedItem().toString();
mpostcode = postcode.getText().toString();
mlicense =license.getText().toString();
mid = id.getText().toString();
mmobile = mobile.getText().toString();
memail= email.getText().toString();
mpassword=password.getText().toString();
mconfirm_password=confirm_password.getText().toString();
mfname.replace("" ,"%20");
msname.replace("" ,"%20");
mun.replace("" ,"%20");
mstreet.replace("" ,"%20");
msuburb.replace("" ,"%20");
mstate.replace("" ,"%20");
mpostcode.replace("" ,"%20");
mlicense.replace("" ,"%20");
mid.replace("" ,"%20");
mmobile.replace("" ,"%20");
memail.replace("" ,"%20");
mpassword.replace("" ,"%20");
mconfirm_password.replace("" ,"%20");
//
bfo = new BitmapFactory.Options();
bfo.inSampleSize = 32;
//bitmapOrg = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory() + "/" + customImage, bfo);
bfo.inScaled = true;
bfo.inPurgeable = true;
bao = new ByteArrayOutputStream();
license_img.compress(Bitmap.CompressFormat.PNG, 50, bao);
license_img = Bitmap.createScaledBitmap(license_img, 200, 200, false);
byte [] ba = bao.toByteArray();
int baa = (ba.length)/1024;
authority_img.compress(Bitmap.CompressFormat.PNG, 50, bao);
authority_img = Bitmap.createScaledBitmap(license_img, 200, 200, false);
byte [] baaa = bao.toByteArray();
int baas = (baaa.length)/1024;
face_img.compress(Bitmap.CompressFormat.PNG, 50, bao);
face_img.compress(Bitmap.CompressFormat.PNG, 50, bao);
byte [] baaap = bao.toByteArray();
int baasp = (baaap.length)/1024;
System.out.println("size of image ....."+baa);
//String image_str = Base64.encodeBytes(ba);
System.out.println(" imagedata................"+baa);
String license_img = Base64.encodeBytes(ba);
String authority_img = Base64.encodeBytes(baaa);
String face_img = Base64.encodeBytes(baaap);
System.out.println("size of image1 ................."+license_img);
System.out.println("size of image2 ................."+authority_img);
System.out.println("size of image3 ................."+face_img);
job.put("method","driver_register");
job.put("first_name",mfname);
job.put("surname",msname);
job.put("unit_no",mun);
job.put("street_name",mstreet);
job.put("suburb",msuburb);
job.put("state",mstate);
job.put("password",mpassword);
// job.put("mpassword",mconfirm_password);
job.put("post_code","mpostcode");
job.put("driving_license",mlicense);
job.put("authority_id",mid);
job.put("mobile_no",mmobile);
job.put("email",memail);
job.put("device_id",device_token);
job.put("license_pic",license_img);
job.put("audit_card_pic",authority_img);
job.put("face_pic",face_img);
// job.put("unique_id",androidId);
StringEntity se = new StringEntity(job.toString());
//HttpPost httppost = new HttpPost("http://suntechwebsolutions.com/clients/mobileapp_now/webservice.php");
HttpPost httppost = new HttpPost("http://suntechwebsolutions.com/clients/DGCapp/webservice.php");
httppost.setEntity(se);
HttpResponse response = httpclient.execute(httppost);
String tmpString = EntityUtils.toString(response.getEntity());
String data = tmpString.replace("yes","");
Log.i("response", data);
System.out.println("response "+data);
String call;
call = data;
System.out.println("print me............."+call);
JSONObject jo = new JSONObject(data);
Log.d("response", jo.toString(4));
if(jo.getString("err-code").equals("0"))
{
final AlertDialog.Builder alert = new AlertDialog.Builder(Registration.this);
alert.setTitle("Alert!!!");
alert.setMessage(jo.getString("message"));
alert.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton)
{
pDialog.dismiss();
dialog.dismiss();
startActivity(new Intent(Registration.this,LoginActivity.class));
}
});
runOnUiThread(new Runnable() {
public void run() {
alert.show();
}
});
}
else
{
final AlertDialog.Builder alert = new AlertDialog.Builder(Registration.this);
alert.setTitle("Alert !");
alert.setMessage(jo.getString("message"));
alert.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
dialog.dismiss();
}
});
runOnUiThread(new Runnable()
{
public void run()
{
pDialog.dismiss();
alert.show();
}
});
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
else
{
final AlertDialog.Builder alert = new AlertDialog.Builder(Registration.this);
alert.setTitle("Alert !");
alert.setMessage("No Internet connection ");
alert.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton)
{
dialog.dismiss();
}
});
runOnUiThread(new Runnable()
{
public void run()
{
//pDialog.dismiss();
alert.show();
}
});
}
return params;
}
@Override
protected void onPostExecute(String[] result)
{
//
pDialog.dismiss();
super.onPostExecute(result);
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(requestCode)
{
case REQUEST_CODE1:
resultCode = Activity.RESULT_OK ;
try {
// We need to recyle unused bitmaps
if (license_img != null) {
license_img.recycle();
}
InputStream stream = getContentResolver().openInputStream(
data.getData());
license_img = BitmapFactory.decodeStream(stream);
stream.close();
license_image.setImageBitmap(license_img);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
super.onActivityResult(requestCode, resultCode, data);
break;
case REQUEST_CODE2:
resultCode = Activity.RESULT_OK ;
try {
// We need to recyle unused bitmaps
if (authority_img != null) {
authority_img.recycle();
}
InputStream stream = getContentResolver().openInputStream(
data.getData());
authority_img = BitmapFactory.decodeStream(stream);
stream.close();
authority_image.setImageBitmap(authority_img);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
super.onActivityResult(requestCode, resultCode, data);
break;
case REQUEST_CODE3:
resultCode = Activity.RESULT_OK ;
try {
// We need to recyle unused bitmaps
if (face_img != null) {
face_img.recycle();
}
InputStream stream = getContentResolver().openInputStream(
data.getData());
face_img = BitmapFactory.decodeStream(stream);
stream.close();
face_image.setImageBitmap(face_img);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
super.onActivityResult(requestCode, resultCode, data);
}
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
if (cursor != null) {
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} else
return null;
}
public void decodeFile(String filePath) {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, o);
final int REQUIRED_SIZE = 70;
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
license_img = BitmapFactory.decodeFile(filePath, o2);
authority_img = BitmapFactory.decodeFile(filePath, o2);
face_img = BitmapFactory.decodeFile(filePath, o2);
license_image.setImageBitmap(license_img);
authority_image.setImageBitmap(authority_img);
face_image.setImageBitmap(face_img);
}
}
私有类映像扩展异步任务{
私人例外;
受保护的位图[]doInBackground(位图[]…URL){
试一试{
bitmap1=下载图像(mimagelicense);
bitmap2=下载图像(mauthcard);
bitmap3=下载图像(mpic);
ByteArrayOutputStream=新建ByteArrayOutputStream();
bitmap1.compress(位图.CompressFormat.JPEG,100,流);
imageInByte1=stream.toByteArray();
系统输出打印(“数据库存储图像”+imageInByte1);
bitmap2.compress(位图.CompressFormat.JPEG,100,流);
imageInByte2=stream.toByteArray();
bitmap3.compress(位图.CompressFormat.JPEG,100,流);
imageInByte3=stream.toByteArray();
System.out.println(“**********************************”);
对于(int i=0;i
Registration.java
private class Images extends AsyncTask<Bitmap[],Bitmap,Bitmap[]> {
private Exception exception;
protected Bitmap[] doInBackground(Bitmap[]...urls) {
try {
bitmap1 = DownloadImage(mimagelicense);
bitmap2 = DownloadImage(mauthcard);
bitmap3= DownloadImage(mpic);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap1.compress(Bitmap.CompressFormat.JPEG, 100, stream);
imageInByte1 = stream.toByteArray();
System.out.print("DB storing image " + imageInByte1);
bitmap2.compress(Bitmap.CompressFormat.JPEG, 100, stream);
imageInByte2 = stream.toByteArray();
bitmap3.compress(Bitmap.CompressFormat.JPEG, 100, stream);
imageInByte3 = stream.toByteArray();
System.out.println(" ****************************");
for(int i = 0; i < driver_details.length; i++)
// System.out.println(driver_details[i]);
Log.v("Async variables",driver_details[i]);
}
catch(Exception e)
{
e.printStackTrace();
}
db = new PasswordDB(getApplicationContext());
try
{
db.insert(mfname, msname, mun, mstreet, msuburb,mstate, mpostcode, mlicense, mid, mmobile, memail, imageInByte1, imageInByte2, imageInByte3);
}
catch (Exception e) {
// TODO: handle exception
}
/**
* CRUD Operations
* */
// Inserting Contacts
//db.close();
return bitmap;
}
private java.io.InputStream OpenHttpConnection(String urlString)
throws IOException
{
java.io.InputStream in = null;
int response = -1;
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
if (!(conn instanceof HttpURLConnection))
throw new IOException("Not an HTTP connection");
try{
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
response = httpConn.getResponseCode();
if (response == HttpURLConnection.HTTP_OK) {
in = httpConn.getInputStream();
}
}
catch (Exception ex)
{
ex.printStackTrace();
throw new IOException("Error connecting");
}
return in;
}
private Bitmap DownloadImage(String URL)
{
Bitmap bitmap = null;
java.io.InputStream in = null;
try {
in = OpenHttpConnection(URL);
bitmap = BitmapFactory.decodeStream(in);
in.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return bitmap;
}
@Override
protected void onPostExecute(Bitmap[] result)
{
super.onPostExecute(result);
}
}
private void checkNotNull(Object reference, String name) {
if (reference == null) {
throw new NullPointerException("Config error "+name);
}
}
private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent)
{
}
};
public void gcmService()
{
checkNotNull(SERVER_URL, "SERVER_URL");
checkNotNull(SENDER_ID, "SENDER_ID");
GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
registerReceiver(mHandleMessageReceiver, new IntentFilter(
DISPLAY_MESSAGE_ACTION));
final String regId = GCMRegistrar.getRegistrationId(this);
if (regId.equals(""))
{
GCMRegistrar.register(this, SENDER_ID);
} else {
if (GCMRegistrar.isRegisteredOnServer(this)) {
} else {
final Context context = this;
mRegisterTask = new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
boolean registered = ServerUtilities.register(context,
regId);
if (!registered) {
GCMRegistrar.unregister(context);
}
return null;
}
@Override
protected void onPostExecute(Void result) {
mRegisterTask = null;
}
};
mRegisterTask.execute(null, null, null);
}
}
}
@Override
protected void onDestroy() {
try{
if (mRegisterTask != null) {
mRegisterTask.cancel(true);
}
if(mHandleMessageReceiver!=null)
{
unregisterReceiver(mHandleMessageReceiver);
GCMRegistrar.onDestroy(this);
}
}
catch (Exception e)
{
e.printStackTrace();
}
super.onDestroy();
}
}
public class Registration extends Activity implements OnClickListener{
EditText fname,sname,un,street,suburb,password,confirm_password,postcode,license,id,mobile,email,imagelicense,authcard,pic;
String mfname,msname,mun,mstreet,msuburb,mstate,mpassword,mconfirm_password,mpostcode,mlicense,mid,mmobile,memail,mimagelicense,mauthcard,mpic;
Button submit , upload_license, upload_authority,upload_face;
TextView rt;
private Spinner state;
private static MyDialog dialog1;
private Typeface ftype;
private PasswordDB dp;
static final int DIALOG_ID = 0;
Uri imageUri;
private Bitmap license_img,authority_img,face_img ;
private static final int PICK_Camera_IMAGE = 2;
private WebView webview1;
private static final int REQUEST_CODE1 = 1;
private static final int REQUEST_CODE2 = 2;
private static final int REQUEST_CODE3 = 3;
String regid;
private Context mContext;
static String device_token="";
public static final String EXTRA_MESSAGE = "message";
public static final String PROPERTY_REG_ID = "registration_id";
String SENDER_ID = "960152798745";
AsyncTask<Void, Void, Void> mRegisterTask;
static final String TAG = "GCMDemo";
//GoogleCloudMessaging gcm;
ImageView license_image,authority_image,face_image;
InputStream iss;
BitmapFactory.Options bfo;
Bitmap bitmapOrg;
ByteArrayOutputStream bao ;
private Bitmap bitmap;
String deviceId;
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.registration);
deviceId = Secure.getString(this.getContentResolver(),Secure.ANDROID_ID);
Toast.makeText(this, deviceId, Toast.LENGTH_SHORT).show();
System.out.print("Device Id"+deviceId);
Toast.makeText(getApplicationContext(), "ID is"+deviceId, Toast.LENGTH_LONG).show();
Log.v("debiceID",deviceId);
if (android.os.Build.VERSION.SDK_INT > 9)
{
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
// ftype=Typeface.createFromAsset(getAssets(),"font/Futura Koyu.ttf");
fname = (EditText)findViewById(R.id.efirst_name);
sname = (EditText)findViewById(R.id.esur_name);
un = (EditText)findViewById(R.id.eunit_no);
street = (EditText)findViewById(R.id.estreet_name);
suburb = (EditText)findViewById(R.id.esuburb);
state = (Spinner)findViewById(R.id.state);
password = (EditText)findViewById(R.id.epassword);
confirm_password = (EditText)findViewById(R.id.econfirm_password);
postcode = (EditText)findViewById(R.id.epost_code);
license = (EditText)findViewById(R.id.edriving_license);
id = (EditText)findViewById(R.id.eauthority_id);
mobile = (EditText)findViewById(R.id.emobile_no);
email = (EditText)findViewById(R.id.eemail);
submit = (Button)findViewById(R.id.submit);
upload_license = (Button)findViewById(R.id.license);
upload_authority = (Button)findViewById(R.id.authority);
upload_face = (Button)findViewById(R.id.face);
license_image =(ImageView)findViewById(R.id.imageView_pic);
authority_image =(ImageView)findViewById(R.id.imageView_pic2);
face_image =(ImageView)findViewById(R.id.imageView_pic3);
submit.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent intent = new Intent(Registration.this, webview.class);
startActivity(intent);
new DriverRegistration().execute();
System.out.println("called ");
}
});
String[] mystate= new String[]{"New South Wales","Victoria","Queensland","Northern Territory","Western Australia","South Australia"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(Registration.this, R.layout.listrow, mystate);
state.setAdapter(adapter);
state.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,int pos, long arg3) {
mstate = state.getSelectedItem().toString();
String s1=arg0.getItemAtPosition(pos).toString();
if(s1.equals("New South Wales"))
suburb.setText("Sydney");
else if(s1.equals("Victoria"))
suburb.setText("Melbourne");
else if(s1.equals("Queensland"))
suburb.setText("Brisbane");
else if(s1.equals("Northern Territory"))
suburb.setText("Darwin");
else if(s1.equals("Western Australia"))
suburb.setText("Perth");
else if(s1.equals("South Australia"))
suburb.setText("Adelaide");
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
}
public void pickImage(View View) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(intent, REQUEST_CODE1);
}
public void pickImage2(View View) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(intent, REQUEST_CODE2);
}
public void pickImage3(View View) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(intent, REQUEST_CODE3);
}
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
class RegisterBackground extends AsyncTask<String,String,String>{
private final Context _context;
public RegisterBackground(Context context){
_context = context;
}
@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
String msg = "";
return regid;
}
@Override
protected void onPostExecute(String msg) {
//mDisplay.append(msg + "\n");
Registration test =(Registration) _context ;
device_token=msg;
super.onPostExecute(msg);
}
private void sendRegistrationIdToBackend(String regid) {
//String url = "http://suntechwebsolutions.com/clients/mobileapp_now/webservice.php";
String url = "http://suntechwebsolutions.com/clients/DGCapp/webservice.php";
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("device_token", regid));
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
try {
httpPost.setEntity(new UrlEncodedFormEntity(params));
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
HttpResponse httpResponse = httpClient.execute(httpPost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private class DriverRegistration extends AsyncTask<String, String, String[]> {
ProgressDialog pDialog = new ProgressDialog(Registration.this);
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@Override
protected String[] doInBackground(final String... params)
{
ConnectivityManager conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if (conMgr.getActiveNetworkInfo() != null
&& conMgr.getActiveNetworkInfo().isAvailable()
&& conMgr.getActiveNetworkInfo().isConnected())
{
HttpClient httpclient = new DefaultHttpClient();
try
{
System.out.println("driver ");
pDialog.setMessage("Please wait DriverRegistration...");
runOnUiThread(new Runnable()
{
public void run()
{
pDialog.setCanceledOnTouchOutside(false);
pDialog.show();
}
});
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
JSONObject job= new JSONObject();
mfname = fname.getText().toString();
msname = sname.getText().toString();
mun= un.getText().toString();
mstreet = street.getText().toString();
msuburb = suburb.getText().toString();
mstate= state.getSelectedItem().toString();
mpostcode = postcode.getText().toString();
mlicense =license.getText().toString();
mid = id.getText().toString();
mmobile = mobile.getText().toString();
memail= email.getText().toString();
mpassword=password.getText().toString();
mconfirm_password=confirm_password.getText().toString();
mfname.replace("" ,"%20");
msname.replace("" ,"%20");
mun.replace("" ,"%20");
mstreet.replace("" ,"%20");
msuburb.replace("" ,"%20");
mstate.replace("" ,"%20");
mpostcode.replace("" ,"%20");
mlicense.replace("" ,"%20");
mid.replace("" ,"%20");
mmobile.replace("" ,"%20");
memail.replace("" ,"%20");
mpassword.replace("" ,"%20");
mconfirm_password.replace("" ,"%20");
//
bfo = new BitmapFactory.Options();
bfo.inSampleSize = 32;
//bitmapOrg = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory() + "/" + customImage, bfo);
bfo.inScaled = true;
bfo.inPurgeable = true;
bao = new ByteArrayOutputStream();
license_img.compress(Bitmap.CompressFormat.PNG, 50, bao);
license_img = Bitmap.createScaledBitmap(license_img, 200, 200, false);
byte [] ba = bao.toByteArray();
int baa = (ba.length)/1024;
authority_img.compress(Bitmap.CompressFormat.PNG, 50, bao);
authority_img = Bitmap.createScaledBitmap(license_img, 200, 200, false);
byte [] baaa = bao.toByteArray();
int baas = (baaa.length)/1024;
face_img.compress(Bitmap.CompressFormat.PNG, 50, bao);
face_img.compress(Bitmap.CompressFormat.PNG, 50, bao);
byte [] baaap = bao.toByteArray();
int baasp = (baaap.length)/1024;
System.out.println("size of image ....."+baa);
//String image_str = Base64.encodeBytes(ba);
System.out.println(" imagedata................"+baa);
String license_img = Base64.encodeBytes(ba);
String authority_img = Base64.encodeBytes(baaa);
String face_img = Base64.encodeBytes(baaap);
System.out.println("size of image1 ................."+license_img);
System.out.println("size of image2 ................."+authority_img);
System.out.println("size of image3 ................."+face_img);
job.put("method","driver_register");
job.put("first_name",mfname);
job.put("surname",msname);
job.put("unit_no",mun);
job.put("street_name",mstreet);
job.put("suburb",msuburb);
job.put("state",mstate);
job.put("password",mpassword);
// job.put("mpassword",mconfirm_password);
job.put("post_code","mpostcode");
job.put("driving_license",mlicense);
job.put("authority_id",mid);
job.put("mobile_no",mmobile);
job.put("email",memail);
job.put("device_id",device_token);
job.put("license_pic",license_img);
job.put("audit_card_pic",authority_img);
job.put("face_pic",face_img);
// job.put("unique_id",androidId);
StringEntity se = new StringEntity(job.toString());
//HttpPost httppost = new HttpPost("http://suntechwebsolutions.com/clients/mobileapp_now/webservice.php");
HttpPost httppost = new HttpPost("http://suntechwebsolutions.com/clients/DGCapp/webservice.php");
httppost.setEntity(se);
HttpResponse response = httpclient.execute(httppost);
String tmpString = EntityUtils.toString(response.getEntity());
String data = tmpString.replace("yes","");
Log.i("response", data);
System.out.println("response "+data);
String call;
call = data;
System.out.println("print me............."+call);
JSONObject jo = new JSONObject(data);
Log.d("response", jo.toString(4));
if(jo.getString("err-code").equals("0"))
{
final AlertDialog.Builder alert = new AlertDialog.Builder(Registration.this);
alert.setTitle("Alert!!!");
alert.setMessage(jo.getString("message"));
alert.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton)
{
pDialog.dismiss();
dialog.dismiss();
startActivity(new Intent(Registration.this,LoginActivity.class));
}
});
runOnUiThread(new Runnable() {
public void run() {
alert.show();
}
});
}
else
{
final AlertDialog.Builder alert = new AlertDialog.Builder(Registration.this);
alert.setTitle("Alert !");
alert.setMessage(jo.getString("message"));
alert.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
dialog.dismiss();
}
});
runOnUiThread(new Runnable()
{
public void run()
{
pDialog.dismiss();
alert.show();
}
});
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
else
{
final AlertDialog.Builder alert = new AlertDialog.Builder(Registration.this);
alert.setTitle("Alert !");
alert.setMessage("No Internet connection ");
alert.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton)
{
dialog.dismiss();
}
});
runOnUiThread(new Runnable()
{
public void run()
{
//pDialog.dismiss();
alert.show();
}
});
}
return params;
}
@Override
protected void onPostExecute(String[] result)
{
//
pDialog.dismiss();
super.onPostExecute(result);
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(requestCode)
{
case REQUEST_CODE1:
resultCode = Activity.RESULT_OK ;
try {
// We need to recyle unused bitmaps
if (license_img != null) {
license_img.recycle();
}
InputStream stream = getContentResolver().openInputStream(
data.getData());
license_img = BitmapFactory.decodeStream(stream);
stream.close();
license_image.setImageBitmap(license_img);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
super.onActivityResult(requestCode, resultCode, data);
break;
case REQUEST_CODE2:
resultCode = Activity.RESULT_OK ;
try {
// We need to recyle unused bitmaps
if (authority_img != null) {
authority_img.recycle();
}
InputStream stream = getContentResolver().openInputStream(
data.getData());
authority_img = BitmapFactory.decodeStream(stream);
stream.close();
authority_image.setImageBitmap(authority_img);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
super.onActivityResult(requestCode, resultCode, data);
break;
case REQUEST_CODE3:
resultCode = Activity.RESULT_OK ;
try {
// We need to recyle unused bitmaps
if (face_img != null) {
face_img.recycle();
}
InputStream stream = getContentResolver().openInputStream(
data.getData());
face_img = BitmapFactory.decodeStream(stream);
stream.close();
face_image.setImageBitmap(face_img);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
super.onActivityResult(requestCode, resultCode, data);
}
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
if (cursor != null) {
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} else
return null;
}
public void decodeFile(String filePath) {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, o);
final int REQUIRED_SIZE = 70;
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
license_img = BitmapFactory.decodeFile(filePath, o2);
authority_img = BitmapFactory.decodeFile(filePath, o2);
face_img = BitmapFactory.decodeFile(filePath, o2);
license_image.setImageBitmap(license_img);
authority_image.setImageBitmap(authority_img);
face_image.setImageBitmap(face_img);
}
}
公共类注册扩展活动实现OnClickListener{
EditText fname、sname、un、街道、郊区、密码、确认密码、邮政编码、许可证、id、手机、电子邮件、图像许可证、authcard、pic;
字符串mfname、msname、mun、mstreet、msuburb、mstate、mpassword、mconfirm\u密码、mpostcode、mlicense、mid、mmobile、memail、mimagelicense、mauthcard、mpic;
按钮提交、上传许可证、上传权限、上传人脸;
文本视图rt;
私人纺纱机国家;
私有的