我有一个四个按钮,可以从Imageview
显示的图库中选择图像。
但问题是,当我选择一个图像时,它会填充四个Imageview
中的相同图像。
请妥善解决我的问题,我将感激你。
代码:
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Typeface;
import android.media.MediaPlayer;
import android.net.ConnectivityManager;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
public class RegisterServiceProvider extends Activity {
private Bitmap docimg,dlimage,passportdoc,textcertificate;
String iddd;
ImageView imageView;
private String userid;
String url;
private static int RESULT_LOAD_IMAGE = 1;
byte[] bitmapdata;
ImageView eimage;
String ba1,setgender;
//
private static final int PICK_IMAGE = 1;
private static final int PICK_IMAGE2 = 3;
private static final int PICK_IMAGE3 = 4;
private static final int PICK_IMAGE4= 5;
private static final int PICK_Camera_IMAGE = 2;
private ImageView imgView;
private Button upload,cancel;
private Bitmap bitmap;
private ProgressDialog dialog;
Uri imageUri;
Spinner spinner1;
MediaPlayer mp=new MediaPlayer();
/////
TextView regds,doccomp,docoftext,doudl,updlss,updpasso,select;
Button submit;
Typeface type;
CheckBox check;
EditText uname,emailid,password,confpass,companyname,companyreg,valdate,ownerdetail,fname,surname,nationality,dlno,psidno,contactno,familymemberphno,mobno,addcon;
Spinner companytype,gender;
ImageView companydoc,taxcertificate,uploaddl,passport;
String suname,semailid,spassword,sconfpass,scompanyname,scompanyreg,svaldate,sownerdetail,sfname,ssurname,snationality,sdlno,spsidno,scontactno,sfamilymemberphno,smobno,saddcon;
String scompanytype,sgender;
Button codoc,texcef,updl,updlpassport;
InputStream iss;
BitmapFactory.Options bfo;
Bitmap bitmapOrg;
ByteArrayOutputStream bao ;
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.servicregdactivity);
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
type = Typeface.createFromAsset(getAssets(),"segoeui.ttf");
regds = (TextView)findViewById(R.id.regds);
regds.setText("Service Provider Registeration");
regds.setTextSize(14);
regds.setTypeface( type);
doccomp = (TextView)findViewById(R.id.doccomp);
doccomp.setText("Upload your Company Doc.");
doccomp.setTextSize(14);
doccomp.setTypeface( type);
docoftext = (TextView)findViewById(R.id.docoftext);
docoftext.setText("Upload your TextDocument.");
docoftext.setTextSize(14);
docoftext.setTypeface( type);
doudl = (TextView)findViewById(R.id.doudl);
doudl.setText("Do you have DL ?");
doudl.setTextSize(14);
doudl.setTypeface( type);
updlss = (TextView)findViewById(R.id.updlss);
updlss.setText("Upload your DL here.");
updlss.setTextSize(14);
updlss.setTypeface( type);
updpasso = (TextView)findViewById(R.id.updpasso);
updpasso.setText("Upload your Passport here.");
updpasso.setTextSize(14);
updpasso.setTypeface( type);
select = (TextView)findViewById(R.id.select);
select.setText("Select The Term And Condition.");
select.setTextSize(12);
select.setTypeface( type);
check =(CheckBox)findViewById(R.id.checkBox1);
submit = (Button)findViewById(R.id.submit);
submit.setText("Register");
submit.setTextSize(16);
submit.setTypeface( type);
uname = (EditText)findViewById(R.id.uname);
emailid = (EditText)findViewById(R.id.emailid);
password = (EditText)findViewById(R.id.password);
confpass = (EditText)findViewById(R.id.confpass);
companyname = (EditText)findViewById(R.id.companyname);
companyreg = (EditText)findViewById(R.id.companyreg);
valdate = (EditText)findViewById(R.id.valdate);
ownerdetail = (EditText)findViewById(R.id.ownerdetail);
fname = (EditText)findViewById(R.id.fname);
surname = (EditText)findViewById(R.id.surname);
nationality = (EditText)findViewById(R.id.nationality);
dlno = (EditText)findViewById(R.id.dlno);
contactno = (EditText)findViewById(R.id.contactno);
familymemberphno = (EditText)findViewById(R.id.familymemberphno);
mobno = (EditText)findViewById(R.id.mobno);
addcon = (EditText)findViewById(R.id.addcon);
psidno = (EditText)findViewById(R.id.passportno);
check.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Perform action on clicks, depending on whether it's now checked
if (((CheckBox) v).isChecked()) {
check.setButtonDrawable(R.drawable.check_box_active) ;
} else {
check.setButtonDrawable(R.drawable.check_box_deactive) ;
}
}
});
TextView cross = (TextView)findViewById(R.id.cross);
cross.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
finish();
}
});
//spinner data
companytype = (Spinner)findViewById(R.id.companytype);
gender = (Spinner)findViewById(R.id.gender);
//System.gc();
String[] gendertype= new String[]{"Gender Type","Male","Female"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.listrow, gendertype);
gender.setAdapter(adapter);
gender.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int pos, long arg3) {
// TODO Auto-generated method stub
sgender = gender.getItemAtPosition(pos).toString();
//gender.setText(setgender);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
String[] companyname= new String[]{"Select Company","Electrical","Plumber"};
ArrayAdapter<String> adapters = new ArrayAdapter<String>(this,
R.layout.listrow, companyname);
companytype.setAdapter(adapters);
companytype.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int pos, long arg3) {
// TODO Auto-generated method stub
scompanytype = companytype.getItemAtPosition(pos).toString();
//gender.setText(setgender);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
// imageview data
companydoc = (ImageView)findViewById(R.id.companydoc);
taxcertificate = (ImageView)findViewById(R.id.taxcertificate);
uploaddl = (ImageView)findViewById(R.id.uploaddl);
passport = (ImageView)findViewById(R.id.passport);
codoc = (Button)findViewById(R.id.codoc);
texcef = (Button)findViewById(R.id.texcef);
updl = (Button)findViewById(R.id.updl);
updlpassport = (Button)findViewById(R.id.updlpassport);
codoc.setText("Upload");
codoc.setTextSize(16);
codoc.setTypeface( type);
texcef.setText("Upload");
texcef.setTextSize(16);
texcef.setTypeface( type);
updl.setText("Upload");
updl.setTextSize(16);
updl.setTypeface( type);
updlpassport.setText("Upload");
updlpassport.setTextSize(16);
updlpassport.setTypeface( type);
codoc.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
Intent gintent = new Intent();
gintent.setType("image/*");
gintent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(
Intent.createChooser(gintent, "Select Picture"),
PICK_IMAGE);
} catch (Exception e) {
// TODO: handle exception
Toast.makeText(getApplicationContext(),
e.getMessage(),
Toast.LENGTH_LONG).show();
Log.e(e.getClass().getName(), e.getMessage(), e);
}
}
});
texcef.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
Intent gintent = new Intent();
gintent.setType("image/*");
gintent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(
Intent.createChooser(gintent, "Select Picture"),
PICK_IMAGE2);
} catch (Exception e) {
// TODO: handle exception
Toast.makeText(getApplicationContext(),
e.getMessage(),
Toast.LENGTH_LONG).show();
Log.e(e.getClass().getName(), e.getMessage(), e);
}
}
});
updl.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
Intent gintent = new Intent();
gintent.setType("image/*");
gintent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(
Intent.createChooser(gintent, "Select Picture"),
PICK_IMAGE3);
} catch (Exception e) {
// TODO: handle exception
Toast.makeText(getApplicationContext(),
e.getMessage(),
Toast.LENGTH_LONG).show();
Log.e(e.getClass().getName(), e.getMessage(), e);
}
}
});
updlpassport.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
Intent gintent = new Intent();
gintent.setType("image/*");
gintent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(
Intent.createChooser(gintent, "Select Picture"),
PICK_IMAGE4);
} catch (Exception e) {
// TODO: handle exception
Toast.makeText(getApplicationContext(),
e.getMessage(),
Toast.LENGTH_LONG).show();
Log.e(e.getClass().getName(), e.getMessage(), e);
}
}
});
submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
/*if (bitmap == null) {
Toast.makeText(getApplicationContext(),
"Please select image", Toast.LENGTH_SHORT).show();
} else {
dialog = ProgressDialog.show(RegisterServiceProvider.this, "Uploading Your Data",
"Please wait...", true);
new ImageGalleryTask().execute();
}*/
new serviceProviderRegistration().execute();
}
});
}
}
private class serviceProviderRegistration extends AsyncTask<String, String, String[]> {
ProgressDialog pDialog = new ProgressDialog(RegisterServiceProvider.this);
@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
{
pDialog.setMessage("Please wait ServiceProviderRegistration.....");
runOnUiThread(new Runnable()
{
public void run()
{
pDialog.show();
}
});
/* StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
*/
JSONObject job= new JSONObject();
suname = uname.getText().toString();
semailid = emailid.getText().toString();
spassword = password.getText().toString();
sconfpass = confpass.getText().toString();
scompanyname = companyname.getText().toString();
scompanyreg = companyreg.getText().toString();
svaldate = valdate.getText().toString();
sownerdetail = ownerdetail.getText().toString();
sfname = fname.getText().toString();
ssurname = surname.getText().toString();
snationality = nationality.getText().toString();
sdlno = dlno.getText().toString();
spsidno = psidno.getText().toString();
scontactno = contactno.getText().toString();
sfamilymemberphno = familymemberphno.getText().toString();
smobno = mobno.getText().toString();
saddcon = addcon.getText().toString();
scompanytype = companytype.getSelectedItem().toString();
sgender = gender.getSelectedItem().toString();
System.out.println("print all datas.........."+suname);
suname.replace("" ,"%20");
semailid.replace("" ,"%20");
spassword.replace("" ,"%20");
sconfpass.replace("" ,"%20");
scompanyname.replace("" ,"%20");
scompanyreg.replace("" ,"%20");
svaldate.replace("" ,"%20");
sownerdetail.replace("" ,"%20");
sfname.replace("" ,"%20");
ssurname.replace("" ,"%20");
snationality.replace("" ,"%20");
sdlno.replace("" ,"%20");
spsidno.replace("" ,"%20");
scontactno.replace("" ,"%20");
sfamilymemberphno.replace("" ,"%20");
smobno.replace("" ,"%20");
saddcon.replace("" ,"%20");
scompanytype.replace("" ,"%20");
sgender.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();
docimg.compress(Bitmap.CompressFormat.PNG, 75, bao);
byte [] ba = bao.toByteArray();
int baa = (ba.length)/1024;
dlimage.compress(Bitmap.CompressFormat.PNG, 75, bao);
byte [] baaa = bao.toByteArray();
int baas = (baaa.length)/1024;
passportdoc.compress(Bitmap.CompressFormat.PNG, 75, bao);
byte [] baaap = bao.toByteArray();
int baasp = (baaap.length)/1024;
textcertificate.compress(Bitmap.CompressFormat.PNG, 75, bao);
byte [] baaat = bao.toByteArray();
int baast = (baaat.length)/1024;
System.out.println("size of image ....."+baa);
//String image_str = Base64.encodeBytes(ba);
System.out.println(" imagedata................"+baa);
String image_str_companydoc = Base64.encodeBytes(ba);
String image_str_taxcertificate = Base64.encodeBytes(baaat);
String image_str_uploaddl = Base64.encodeBytes(baaa);
String image_str_passport = Base64.encodeBytes(baaap);
System.out.println("size of image1 ................."+image_str_companydoc);
System.out.println("size of image2 ................."+image_str_taxcertificate);
System.out.println("size of image3 ................."+image_str_uploaddl);
System.out.println("size of image4 ................."+image_str_passport);
job.put("method","service_provider_signup");
job.put("username",suname);
job.put("email",semailid);
job.put("password",spassword);
job.put("company_type",sconfpass);
job.put("company_name",scompanyname);
job.put("company_registration",scompanyreg);
job.put("tax_validation",svaldate);
job.put("owner_details",sownerdetail);
job.put("first_name",sfname);
job.put("last_name",ssurname);
job.put("gender",sgender);
job.put("nationality",snationality);
job.put("id_pass_number",spsidno);
job.put("driving_lic",sdlno);
job.put("contact_no",scontactno);
job.put("other_details",sfamilymemberphno);
job.put("terms_cond","1");
job.put("mobile",smobno);
job.put("address_phone",saddcon);
job.put("company_doc",image_str_companydoc);
job.put("tax_certificate_doc",image_str_taxcertificate);
job.put("driver_lic_doc",image_str_uploaddl);
job.put("pass_id_doc",image_str_passport);
StringEntity se = new StringEntity(job.toString());
System.out.println("send items print here..............."+se);
HttpPost httppost = new HttpPost("http://sw-sol-world.com/clients/planb/webservice.php");
httppost.setEntity(se);
HttpResponse response = httpclient.execute(httppost);
String data = EntityUtils.toString(response.getEntity());
System.out.println("response "+data);
String call;
call = data;
System.out.println("print me............."+call);
JSONObject jo = new JSONObject(data);
if(jo.getString("err-code").equals("0"))
{
final AlertDialog.Builder alert = new AlertDialog.Builder(RegisterServiceProvider.this);
alert.setTitle("Service Provider Registration!!!");
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(RegisterServiceProvider.this,LoginScreen.class));
}
});
runOnUiThread(new Runnable() {
public void run() {
alert.show();
}
});
}
else
{
//params[0]="0";
final AlertDialog.Builder alert = new AlertDialog.Builder(RegisterServiceProvider.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();
}
}
return params;
}
@Override
protected void onPostExecute(String[] result)
{
super.onPostExecute(result);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.splash_screen, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.camera:
//define the file-name to save photo taken by Camera activity
String fileName = "new-photo-name.jpg";
//create parameters for Intent with filename
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, fileName);
values.put(MediaStore.Images.Media.DESCRIPTION,"Image captured by camera");
//imageUri is the current activity attribute, define and save it for later usage (also in onSaveInstanceState)
imageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
//create new Intent
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
startActivityForResult(intent, PICK_Camera_IMAGE);
return true;
case R.id.gallery:
try {
Intent gintent = new Intent();
gintent.setType("image/*");
gintent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(
Intent.createChooser(gintent, "Select Picture"),
PICK_IMAGE);
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
e.getMessage(),
Toast.LENGTH_LONG).show();
Log.e(e.getClass().getName(), e.getMessage(), e);
}
return true;
}
return false;
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Uri selectedImageUri = null;
String filePath = null;
switch (requestCode) {
case PICK_IMAGE:
if (resultCode == Activity.RESULT_OK) {
selectedImageUri = data.getData();
}
break;
case PICK_Camera_IMAGE:
if (resultCode == RESULT_OK) {
//use imageUri here to access the image
selectedImageUri = imageUri;
/*Bitmap mPic = (Bitmap) data.getExtras().get("data");
selectedImageUri = Uri.parse(MediaStore.Images.Media.insertImage(getContentResolver(), mPic, getResources().getString(R.string.app_name), Long.toString(System.currentTimeMillis())));*/
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT).show();
}
break;
}
if(selectedImageUri != null){
try {
// OI FILE Manager
String filemanagerstring = selectedImageUri.getPath();
// MEDIA GALLERY
String selectedImagePath = getPath(selectedImageUri);
if (selectedImagePath != null) {
filePath = selectedImagePath;
} else if (filemanagerstring != null) {
filePath = filemanagerstring;
} else {
Toast.makeText(getApplicationContext(), "Unknown path",
Toast.LENGTH_LONG).show();
Log.e("Bitmap", "Unknown path");
}
if (filePath != null) {
decodeFile(filePath);
} else {
bitmap = null;
}
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Internal error",
Toast.LENGTH_LONG).show();
Log.e(e.getClass().getName(), e.getMessage(), e);
}
}
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
if (cursor != null) {
// HERE YOU WILL GET A NULLPOINTER IF CURSOR IS NULL
// THIS CAN BE, IF YOU USED OI FILE MANAGER FOR PICKING THE MEDIA
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
// int columnIndex = cursor.getColumnIndex(projection[0]);
// String picturePath = cursor.getString(column_index);
// String picturePaths = cursor.getString(column_index);
// String picturePathd = cursor.getString(column_index);
// String picturePathh = cursor.getString(column_index);
//
// cursor.close();
//
//
// companydoc.setImageBitmap(BitmapFactory.decodeFile(picturePath));
// taxcertificate.setImageBitmap(BitmapFactory.decodeFile(picturePaths));
// uploaddl.setImageBitmap(BitmapFactory.decodeFile(picturePathd));
// passport.setImageBitmap(BitmapFactory.decodeFile(picturePathh));
//
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);
// The new size we want to scale to
final int REQUIRED_SIZE = 70;
// Find the correct scale value. It should be the power of 2.
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;
docimg = BitmapFactory.decodeFile(filePath, o2);
textcertificate = BitmapFactory.decodeFile(filePath, o2);
dlimage = BitmapFactory.decodeFile(filePath, o2);
passportdoc = BitmapFactory.decodeFile(filePath, o2);
companydoc.setImageBitmap(docimg);
taxcertificate.setImageBitmap(textcertificate);
uploaddl.setImageBitmap(dlimage);
passport.setImageBitmap(passportdoc);
}
}