0

I have created an image upload using php & xampp in Android Studio, but I don't know how to set upload limit in image file?

enter image description here

My code is,

package com.moqawalat.uploadimg;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.kosalgeek.android.photoutil.CameraPhoto;
import com.kosalgeek.android.photoutil.GalleryPhoto;
import com.kosalgeek.android.photoutil.ImageBase64;
import com.kosalgeek.android.photoutil.ImageLoader;
import com.kosalgeek.genasync12.AsyncResponse;
import com.kosalgeek.genasync12.EachExceptionsHandler;
import com.kosalgeek.genasync12.PostResponseAsyncTask;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.util.HashMap;

public class MainActivity extends AppCompatActivity {

    private final String TAG = this.getClass().getName();

    ImageView ivCamera,ivGallery,ivUpload,ivImage;
    private static int RESULT_LOAD_IMG = 1;

    CameraPhoto cameraPhoto;
    GalleryPhoto galleryPhoto;
    String selectedPhoto;

    final int CAMERA_REQUEST = 13323;
    final int GALLERY_REQUEST = 22131;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        cameraPhoto = new CameraPhoto(getApplicationContext());
        galleryPhoto = new GalleryPhoto(getApplicationContext());

        ivCamera = (ImageView) findViewById(R.id.ivCamera);
        ivGallery = (ImageView) findViewById(R.id.ivGallery);
        ivUpload = (ImageView) findViewById(R.id.ivUpload);
        ivImage = (ImageView) findViewById(R.id.ivImage);


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

                try{
                    startActivityForResult(cameraPhoto.takePhotoIntent(), CAMERA_REQUEST);
                    cameraPhoto.addToGallery();
                }catch (IOException e){
                    Toast.makeText(getApplicationContext(),
                            "Something wrong while taking photos", Toast.LENGTH_SHORT).show();
                }
            }
        });

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

                startActivityForResult(galleryPhoto.openGalleryIntent(), GALLERY_REQUEST);

            }
        });

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


                try {
                    Bitmap bitmap = ImageLoader.init().from(selectedPhoto).requestSize(1024, 1024).getBitmap();
                    String encodedImage = ImageBase64.encode(bitmap);
                    Log.d(TAG, encodedImage);

                    HashMap<String, String> postData = new HashMap<String, String>();
                    postData.put("image", encodedImage);

                    PostResponseAsyncTask  task = new PostResponseAsyncTask(MainActivity.this, postData, new AsyncResponse() {


                        @Override
                        public void processFinish(String s) {



                            if(s.contains("uploaded_success")){
                                Toast.makeText(getApplicationContext(), "Image Uploaded Successfully",Toast.LENGTH_SHORT).show();
                            }
                            else{
                                Toast.makeText(getApplicationContext(), "Error while uploading...",Toast.LENGTH_SHORT).show();
                            }

                        }
                    });
                    task.execute("http://192.168.1.7/news/upload.php");
                    task.setEachExceptionsHandler(new EachExceptionsHandler() {
                        @Override
                        public void handleIOException(IOException e) {
                            Toast.makeText(getApplicationContext(), "Cannot Connect to Server.",Toast.LENGTH_SHORT).show();
                        }

                        @Override
                        public void handleMalformedURLException(MalformedURLException e) {

                            Toast.makeText(getApplicationContext(), "URL Error.",Toast.LENGTH_SHORT).show();

                        }

                        @Override
                        public void handleProtocolException(ProtocolException e) {
                            Toast.makeText(getApplicationContext(), "Protocol Error.",Toast.LENGTH_SHORT).show();
                        }

                        @Override
                        public void handleUnsupportedEncodingException(UnsupportedEncodingException e) {
                            Toast.makeText(getApplicationContext(), "Unsupported Error.",Toast.LENGTH_SHORT).show();
                        }
                    });

                } catch (FileNotFoundException e) {
                    Toast.makeText(getApplicationContext(),
                            "Something wrong while taking photos", Toast.LENGTH_SHORT).show();
                }

            }
        });
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data){


        if(resultCode == RESULT_OK){
            if(requestCode == CAMERA_REQUEST){
                String photoPath = cameraPhoto.getPhotoPath();
                selectedPhoto = photoPath;
                Bitmap bitmap = null;
                try {
                     bitmap = ImageLoader.init().from(photoPath).requestSize(512, 512).getBitmap();
                    ivImage.setImageBitmap(bitmap);
                } catch (FileNotFoundException e){
                    Toast.makeText(getApplicationContext(),
                            "Something wrong while taking photos", Toast.LENGTH_SHORT).show();
                }

            }
            else if(requestCode == GALLERY_REQUEST) {

                Uri uri = data.getData();
                galleryPhoto.setPhotoUri(uri);

                String photoPath = galleryPhoto.getPath();
                selectedPhoto = photoPath;
                try{
                    Bitmap bitmap = ImageLoader.init().from(photoPath).requestSize(512, 512).getBitmap();
                    ivImage.setImageBitmap(bitmap);
                } catch (FileNotFoundException e){
                    Toast.makeText(getApplicationContext(),
                            "Something wrong while choosing photos", Toast.LENGTH_SHORT).show();
                }

            }
        }

    }


}


php file :

<?PHP
if(isset($_POST['image'])){
    $now = DateTime::createFromFormat('U.u', microtime(true));
    $id  = $now->format('YmdHisu');

    $upload_folder = "upload/";
    $path = "$upload_folder/$id.jpg";
    $image = $_POST['image'];


    if(file_put_contents($path, base64_decode($image)) != false){
        echo "uploaded_success";
        exit;
    }
    else 
    {
    echo "upload_failed";
    exit;
    }
    }
    else{

    echo "image_not_in";
    exit;


}
?>

activity_main :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.moqawalat.uploadimg.MainActivity">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@android:drawable/ic_menu_gallery"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="39dp"
        android:layout_marginStart="39dp"
        android:layout_marginTop="53dp"
        android:id="@+id/ivGallery"
        android:layout_weight="1" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@android:drawable/ic_menu_camera"

        android:id="@+id/ivCamera"
        android:layout_marginLeft="11dp"
        android:layout_marginStart="11dp"
        android:layout_alignTop="@+id/ivGallery"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@android:drawable/ic_menu_upload_you_tube"
        android:id="@+id/ivUpload"
        android:layout_weight="1"
        android:layout_alignTop="@+id/ivGallery"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginRight="16dp"
        android:layout_marginEnd="16dp" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@mipmap/ic_launcher"
        android:id="@+id/ivImage"
        android:layout_below="@+id/ivGallery"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="161dp" />
</RelativeLayout>

I am a newbie in Android, and I don't know how to set the upload size limit in uploading image. How can I do it?

0

1 Answer 1

0
<?PHP
if(isset($_POST['image']['size'] < 5242880)){
    $now = DateTime::createFromFormat('U.u', microtime(true));
    $id  = $now->format('YmdHisu');

    $upload_folder = "upload/";
    $path = "$upload_folder/$id.jpg";
    $image = $_POST['image'];


    if(file_put_contents($path, base64_decode($image)) != false){
        echo "uploaded_success";
        exit;
    }
    else 
    {
    echo "upload_failed";
    exit;
    }
    }
    else{

    echo "image_not_in";
    exit;


}
?>

enter link description here

you want set limit to your file in php code to upload webserver you just use above code.it working perfectly.i checked it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.