Monday, 26 October 2015

Upload Selected Image On Server

Hi!

  Some of our Android developers are struggle with Images, for Upload on Server & Download from the server.

Normally we can Upload & Download Images from the server, But the thing is, the Base64 is the only format to help us. In this blog i have explain how to do this, and i have reduce your working time, within 3 Mins you can do this, Needn't to R&D for this i have done and attached my Raj_Img_Upload.aar file for you. Just download the file and fallow the below.

As usual we can use Json format to Upload & Download the image. But the method should be "POST".

Link to Download My .aar file  download this .aar file and add in your project, That's all.


My Activity Class :

public class MainActivity extends ActionBarActivity {


// Image Button to set the selected Image
    ImageButton img_btn_prf_img;

// My .AAR file to handle all your risk part.
    Raj_Image_Handler raj_Handler;
// TextView to show your converted format (Image into Base64)
    TextView base_64_txt;
// To open up Image Gallery 
    private static final int GALLERY = 1;

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        img_btn_prf_img = (ImageButton) findViewById(R.id.img_btn_prf_img);
        base_64_txt = (TextView) findViewById(R.id.base_64_txt);
// Pass your ImageButton & Activity to my .aar
        raj_Handler = new Raj_Image_Handler(getApplicationContext(), img_btn_prf_img);

        img_btn_prf_img.setOnClickListener(new View.OnClickListener() {
            @Override            public void onClick(View view) {
// Get Default Bitmap date what i have set
                Bitmap Image = raj_Handler.get_Image();
                img_btn_prf_img.setImageBitmap(null);
                if (Image != null)
                    Image.recycle();
                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(
                        Intent.createChooser(intent, "Select Picture"), GALLERY);
            }
        });
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == GALLERY && resultCode != 0) {
            Uri mImageUri = data.getData();
// Pass the selected image Uri to my .aar
String enc_img = raj_Handler.set_Image_From_Gallery(mImageUri);
// get the Base64 format of your selected Image
            base_64_txt.setText(enc_img);
        }
    }
}









Convert From Base64 to Image :

If you want to convert from Base64 to Image please do the fallowing

//ImageButton for check the output 
ImageButton imb_set;

imb_set = (ImageButton) findViewById(R.id.imb_set);

// Action to convert Base64 to Image
imb_set.setOnClickListener(new View.OnClickListener() {
    @Override    public void onClick(View view) {

// Single line parameter (ImageButton & Base64 output)
        raj_Handler.set_Image_Base_64(imb_set,your_base64_string);
    }
});


Sample APK: Download APK

Out Put:



Enjoy!..,

Thank You!

Have A Happy Day..,

No comments:

Post a Comment