Envoie de fichier PDF d'une application android à un serveur
Dans ce tutoriel on va voir comment envoyer un fichier pdf à partir d'une activité android vers un serveur distant. Pour se faire nous allons besoin d'un logiciel qui contient un serveur web apache, comme XXAMP, EasyPHP, WAMP. et de l'environnement de développement Android Studio.
on va débuter par développer le code du script php qui sert comme web service. ce fichier doit être héberger dans le dossier htdocs pour XAMPP et le dossier www pour EasyPHP et WAMP.
sendfile.phpAjoutez les permissions suivantes :
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.INTERNET" />
<?php
header('Access-Control-Allow-Origin: *');
error_reporting(E_ALL);
if(isset($_POST['ImageName'])){
$imgname = $_POST['ImageName'];
$imsrc = str_replace(' ','+',$_POST['base64']);
$imsrc = base64_decode($imsrc);
$fp = fopen($imgname, 'w');
fwrite($fp, $imsrc);
if(fclose($fp)){
echo "Image uploaded";
}else{
echo "Error uploading image";
}
}
?>
Par la suite dans notre projet Android Studio, créer cette interface dans votre layout :
activity_main.xml
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="@+id/Imageprev"
android:layout_width="match_parent"
android:layout_height="400dp" />
<Button
android:id="@+id/cpic"
android:text="SELECTION PDF"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/Imageprev" />
<Button
android:id="@+id/up"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Envoie PDF"
android:layout_below="@+id/cpic"
android:layout_centerInParent="true" />
</RelativeLayout>
Par la suite, il faut créer une classe java dans le package principal du projet. On nomme cette classe
FilePath.java
package com.lahiani.houssem.fileupload; import android.content.ContentUris; import android.content.Context; import android.database.Cursor; import android.net.Uri; import android.os.Build; import android.os.Environment; import android.provider.DocumentsContract; import android.provider.MediaStore; public class FilePath { /** * Method for return file path of Gallery image * * @param context * @param uri * @return path of the selected image file from gallery */ public static String getPath(final Context context, final Uri uri) { //check here to KITKAT or new version final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT; // DocumentProvider if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) { // ExternalStorageProvider if (isExternalStorageDocument(uri)) { final String docId = DocumentsContract.getDocumentId(uri); final String[] split = docId.split(":"); final String type = split[0]; if ("primary".equalsIgnoreCase(type)) { return Environment.getExternalStorageDirectory() + "/" + split[1]; } } //DownloadsProvider else if (isDownloadsDocument(uri)) { final String id = DocumentsContract.getDocumentId(uri); final Uri contentUri = ContentUris.withAppendedId( Uri.parse("content://downloads/public_downloads"), Long.valueOf(id)); return getDataColumn(context, contentUri, null, null); } // MediaProvider else if (isMediaDocument(uri)) { final String docId = DocumentsContract.getDocumentId(uri); final String[] split = docId.split(":"); final String type = split[0]; Uri contentUri = null; if ("image".equals(type)) { contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; } else if ("video".equals(type)) { contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI; } else if ("audio".equals(type)) { contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; } final String selection = "_id=?"; final String[] selectionArgs = new String[] { split[1] }; return getDataColumn(context, contentUri, selection, selectionArgs); } } // MediaStore (and general) else if ("content".equalsIgnoreCase(uri.getScheme())) { // Return the remote address if (isGooglePhotosUri(uri)) return uri.getLastPathSegment(); return getDataColumn(context, uri, null, null); } // File else if ("file".equalsIgnoreCase(uri.getScheme())) { return uri.getPath(); } return null; } /** * Get the value of the data column for this Uri. This is useful for * MediaStore Uris, and other file-based ContentProviders. * * @param context The context. * @param uri The Uri to query. * @param selection (Optional) Filter used in the query. * @param selectionArgs (Optional) Selection arguments used in the query. * @return The value of the _data column, which is typically a file path. */ public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) { Cursor cursor = null; final String column = "_data"; final String[] projection = { column }; try { cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null); if (cursor != null && cursor.moveToFirst()) { final int index = cursor.getColumnIndexOrThrow(column); return cursor.getString(index); } } finally { if (cursor != null) cursor.close(); } return null; } /** * @param uri The Uri to check. * @return Whether the Uri authority is ExternalStorageProvider. */ public static boolean isExternalStorageDocument(Uri uri) { return "com.android.externalstorage.documents".equals(uri.getAuthority()); } /** * @param uri The Uri to check. * @return Whether the Uri authority is DownloadsProvider. */ public static boolean isDownloadsDocument(Uri uri) { return "com.android.providers.downloads.documents".equals(uri.getAuthority()); } /** * @param uri The Uri to check. * @return Whether the Uri authority is MediaProvider. */ public static boolean isMediaDocument(Uri uri) { return "com.android.providers.media.documents".equals(uri.getAuthority()); } /** * @param uri The Uri to check. * @return Whether the Uri authority is Google Photos. */ public static boolean isGooglePhotosUri(Uri uri) { return "com.google.android.apps.photos.content".equals(uri.getAuthority()); } }
Par la suite, il faut créer une autre classe java dans le package principal du projet aussi. On nomme cette classe JSONParser.javapackage com.lahiani.houssem.fileupload; import android.util.Log; import org.json.JSONException; import org.json.JSONObject; import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; import java.util.HashMap; public class JSONParser { String charset = "UTF-8"; HttpURLConnection conn; DataOutputStream wr; StringBuilder result; URL urlObj; JSONObject jObj = null; StringBuilder sbParams; String paramsString; public JSONObject makeHttpRequest(String url, String method, HashMapparams) { sbParams = new StringBuilder(); int i = 0; for (String key : params.keySet()) { try { if (i != 0){ sbParams.append("&"); } sbParams.append(key).append("=") .append(URLEncoder.encode(params.get(key), charset)); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } i++; } if (method.equals("POST")) { // request method is POST try { urlObj = new URL(url); conn = (HttpURLConnection) urlObj.openConnection(); conn.setDoOutput(true); conn.setRequestMethod("POST"); conn.setRequestProperty("Accept-Charset", charset); conn.setReadTimeout(10000); conn.setConnectTimeout(15000); conn.connect(); paramsString = sbParams.toString(); wr = new DataOutputStream(conn.getOutputStream()); wr.writeBytes(paramsString); wr.flush(); wr.close(); } catch (IOException e) { e.printStackTrace(); } } else if(method.equals("GET")){ // request method is GET if (sbParams.length() != 0) { url += "?" + sbParams.toString(); } try { urlObj = new URL(url); conn = (HttpURLConnection) urlObj.openConnection(); conn.setDoOutput(false); conn.setRequestMethod("GET"); conn.setRequestProperty("Accept-Charset", charset); conn.setConnectTimeout(15000); conn.connect(); } catch (IOException e) { e.printStackTrace(); } } try { //Receive the response from the server InputStream in = new BufferedInputStream(conn.getInputStream()); BufferedReader reader = new BufferedReader(new InputStreamReader(in)); result = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { result.append(line); } Log.d("JSON Parser", "result: " + result.toString()); } catch (IOException e) { e.printStackTrace(); } conn.disconnect(); // try parse the string to a JSON object try { jObj = new JSONObject(result.toString()); } catch (JSONException e) { Log.e("JSON Parser", "Error parsing data " + e.toString()); } // return JSON Object return jObj; } } Finalement, nous développons le code de notre activité principale MainActivity.javapackage com.lahiani.houssem.fileupload; import android.Manifest; import android.app.ProgressDialog; import android.content.Intent; import android.content.pm.PackageManager; import android.database.Cursor; import android.net.Uri; import android.os.AsyncTask; import android.provider.MediaStore; import android.support.v4.app.ActivityCompat; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Base64; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.Toast; import org.json.JSONObject; import java.io.ByteArrayOutputStream; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.net.HttpURLConnection; import java.net.URL; import java.util.ArrayList; import java.util.HashMap; public class MainActivity extends AppCompatActivity { JSONParser jsonParser=new JSONParser(); Button btpic, btnup; private Uri fileUri; String picturePath; Uri selectedImage; String ba1; public int PDF_REQ_CODE = 1; Uri uri; public static String URL = "http://YourIpAdress/sendfile.php"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); AllowRunTimePermission(); btpic = (Button) findViewById(R.id.cpic); btpic.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // clickpic(); // PDF selection code start from here . Intent intent = new Intent(); intent.setType("application/pdf"); intent.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(Intent.createChooser(intent, "Select Pdf"), PDF_REQ_CODE); } }); btnup = (Button) findViewById(R.id.up); btnup.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { upload(); } }); } private void upload() { picturePath= FilePath.getPath(this, selectedImage); // Image location URL Log.e("path", "----------------" + picturePath); //read file into bytes[] FileInputStream fileInputStream = null; try { File f=new File(picturePath); byte[] bytesArray = new byte[(int) f.length()]; fileInputStream = new FileInputStream(f); fileInputStream.read(bytesArray); ByteArrayOutputStream bao = new ByteArrayOutputStream(); ba1 = Base64.encodeToString(bytesArray, Base64.NO_WRAP); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } Log.e("base64", "-----" + ba1); // Upload image to server new uploadToServer().execute(); } protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == PDF_REQ_CODE && resultCode == RESULT_OK && data != null && data.getData() != null) { selectedImage = data.getData(); } } class uploadToServer extends AsyncTask{ private ProgressDialog pd = new ProgressDialog(MainActivity.this); protected void onPreExecute() { super.onPreExecute(); pd.setMessage("file uploading!"); pd.show(); } @Override protected String doInBackground(Void... params) { HashMap param = new HashMap (); param.put("base64", ba1); param.put("ImageName", System.currentTimeMillis() + ".pdf"); JSONObject json=jsonParser.makeHttpRequest(URL,"POST",param); return "Success"; } protected void onPostExecute(String result) { super.onPostExecute(result); pd.hide(); pd.dismiss(); } } public void AllowRunTimePermission() { if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE)) { Toast.makeText(MainActivity.this,"READ_EXTERNAL_STORAGE permission Access Dialog", Toast.LENGTH_LONG).show(); } else { ActivityCompat.requestPermissions(MainActivity.this,new String[]{ Manifest.permission.READ_EXTERNAL_STORAGE}, 1); } } @Override public void onRequestPermissionsResult(int RC, String per[], int[] Result) { switch (RC) { case 1: if (Result.length > 0 && Result[0] == PackageManager.PERMISSION_GRANTED) { Toast.makeText(MainActivity.this,"Permission Granted", Toast.LENGTH_LONG).show(); } else { Toast.makeText(MainActivity.this,"Permission Canceled", Toast.LENGTH_LONG).show(); } break; } } }
Commentaires
Enregistrer un commentaire
Your comment will be reviewed by moderators