Guest User

Untitled

a guest
Mar 21st, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.41 KB | None | 0 0
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:id="@+id/activity_main"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".MainActivity">
  8.  
  9. <ImageView
  10. android:layout_width="300dp"
  11. android:layout_height="250dp"
  12. android:layout_centerHorizontal="true"
  13. android:layout_marginTop="15dp"
  14. android:id="@+id/imageView"
  15. android:visibility="gone"/>
  16.  
  17. <EditText
  18. android:layout_width="match_parent"
  19. android:layout_height="wrap_content"
  20. android:layout_below="@id/imageView"
  21. android:hint="Enter a name"
  22. android:layout_marginTop="20dp"
  23. android:id="@+id/name"
  24. android:visibility="gone"/>
  25.  
  26. <Button
  27. android:layout_width="match_parent"
  28. android:layout_height="wrap_content"
  29. android:layout_below="@+id/name"
  30. android:text="Choose image"
  31. android:layout_marginTop="25dp"
  32. android:id="@+id/chooseBn"/>
  33.  
  34. <Button
  35. android:layout_width="match_parent"
  36. android:layout_height="wrap_content"
  37. android:layout_below="@+id/chooseBn"
  38. android:text="Upload image"
  39. android:id="@+id/uploadBn"/>
  40.  
  41. </RelativeLayout>
  42.  
  43. package com.mypackage.sergey.uploaddemo;
  44.  
  45. import android.content.Intent;
  46. import android.graphics.Bitmap;
  47. import android.net.Uri;
  48. import android.provider.MediaStore;
  49. import android.support.v7.app.AppCompatActivity;
  50. import android.os.Bundle;
  51. import android.util.Base64;
  52. import android.view.View;
  53. import android.widget.Button;
  54. import android.widget.EditText;
  55. import android.widget.ImageView;
  56. import android.widget.Toast;
  57.  
  58. import com.android.volley.AuthFailureError;
  59. import com.android.volley.Request;
  60. import com.android.volley.Response;
  61. import com.android.volley.VolleyError;
  62. import com.android.volley.toolbox.StringRequest;
  63.  
  64. import org.json.JSONException;
  65. import org.json.JSONObject;
  66.  
  67. import java.io.ByteArrayInputStream;
  68. import java.io.ByteArrayOutputStream;
  69. import java.io.IOException;
  70. import java.util.HashMap;
  71. import java.util.Map;
  72.  
  73. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  74. private Button UploadBn, ChooseBn;
  75. private EditText NAME;
  76. private ImageView imgView;
  77. private final int IMG_REQUEST = 1;
  78. private Bitmap bitmap;
  79. private String UploadUrl = "http://mysite.com/image_upload_app/updateinfo.php";
  80.  
  81. @Override
  82. protected void onCreate(Bundle savedInstanceState) {
  83. super.onCreate(savedInstanceState);
  84. setContentView(R.layout.activity_main);
  85.  
  86. UploadBn = (Button) findViewById(R.id.uploadBn);
  87. ChooseBn = (Button) findViewById(R.id.chooseBn);
  88. NAME = (EditText) findViewById(R.id.name);
  89. imgView = (ImageView) findViewById(R.id.imageView);
  90. ChooseBn.setOnClickListener(this);
  91. UploadBn.setOnClickListener(this);
  92. }
  93.  
  94. @Override
  95. public void onClick(View view) {
  96. switch (view.getId()) {
  97. case R.id.chooseBn:
  98. selectImage();
  99. break;
  100. case R.id.uploadBn:
  101. uploadImage();
  102. break;
  103. }
  104. }
  105.  
  106. private void selectImage() {
  107. Intent intent = new Intent();
  108. intent.setType("image/*");
  109. intent.setAction(Intent.ACTION_GET_CONTENT);
  110. startActivityForResult(intent, IMG_REQUEST);
  111. }
  112.  
  113. @Override
  114. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  115. super.onActivityResult(requestCode, resultCode, data);
  116.  
  117. if (requestCode == IMG_REQUEST && resultCode == RESULT_OK && data != null) {
  118. Uri path = data.getData();
  119. try {
  120. bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), path);
  121. imgView.setImageBitmap(bitmap);
  122. imgView.setVisibility(View.VISIBLE);
  123. NAME.setVisibility(View.VISIBLE);
  124. } catch (IOException e) {
  125. e.printStackTrace();
  126. }
  127. }
  128. }
  129.  
  130. private void uploadImage() {
  131. StringRequest stringRequest = new StringRequest(Request.Method.POST, UploadUrl,
  132. new Response.Listener<String>() {
  133. @Override
  134. public void onResponse(String response) {
  135. try {
  136. JSONObject jsonObject = new JSONObject(response);
  137. String Response = jsonObject.getString("response");
  138. Toast.makeText(MainActivity.this, Response, Toast.LENGTH_LONG).show();
  139. imgView.setImageResource(0);
  140. imgView.setVisibility(View.GONE);
  141. NAME.setText("");
  142. NAME.setVisibility(View.GONE);
  143. } catch (JSONException e) {
  144. e.printStackTrace();
  145. }
  146. }
  147. }, new Response.ErrorListener() {
  148. @Override
  149. public void onErrorResponse(VolleyError error) {
  150.  
  151. }
  152. })
  153.  
  154. {
  155. @Override
  156. protected Map<String, String> getParams() throws AuthFailureError {
  157. Map<String, String> params = new HashMap<>();
  158. params.put("name", NAME.getText().toString().trim());
  159. params.put("image", imageToString(bitmap));
  160.  
  161. return params;
  162. }
  163. };
  164. MySingleton.getInstanse(MainActivity.this).addToRequestQueue(stringRequest);
  165. }
  166.  
  167. private String imageToString(Bitmap bitmap) {
  168. ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
  169. bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
  170. byte[] imgBytes = byteArrayOutputStream.toByteArray();
  171.  
  172. return Base64.encodeToString(imgBytes, Base64.DEFAULT);
  173. }
  174. }
  175.  
  176. package com.mypackage.sergey.uploaddemo;
  177.  
  178. import android.content.Context;
  179.  
  180. import com.android.volley.Request;
  181. import com.android.volley.RequestQueue;
  182. import com.android.volley.toolbox.Volley;
  183.  
  184. public class MySingleton {
  185. private static MySingleton mInstanse;
  186. private RequestQueue requestQueue;
  187. private static Context mCtx;
  188.  
  189. private MySingleton(Context context) {
  190. mCtx = context;
  191. requestQueue = getRequestQueue();
  192. }
  193.  
  194. private RequestQueue getRequestQueue() {
  195. if (requestQueue == null) {
  196. requestQueue = Volley.newRequestQueue(mCtx.getApplicationContext());
  197. }
  198. return requestQueue;
  199. }
  200.  
  201. public static synchronized MySingleton getInstanse(Context context) {
  202. if (mInstanse == null) {
  203. mInstanse = new MySingleton(context);
  204. }
  205. return mInstanse;
  206. }
  207.  
  208. public<T> void addToRequestQueue(Request<T> request) {
  209. getRequestQueue().add(request);
  210. }
  211. }
  212.  
  213. <?php
  214.  
  215. header('Content-type: bitmap; charset=utf-8');
  216.  
  217. $user_name = "user_name";
  218. $user_pass = "user_pass";
  219. $host_name = "host_name";
  220. $db_name = "db_name";
  221.  
  222. $con = mysqli_connect($host_name, $user_name, $user_pass, $db_name);
  223.  
  224. if ($con) {
  225. $image = $_POST['image'];
  226. $name = $_POST['name'];
  227. $sql = "INSERT INTO imageinfo (name) values('$name')";
  228. $upload_path = "uploads/$name.jpg";
  229.  
  230. if (mysqli_query($con, $sql)) {
  231. file_put_contents($upload_path, base64_decode($image));
  232. echo json_encode(array('response' => 'Image Uploaded Successfully'));
  233. } else {
  234. echo json_encode(array('response' => 'Image upload failed'));
  235. }
  236. } else {
  237. echo json_encode(array('response' => 'Image Uplode Failed'));
  238. }
  239. mysqli_close($con);
  240. ?>
  241.  
  242. apply plugin: 'com.android.application'
  243.  
  244. android {
  245. compileSdkVersion 26
  246. defaultConfig {
  247. applicationId "com.mypackage.sergey.uploaddemo"
  248. minSdkVersion 17
  249. targetSdkVersion 26
  250. versionCode 1
  251. versionName "1.0"
  252. testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
  253. }
  254. buildTypes {
  255. release {
  256. minifyEnabled false
  257. proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
  258. }
  259. }
  260. }
  261.  
  262. dependencies {
  263. implementation fileTree(dir: 'libs', include: ['*.jar'])
  264. implementation 'com.android.support:appcompat-v7:26.1.0'
  265. implementation 'com.android.support.constraint:constraint-layout:1.0.2'
  266. testImplementation 'junit:junit:4.12'
  267. androidTestImplementation 'com.android.support.test:runner:1.0.1'
  268. androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
  269. compile 'com.android.volley:volley:1.0.0'
  270. }
  271.  
  272. <?xml version="1.0" encoding="utf-8"?>
  273. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  274. package="com.mypackage.sergey.uploaddemo">
  275. <uses-permission android:name="android.permission.INTERNET" />
  276. <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
  277. <application
  278. android:allowBackup="true"
  279. android:icon="@mipmap/ic_launcher"
  280. android:label="@string/app_name"
  281. android:roundIcon="@mipmap/ic_launcher_round"
  282. android:supportsRtl="true"
  283. android:theme="@style/AppTheme">
  284. <activity android:name=".MainActivity">
  285. <intent-filter>
  286. <action android:name="android.intent.action.MAIN" />
  287.  
  288. <category android:name="android.intent.category.LAUNCHER" />
  289. </intent-filter>
  290. </activity>
  291. </application>
  292.  
  293. </manifest>
Add Comment
Please, Sign In to add comment