Friday, 9 June 2017

how to load url image from the network by asych task andorid code

Step1. The LoadImageTask class under the      com.example.task.LoadImageTask.java
import android.graphics.Bitmap;
        import android.graphics.BitmapFactory;
        import android.os.AsyncTask;
        import android.util.Log;

        import java.io.IOException;
        import java.io.InputStream;
        import java.net.URL;

/**
 * Created by amit rawat and  email:amitrawatamit8@gmail.com.
 */
public class LoadImageTask extends AsyncTask<String, Void, Bitmap> {
    private Listener mListener;

    public LoadImageTask(Listener listener) {
        mListener = listener;
    }

    @Override
    protected Bitmap doInBackground(String... args) {
        String arg = args[0];
        Log.e("arg""" + arg);
        try {
            return BitmapFactory.decodeStream((InputStream) new URL(args[0]).getContent());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        if (bitmap != null) {
            mListener.onImageLoaded(bitmap);
        } else {
            mListener.onError();
        }
    }

    public interface Listener {
        void onImageLoaded(Bitmap bitmap);

        void onError();
    }
}
Step2. The MainActivity.java
import com.example.task.LoadImageTask;

/**
 * Created by amit rawat and  email:amitrawatamit8@gmail.com.
 */
public class MainActivity extends AppCompatActivity
        implements LoadImageTask.Listener {
    ImageView Vendor_Login_image;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.vendor_activity_navigationdrawer);
        Vendor_Login_image = (ImageView) findViewById(R.id.Vendor_login_image);
        new LoadImageTask(MainNaviScreen.this).execute("image url path");//
    }


    @Override
    public void onImageLoaded(Bitmap bitmap) {
        Vendor_Login_image.setImageBitmap(bitmap);//if image found
    }

    @Override
    public void onError() {
        Glide.with(getApplicationContext()).load(R.drawable.ab_userwhite).crossFade(1000)
                .into(Vendor_Login_image);//no image found and r.drawable.ab_userwhite is the default image
    }
}

Wednesday, 10 May 2017

Convert string to MD5 in android

Converting the string into MD5 with this simple class.
import java.security.MessageDigest;
/** * Created by amit rawat on 5/10/2017. */public class MD5 {
    public static String getMd5Key(String password) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(password.getBytes());
            byte byteData[] = md.digest();
            //convert the byte to hex format
             StringBuffer sb = new StringBuffer();
            for (int i = 0; i < byteData.length; i++) {
             sb.append(Integer.toString((byteData[i] & 0xff) + 0x10016).substring(1));
            }
            System.out.println("Digest(in hex format):: " + sb.toString());
            //convert the byte to hex format 
            StringBuffer hexString = new StringBuffer();
            for (int i = 0; i < byteData.length; i++) {
                String hex = Integer.toHexString(0xff & byteData[i]);
                if (hex.length() == 1) hexString.append('0');
                hexString.append(hex);
            }
            System.out.println("Digest(in hex format):: " + hexString.toString());
            return hexString.toString();
        } catch (Exception e) {
               }
        return "";
    }
}

Tuesday, 2 May 2017

Passing ArrayList through Intent to one Activity to another Activity in Android

Step1: Firstactivity.java set the array list in intent
ArrayList<String> images new ArrayList<>();
images.add("amit");
Intent i = new Intent(Firstactivity.this, Secondactivity.class);
i.putExtra("key"images);
startActivity(i);

STEP 2. Secondactivity.class receiving the array from intent
ArrayList<String> imageslist = (ArrayList<String>) getIntent().getSerializableExtra("key");

Tuesday, 21 February 2017

Converting the bitmap to base64 string and base64 string to bitmap image in android

/* bitmap code use in main activity*/
Bitmap profileimage = BitmapFactory.decodeFile(you image path here);
String basestring=convert(bitmapConvert)

/*converting the base64 string to bitmap */
public Bitmap convert(String base64Str) throws IllegalArgumentException {
        byte[] decodedBytes = Base64.decode(
                base64Str.substring(base64Str.indexOf(",") + 1),
                Base64.DEFAULT        );
        return BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.length);
    }
  

/*converting the bitmap image to string in base64*/ 
 public String convert(Bitmap bitmap) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG70, outputStream);
        return Base64.encodeToString(outputStream.toByteArray(), Base64.DEFAULT);
    }