public class
insert_ads extends AppCompatActivityprivate
String[] id;private
ListView lv_cat;private
EditText title , intro , desc , seller , email , phone;private
TextView selected_cat , selected_img_txt;private
ImageView selected_img;private
String[] selected_cat_info = new String[2];@
Overridefor (
int i = 0; i < id.length ; i++) selected_cat.setText(name[position]);
}
}
);
}
private int my_requestCode_gallery = 2;
public void onBtnGalleryClick( View v )
{
Intent i = new Intent( Intent.ACTION_PICK ,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI
);
startActivityForResult( i , my_requestCode_gallery );
}
private int my_requestCode = 1;
private Bitmap my_bitmap;
private String my_final_image;
public void onBtnCameraClick( View v )
{
if( getPackageManager().hasSystemFeature( PackageManager.FEATURE_CAMERA_ANY ) )
{
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, my_requestCode);
}
else
{
Toast.makeText( getApplicationContext() ,
getString( R.string.no_camera_error ) ,
Toast.LENGTH_LONG ).show();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if( requestCode == my_requestCode && resultCode == RESULT_OK )
{
Bundle e = data.getExtras();
my_bitmap = (Bitmap) e.get( "data" );
show_captured_image();
}
else if( requestCode == my_requestCode_gallery && resultCode == RESULT_OK )
{
Uri image = data.getData();
show_internal_image( image );
}
else
{
Toast.makeText( getApplicationContext() ,
getString( R.string.get_image_error ) ,
Toast.LENGTH_LONG ).show();
}
}
public void show_captured_image()
{
try
{
AlertDialog.Builder imageLoader = new AlertDialog.Builder(this);
LayoutInflater inflater = (LayoutInflater)
this.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.full_screen_image,
(ViewGroup) findViewById(R.id.full_img_layout_root));
ImageView bigImage = (ImageView) layout.findViewById(R.id.full_img_img);
bigImage.setImageBitmap(my_bitmap);
TextView imgTitle = (TextView) layout.findViewById(R.id.full_img_title);
imgTitle.setText(R.string.captured_img_title);
imageLoader.setView(layout);
imageLoader.setCancelable(false);
imageLoader.setPositiveButton(R.string.captured_img_btn_ok,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which)
{
selected_img.setImageBitmap( my_bitmap );
selected_img_txt.setText( R.string.captured_img_is_true );
dialog.dismiss();
}
}
);
imageLoader.setNegativeButton(R.string.captured_img_btn_again,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
onBtnCameraClick(null);
}
}
);
imageLoader.setNeutralButton(R.string.btn_Back_to_home,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}
);
imageLoader.create();
imageLoader.show();
}
catch ( Exception e )
{
/*
* Log.i( "MatiMessage" , "error 1 -> " + e.toString() );
*/
}
}
public void show_internal_image( final Uri imageUri )
{
try
{
AlertDialog.Builder imageLoader = new AlertDialog.Builder(this);
LayoutInflater inflater = (LayoutInflater)
this.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.full_screen_image,
(ViewGroup) findViewById(R.id.full_img_layout_root));
ImageView bigImage = (ImageView) layout.findViewById(R.id.full_img_img);
bigImage.setImageURI(imageUri);
TextView imgTitle = (TextView) layout.findViewById(R.id.full_img_title);
imgTitle.setText(R.string.captured_img_title);
imageLoader.setView(layout);
imageLoader.setCancelable(false);
imageLoader.setPositiveButton(R.string.captured_img_btn_ok,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which)
{
selected_img.setImageURI(imageUri);
selected_img_txt.setText( R.string.captured_img_is_true );
dialog.dismiss();
}
}
);
imageLoader.setNegativeButton(R.string.captured_img_btn_again,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
onBtnGalleryClick(null);
}
}
);
imageLoader.setNeutralButton(R.string.btn_Back_to_home,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}
);
imageLoader.create();
imageLoader.show();
}
catch ( Exception e )
{
/*
* Log.i( "MatiMessage" , "error 1 -> " + e.toString() );
*/
}
}
public void onBtnInsertAdsClick( View v )
{
if( title.getText().length() > 1 )
{
if( intro.getText().length() > 1 )
{
if( desc.getText().length() > 1 )
{
if( seller.getText().length() > 1 )
{
if( email.getText().length() > 1 )
{
if( phone.getText().length() > 1 )
{
if( selected_cat.getText() != getString( R.string.insert_ads_form_sel_cat_text ) )
{
if( selected_img_txt.getText() != getString( R.string.insert_ads_form_sel_cat_text ) )
{
Bitmap image = ( (BitmapDrawable) selected_img.getDrawable() ).getBitmap();
HashMap<String , String> my_data = new HashMap<>();
my_data.put( "title" , title.getText().toString() );
my_data.put( "intro" , intro.getText().toString() );
my_data.put( "desc" , desc.getText().toString() );
my_data.put("seller", seller.getText().toString());
my_data.put("email", email.getText().toString());
my_data.put("phone", phone.getText().toString());
my_data.put("cat", selected_cat_info[0]);
UploadImage upload = new UploadImage( my_data , image , this );
upload.execute();
}
else
{
Toast.makeText( getApplicationContext() ,
getString( R.string.insert_ads_form_error_sel_img ) ,
Toast.LENGTH_LONG ).show();
}
}
else
{
Toast.makeText( getApplicationContext() ,
getString( R.string.insert_ads_form_error_sel_cat ) ,
Toast.LENGTH_LONG ).show();
}
}
else
{
phone.setHint(R.string.insert_ads_form_error_hint);
}
}
else
{
email.setHint( R.string.insert_ads_form_error_hint );
}
}
else
{
seller.setHint( R.string.insert_ads_form_error_hint );
}
}
else
{
desc.setHint( R.string.insert_ads_form_error_hint );
}
}
else
{
intro.setHint( R.string.insert_ads_form_error_hint );
}
}
else
{
title.setHint( R.string.insert_ads_form_error_hint );
}
}
private class UploadImage extends AsyncTask<Void , Void , Boolean>
{
private Context main_con;
private HashMap<String , String> main_hm;
private Bitmap main_image;
public UploadImage( HashMap<String , String> hm , Bitmap image , Context con )
{
main_hm = hm;
main_image = image;
main_con = con;
}
@Override
protected Boolean doInBackground(Void... params)
{
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
main_image.compress( Bitmap.CompressFormat.JPEG , 100 , outStream );
String encodedImage = Base64.encodeToString(
outStream.toByteArray() , Base64.DEFAULT
);
ArrayList<NameValuePair> dataToSend = new ArrayList<>();
dataToSend.add( new BasicNameValuePair("title" , main_hm.get("title") ) );
dataToSend.add( new BasicNameValuePair("intro" , main_hm.get("intro") ) );
dataToSend.add( new BasicNameValuePair("desc" , main_hm.get("desc") ) );
dataToSend.add( new BasicNameValuePair("seller" , main_hm.get("seller") ) );
dataToSend.add( new BasicNameValuePair("email" , main_hm.get("email") ) );
dataToSend.add( new BasicNameValuePair("phone" , main_hm.get("phone") ) );
dataToSend.add( new BasicNameValuePair("cat" , main_hm.get("cat") ) );
dataToSend.add(new BasicNameValuePair("image", encodedImage));
HttpParams myHttpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(myHttpParams, 1000 * 30);
HttpConnectionParams.setSoTimeout(myHttpParams, 1000 * 30);
HttpClient client = new DefaultHttpClient( myHttpParams );
HttpPost post = new HttpPost( url_insert_ads );
try {
post.setEntity( new UrlEncodedFormEntity( dataToSend ));
client.execute( post );
return true;
} catch ( Exception e ) {
/*
* Log.i( "MatiMessage" , "error in posting data -> " + e.toString() );
*/
return false;
}
}
@Override
protected void onPostExecute(Boolean result)
{
String message = "";
if( result == true )
{
message = getString( R.string.result_of_insert_message_success );
}
else
{
message = getString( R.string.result_of_insert_message_failure );
}
AlertDialog.Builder alert = new AlertDialog.Builder( main_con );
alert.setCancelable(false);
alert.setTitle(R.string.result_of_insert_title);
alert.setMessage(message);
alert.setPositiveButton(R.string.btn_Back_to_home,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}
);
alert.create();
alert.show();
}
}
public void onBtnBackClick( View v )
{
finish();
}
}
برچسب : نویسنده : استخدام کار phpco بازدید : 158