Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to upload croped image to php in ipicker #9

Open
moosin opened this issue Nov 19, 2017 · 7 comments
Open

How to upload croped image to php in ipicker #9

moosin opened this issue Nov 19, 2017 · 7 comments

Comments

@moosin
Copy link

moosin commented Nov 19, 2017

Hi, Pleas help me to upload cropped image in ipicker using php
i tried a lot its not working
pls some one help

Thank you

@liuguangqiang
Copy link
Owner

liuguangqiang commented Nov 20, 2017 via email

@moosin
Copy link
Author

moosin commented Nov 20, 2017

@liuguangqiang Thanks for the replay,

## Yes i'm using php to upload

i'm trying to convert the croped image to bitmap and bitmap to base64
but i can't make it becase the image loading in adpter

i'm following this "https://androidjson.com/android-upload-image-server-using-php-mysql/"


  bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);

                imageView.setImageBitmap(bitmap);
ByteArrayOutputStream byteArrayOutputStreamObject ;

        byteArrayOutputStreamObject = new ByteArrayOutputStream();

        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStreamObject);

        byte[] byteArrayVar = byteArrayOutputStreamObject.toByteArray();

        final String ConvertImage = Base64.encodeToString(byteArrayVar, Base64.DEFAULT);

Pls help

@liuguangqiang
Copy link
Owner

What’s error message do you found? If you would like to upload a bitmap to remote server, you can use a third-part library like the ‘Retrofit’.
Are you sure the PHP API is correct?

@moosin
Copy link
Author

moosin commented Nov 20, 2017

php is correct only
I can't able to convert cropped image to base64 encodeToString
Because all this codes are in Mainactivity but the image is in adapter

Here is the actual code its working well,
When i integrate this with IPicker its not working

Image selection

SelectImageGallery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent = new Intent();

                intent.setType("image/*");

                intent.setAction(Intent.ACTION_GET_CONTENT);

                startActivityForResult(Intent.createChooser(intent, "Select Image From Gallery"), 1);

            }
        });

@Override
    protected void onActivityResult(int RC, int RQC, Intent I) {

        super.onActivityResult(RC, RQC, I);

        if (RC == 1 && RQC == RESULT_OK && I != null && I.getData() != null) {

           Uri uri = I.getData();

            try {

                bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);

                imageView.setImageBitmap(bitmap);

            } catch (IOException e) {

                e.printStackTrace();
            }
        }
    }

Upload Click

 ```

UploadImageServer.setOnClickListener(new View.OnClickListener() {
@OverRide
public void onClick(View view) {

            GetImageNameEditText = imageName.getText().toString();

            ImageUploadToServerFunction();

        }
    });
}


**the main function**


public void ImageUploadToServerFunction(){

    ByteArrayOutputStream byteArrayOutputStreamObject ;

    byteArrayOutputStreamObject = new ByteArrayOutputStream();

    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStreamObject);

    byte[] byteArrayVar = byteArrayOutputStreamObject.toByteArray();

    final String ConvertImage = Base64.encodeToString(byteArrayVar, Base64.DEFAULT);

    class AsyncTaskUploadClass extends AsyncTask<Void,Void,String> {

        @Override
        protected void onPreExecute() {

            super.onPreExecute();

            progressDialog = ProgressDialog.show(MainActivity.this,"Image is Uploading","Please Wait",false,false);
        }

        @Override
        protected void onPostExecute(String string1) {

            super.onPostExecute(string1);

            // Dismiss the progress dialog after done uploading.
            progressDialog.dismiss();

            // Printing uploading success message coming from server on android app.
            Toast.makeText(MainActivity.this,string1,Toast.LENGTH_LONG).show();

            // Setting image as transparent after done uploading.
            imageView.setImageResource(android.R.color.transparent);


        }

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

            ImageProcessClass imageProcessClass = new ImageProcessClass();

            HashMap<String,String> HashMapParams = new HashMap<String,String>();

            HashMapParams.put(ImageName, GetImageNameEditText);

            HashMapParams.put(ImagePath, ConvertImage);

            String FinalData = imageProcessClass.ImageHttpRequest(ServerUploadPath, HashMapParams);

            return FinalData;
        }
    }
    AsyncTaskUploadClass AsyncTaskUploadClassOBJ = new AsyncTaskUploadClass();

    AsyncTaskUploadClassOBJ.execute();
}

public class ImageProcessClass{

    public String ImageHttpRequest(String requestURL,HashMap<String, String> PData) {

        StringBuilder stringBuilder = new StringBuilder();

        try {

            URL url;
            HttpURLConnection httpURLConnectionObject ;
            OutputStream OutPutStream;
            BufferedWriter bufferedWriterObject ;
            BufferedReader bufferedReaderObject ;
            int RC ;

            url = new URL(requestURL);

            httpURLConnectionObject = (HttpURLConnection) url.openConnection();

            httpURLConnectionObject.setReadTimeout(19000);

            httpURLConnectionObject.setConnectTimeout(19000);

            httpURLConnectionObject.setRequestMethod("POST");

            httpURLConnectionObject.setDoInput(true);

            httpURLConnectionObject.setDoOutput(true);

            OutPutStream = httpURLConnectionObject.getOutputStream();

            bufferedWriterObject = new BufferedWriter(

                    new OutputStreamWriter(OutPutStream, "UTF-8"));

            bufferedWriterObject.write(bufferedWriterDataFN(PData));

            bufferedWriterObject.flush();

            bufferedWriterObject.close();

            OutPutStream.close();

            RC = httpURLConnectionObject.getResponseCode();

            if (RC == HttpsURLConnection.HTTP_OK) {

                bufferedReaderObject = new BufferedReader(new InputStreamReader(httpURLConnectionObject.getInputStream()));

                stringBuilder = new StringBuilder();

                String RC2;

                while ((RC2 = bufferedReaderObject.readLine()) != null){

                    stringBuilder.append(RC2);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }

    private String bufferedWriterDataFN(HashMap<String, String> HashMapParams) throws UnsupportedEncodingException {

        StringBuilder stringBuilderObject;

        stringBuilderObject = new StringBuilder();

        for (Map.Entry<String, String> KEY : HashMapParams.entrySet()) {

            if (check)

                check = false;
            else
                stringBuilderObject.append("&");

            stringBuilderObject.append(URLEncoder.encode(KEY.getKey(), "UTF-8"));

            stringBuilderObject.append("=");

            stringBuilderObject.append(URLEncoder.encode(KEY.getValue(), "UTF-8"));
        }

        return stringBuilderObject.toString();
    }

}

@liuguangqiang
Copy link
Owner

liuguangqiang commented Nov 20, 2017

I recommend you to upload images by Retrofit.
https://github.com/square/retrofit

@moosin
Copy link
Author

moosin commented Nov 20, 2017

Ok thanks i'll try

@moosin
Copy link
Author

moosin commented Dec 1, 2017

Hi i need one last help

How do i use IPicker in NoActionBar theme

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".activity.EditProfile">
    <android.support.v7.widget.Toolbar
        android:id="@+id/my_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:elevation="4dp"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
    <Button
        android:id="@+id/open_picker"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="选择照片" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_photos"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1" />

</LinearLayout>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
       <!-- Customize your theme here. -->
       <item name="colorPrimary">@color/colorPrimary</item>
       <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
       <item name="colorAccent">@color/colorAccent</item>
       <item name="android:statusBarColor">@color/colorPrimary</item>
       <item name="android:windowLightStatusBar">true</item>
       <item name="android:actionBarStyle">@color/colorAccent</item>
       <item name="colorControlNormal">@color/colorAccent</item>
   </style>

pls help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants