ChatSecureAndroid icon indicating copy to clipboard operation
ChatSecureAndroid copied to clipboard

set Avatar Accomplished!!! yes i did it

Open ghost opened this issue 9 years ago • 9 comments

i managed to set an avatar using your existing code,of course i created my own activity and stuff... am open to share my code to anyone who wants to develop CS!!! just ask a.s.a.p its open source right?

Regards cb

ghost avatar Mar 28 '15 07:03 ghost

Yup! We'd be happy to incorporate your code. Just send a pull request.

n8fr8 avatar Mar 28 '15 15:03 n8fr8

I ty for the request

Joseph2106 avatar Mar 29 '15 03:03 Joseph2106

Great! When i get time i'll send a pull request... Mean while Here is the work around:

Its quite simple

  • From the NewChatActivity i passed an intent with values of the current username ID and the password to my new Profile activity
  • in The Profile activity i created a new connection with the intent values, picked a photo from the gallery load the vcard then set and save it.

This worked for me Thanks so much!

ghost avatar Mar 29 '15 11:03 ghost

i must say your Code is pretty abstract....

ghost avatar Mar 29 '15 15:03 ghost

If your code is not tidy enough for a PR, please just share the github fork and someone will refactor it and send a PR soon enough. :)

WhyNotHugo avatar Apr 29 '15 09:04 WhyNotHugo

Am so Sorry my Code only allows one type of Account (i.e one Xmpp Connection)
Like i had mentioned Above, i pulled the The Username and Password of Active Account via a simple cursor,Since i knew the Server to connect to (its one Account remember) i create a new Xmpp connection using these values... From there on wards i pick a photo,convert it to byteArray, encode it to base64 Then load my vCard using the connection>>upload my photo >>> save my vCard.

It works only if you have one Account logged in.

i've been trying to figure out how i can make it work with multiple accounts till now nothing yet but if you got one xmpp server, try it!!! 100% working

I didn't break down everything to scratch coz i assume you have good Knowledge on Android more than me....

If i was by any means unclear, or not vivid please point out anything am ready to help anyone use my way. people having Java problems like me are welcome to ask for the code, i can simply Comment it here if necessary, otherwise i'll Hit for a PR when i get it working with many accounts

Best.

ghost avatar Apr 30 '15 01:04 ghost

Dear @Cedybt can you please share me your code with the avatar fix? I'm trying to build an app with a single account login. Thanks.

robertcprivett avatar May 06 '15 13:05 robertcprivett

Sorry for the delay, things in Africa are pretty crazzy here... lol

@robertcprivett That's great with one account there many ways you can set the avatar

Here's what you gonna do. in this example am using android SqliteDatabase to save the username and password values

  1. you can either save the username + password in some sort of shared Preferences or simply a database (These values can be captured from the AccountActivity.java where the user signs in or registers) Simply add these few lines of code immediately after the user presses the mBtnSignIn button via its setOnClickListener this is AccountActivity class right?

             mBtnSignIn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
    
            //the password and Username EditText
            String password = mEditPass.getText().toString();
            String username = mEditUserAccount.getText().toString();
    
            try {
                sql = openOrCreateDatabase("DatabaseName", MODE_PRIVATE, null);
    
                /* Creates two Tables in the Database. for username and password */
                sql.execSQL("CREATE TABLE IF NOT EXISTS "
                  + TableName
                  + " (User VARCHAR,Pw VARCHAR);");
    
                /* Inserts values into the Tables*/
                sql.execSQL("INSERT INTO "
                  + TableName
                  + " (User,Pw)"
                  + " VALUES ('"+ username + "','" + password + "');"); 
                  }
            finally {
                sql.close();
              }
          .........other basic functionality of CS......... 
             });
    
  2. the profile Activity(this is an activity you'll have to make yourself) then from there onwards in this profile activity create a new xmpp connection with the values of the username and password saved from above, load VCard from this xmpp connection, set avatar then save VCard.(don't forget to disconnect connection)

Wait a sec,not so fast!!! First select a photo or capture it then crop it to minimize its size(coz its only set to a max of 2MB) after crop save it to some directory. e.g Environment.getExternalStorageDirectory() + "/avatar.jpg"

to be safe i prefer doing my avatar setting in an AsyncTask its more professional

     class MyTask extends AsyncTask<Void, Void, Void> {
           private final Void Result = null;
           String Username;
          String password;
          ProgressDialog pd;

       @Override
       protected void onPreExecute() {
         super.onPreExecute();
          pd = new ProgressDialog(Profile.this);
          pd.setMessage("Uploading...");
          pd.setCancelable( false);
          pd.setCanceledOnTouchOutside( false);
          pd.show();
       }

       @Override
       protected Void doInBackground(Void... params) {

           SQLiteDatabase sql = openOrCreateDatabase("DatabaseName", MODE_PRIVATE, null);
           Cursor c = sql.rawQuery("SELECT * FROM " + TableName , null);

           int ColumnUser = c.getColumnIndex("User");
           int ColumnPw = c.getColumnIndex("Pw");
           c.moveToFirst();
           if (c != null) {
            // Loop through all Results
            do {
             password = c.getString(ColumnPw);
             Username = c.getString(ColumnUser);

                 ConnectionConfiguration ccg = new ConnectionConfiguration("YourServer.com",5222);

                 XMPPConnection conn = new XMPPConnection(ccg);
                 try {
                  conn.connect();
              } catch (XMPPException e1) {
                  // TODO Auto-generated catch block
                  e1.printStackTrace();
              }

                 try {
                     //Login user
                     conn.login(Username,password);
                 } catch (XMPPException e1) {
                     // TODO Auto-generated catch block
                     e1.printStackTrace();
                 }
                  //the photo we saved when we cropped remember?!?
                 String path = Environment.getExternalStorageDirectory() + "/avatar.jpg";
                 Bitmap bm = BitmapFactory.decodeFile(path);
                 ByteArrayOutputStream baos = new ByteArrayOutputStream();
                 bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object   
                 byte[] b = baos.toByteArray(); 

                 //encode it to Base64 for it to be saved by our xmpp server
                 String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);

                 VCard vcard = new VCard();
                 try {
                  //aSmack's load own vCard
                  vcard.load(conn);

              } catch (XMPPException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
                 //b is the photo but encoded to Base64 right?
                 vcard.setAvatar(b, "b" + b);
                 try {
                  vcard.save(conn);
              } catch (XMPPException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
                 System.out.println("image uploaded successfully");
                //Don't forget to disconnect connection
                conn.disconnect();
           }while(c.moveToNext());
            sql.close();
           c.close();
           }
      return null;

       }

       protected void onPostExecute(Void result) {
         super.onPostExecute(result);
         if (pd != null)
         {
            pd.dismiss();
            Toast.makeText( getApplicationContext(), "Profile Pic Changed", Toast.LENGTH_SHORT).show();

         }

}

thats it Bro

This is the simplest i could come up with,there are many ways you can archive this... please note that the username and password are the Most important here,So which ever way you'll get them be sure they are all correct. in my example i used an SQLite database,try your own work arounds to it, twerk it a little bit, Create something cool with it,Send a PR,Save the CS world. lol

Any more Questions am here...

Thanks Best Ceddy

ghost avatar May 10 '15 12:05 ghost

Sorry i'd just closed it

ghost avatar May 10 '15 13:05 ghost