Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:id="@+id/activity_main"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity">
- <ImageView
- android:layout_width="300dp"
- android:layout_height="250dp"
- android:layout_centerHorizontal="true"
- android:layout_marginTop="15dp"
- android:id="@+id/imageView"
- android:visibility="gone"/>
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_below="@id/imageView"
- android:hint="Enter a name"
- android:layout_marginTop="20dp"
- android:id="@+id/name"
- android:visibility="gone"/>
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_below="@+id/name"
- android:text="Choose image"
- android:layout_marginTop="25dp"
- android:id="@+id/chooseBn"/>
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_below="@+id/chooseBn"
- android:text="Upload image"
- android:id="@+id/uploadBn"/>
- </RelativeLayout>
- package com.mypackage.sergey.uploaddemo;
- import android.content.Intent;
- import android.graphics.Bitmap;
- import android.net.Uri;
- import android.provider.MediaStore;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.util.Base64;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.ImageView;
- import android.widget.Toast;
- import com.android.volley.AuthFailureError;
- import com.android.volley.Request;
- import com.android.volley.Response;
- import com.android.volley.VolleyError;
- import com.android.volley.toolbox.StringRequest;
- import org.json.JSONException;
- import org.json.JSONObject;
- import java.io.ByteArrayInputStream;
- import java.io.ByteArrayOutputStream;
- import java.io.IOException;
- import java.util.HashMap;
- import java.util.Map;
- public class MainActivity extends AppCompatActivity implements View.OnClickListener {
- private Button UploadBn, ChooseBn;
- private EditText NAME;
- private ImageView imgView;
- private final int IMG_REQUEST = 1;
- private Bitmap bitmap;
- private String UploadUrl = "http://mysite.com/image_upload_app/updateinfo.php";
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- UploadBn = (Button) findViewById(R.id.uploadBn);
- ChooseBn = (Button) findViewById(R.id.chooseBn);
- NAME = (EditText) findViewById(R.id.name);
- imgView = (ImageView) findViewById(R.id.imageView);
- ChooseBn.setOnClickListener(this);
- UploadBn.setOnClickListener(this);
- }
- @Override
- public void onClick(View view) {
- switch (view.getId()) {
- case R.id.chooseBn:
- selectImage();
- break;
- case R.id.uploadBn:
- uploadImage();
- break;
- }
- }
- private void selectImage() {
- Intent intent = new Intent();
- intent.setType("image/*");
- intent.setAction(Intent.ACTION_GET_CONTENT);
- startActivityForResult(intent, IMG_REQUEST);
- }
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
- if (requestCode == IMG_REQUEST && resultCode == RESULT_OK && data != null) {
- Uri path = data.getData();
- try {
- bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), path);
- imgView.setImageBitmap(bitmap);
- imgView.setVisibility(View.VISIBLE);
- NAME.setVisibility(View.VISIBLE);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- private void uploadImage() {
- StringRequest stringRequest = new StringRequest(Request.Method.POST, UploadUrl,
- new Response.Listener<String>() {
- @Override
- public void onResponse(String response) {
- try {
- JSONObject jsonObject = new JSONObject(response);
- String Response = jsonObject.getString("response");
- Toast.makeText(MainActivity.this, Response, Toast.LENGTH_LONG).show();
- imgView.setImageResource(0);
- imgView.setVisibility(View.GONE);
- NAME.setText("");
- NAME.setVisibility(View.GONE);
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- }, new Response.ErrorListener() {
- @Override
- public void onErrorResponse(VolleyError error) {
- }
- })
- {
- @Override
- protected Map<String, String> getParams() throws AuthFailureError {
- Map<String, String> params = new HashMap<>();
- params.put("name", NAME.getText().toString().trim());
- params.put("image", imageToString(bitmap));
- return params;
- }
- };
- MySingleton.getInstanse(MainActivity.this).addToRequestQueue(stringRequest);
- }
- private String imageToString(Bitmap bitmap) {
- ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
- bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
- byte[] imgBytes = byteArrayOutputStream.toByteArray();
- return Base64.encodeToString(imgBytes, Base64.DEFAULT);
- }
- }
- package com.mypackage.sergey.uploaddemo;
- import android.content.Context;
- import com.android.volley.Request;
- import com.android.volley.RequestQueue;
- import com.android.volley.toolbox.Volley;
- public class MySingleton {
- private static MySingleton mInstanse;
- private RequestQueue requestQueue;
- private static Context mCtx;
- private MySingleton(Context context) {
- mCtx = context;
- requestQueue = getRequestQueue();
- }
- private RequestQueue getRequestQueue() {
- if (requestQueue == null) {
- requestQueue = Volley.newRequestQueue(mCtx.getApplicationContext());
- }
- return requestQueue;
- }
- public static synchronized MySingleton getInstanse(Context context) {
- if (mInstanse == null) {
- mInstanse = new MySingleton(context);
- }
- return mInstanse;
- }
- public<T> void addToRequestQueue(Request<T> request) {
- getRequestQueue().add(request);
- }
- }
- <?php
- header('Content-type: bitmap; charset=utf-8');
- $user_name = "user_name";
- $user_pass = "user_pass";
- $host_name = "host_name";
- $db_name = "db_name";
- $con = mysqli_connect($host_name, $user_name, $user_pass, $db_name);
- if ($con) {
- $image = $_POST['image'];
- $name = $_POST['name'];
- $sql = "INSERT INTO imageinfo (name) values('$name')";
- $upload_path = "uploads/$name.jpg";
- if (mysqli_query($con, $sql)) {
- file_put_contents($upload_path, base64_decode($image));
- echo json_encode(array('response' => 'Image Uploaded Successfully'));
- } else {
- echo json_encode(array('response' => 'Image upload failed'));
- }
- } else {
- echo json_encode(array('response' => 'Image Uplode Failed'));
- }
- mysqli_close($con);
- ?>
- apply plugin: 'com.android.application'
- android {
- compileSdkVersion 26
- defaultConfig {
- applicationId "com.mypackage.sergey.uploaddemo"
- minSdkVersion 17
- targetSdkVersion 26
- versionCode 1
- versionName "1.0"
- testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
- }
- buildTypes {
- release {
- minifyEnabled false
- proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
- }
- }
- }
- dependencies {
- implementation fileTree(dir: 'libs', include: ['*.jar'])
- implementation 'com.android.support:appcompat-v7:26.1.0'
- implementation 'com.android.support.constraint:constraint-layout:1.0.2'
- testImplementation 'junit:junit:4.12'
- androidTestImplementation 'com.android.support.test:runner:1.0.1'
- androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
- compile 'com.android.volley:volley:1.0.0'
- }
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.mypackage.sergey.uploaddemo">
- <uses-permission android:name="android.permission.INTERNET" />
- <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
- <application
- android:allowBackup="true"
- android:icon="@mipmap/ic_launcher"
- android:label="@string/app_name"
- android:roundIcon="@mipmap/ic_launcher_round"
- android:supportsRtl="true"
- android:theme="@style/AppTheme">
- <activity android:name=".MainActivity">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- </manifest>
Add Comment
Please, Sign In to add comment