最新消息:文章中包含代码时,请遵守代码高亮规范!

Android 图片上传的两种方式

Android Surfin 11869浏览 0评论

第一步:Activity

package com.androidexample.uploadtoserver;

public class UploadToServerActivity extends Activity {

	private TextView messageText;
	private Button uploadButton, base64UploadButton;
	
	int serverResponseCode = 0;
	ProgressDialog dialog = null;

	String upLoadServerUri = "http://192.168.1.150/AndroidApi/UploadToServer.php";
	String upLoadBase64ServerUri = "http://192.168.1.150/AndroidApi/UploadBase64ToServer.php";

	
	private static final int RESULT_PICK_IMAGE = 1;
	private static final int RESULT_TAKE_IMAGE = 2;
	
	private static final String JPEG_FILE_PREFIX = "IMG_";
	private static final String JPEG_FILE_SUFFIX = ".jpg";
	
	private String mCurrentPhotoPath = null;
	
	private ImageView imageView;

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_upload_to_server);
		
		Button pickPhotoButton = (Button) findViewById(R.id.pickPhotoButton);
		Button takePhotoButton = (Button) findViewById(R.id.takePhotoButton);
		
		imageView = (ImageView) findViewById(R.id.imageView);
		
		uploadButton = (Button) findViewById(R.id.uploadButton);
		base64UploadButton = (Button) findViewById(R.id.base64UploadButton);
		// takeButton = (Button) findViewById(R.id.takeButton);

		// 相册选取图片
		pickPhotoButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				Intent takePhotoIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
				startActivityForResult(takePhotoIntent, RESULT_PICK_IMAGE);
			}
		});
		
		// 拍照选取图片
		takePhotoButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				//调用相机
		        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		        
				File f = null;
				try {
					f = setUpPhotoFile();
					mCurrentPhotoPath = f.getAbsolutePath();
					Log.d("path", mCurrentPhotoPath);
					
					takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
				} catch (IOException e) {
					e.printStackTrace();
					f = null;
					mCurrentPhotoPath = null;
				}
				
		        startActivityForResult(takePictureIntent, RESULT_TAKE_IMAGE);				
			}
		});
		

		uploadButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (mCurrentPhotoPath != null) {
					//uploadFile(mCurrentPhotoPath, upLoadServerUri);
					new FileUploadTask(mCurrentPhotoPath, upLoadServerUri).execute();
				}
			}
		});
		
		base64UploadButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (mCurrentPhotoPath != null) {
					String base64String = Base64Code.encodeImageToBase64String(mCurrentPhotoPath);
					new FileBase64UploadTask(mCurrentPhotoPath, upLoadBase64ServerUri, base64String).execute();
				}
				
			}
		});		

	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		
		if (resultCode == Activity.RESULT_OK) {
			switch(requestCode) {
			case RESULT_PICK_IMAGE: {
				handlePickImage(data);
			}
			break;
			case RESULT_TAKE_IMAGE: {
				handleTakeImage();
			}
			break;
			default:
				break;
			
			}
		}
	}
	
	/**
	 * Handle a picture from picking
	 * 
	 * @param intent
	 */
	private void handlePickImage(Intent intent) {
		Uri selectedImage = intent.getData();
		String[] filePathColumn = { MediaStore.Images.Media.DATA };

		Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
		cursor.moveToFirst();

		int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
		String picturePath = cursor.getString(columnIndex);
		cursor.close();
		
		setPic(imageView, picturePath);
		mCurrentPhotoPath = picturePath;
	}
	
	/**
	 * Handle a picture from taking 
	 */
	private void handleTakeImage() {
		if (mCurrentPhotoPath != null) {
			setPic(imageView, mCurrentPhotoPath);
			galleryAddPic(mCurrentPhotoPath);
			mCurrentPhotoPath = null;
		}		
	}
	
	/**
	 * Add a picture from taking to Gallery
	 * 
	 * @param srcPath
	 */
	private void galleryAddPic(String srcPath) {
	    Intent mediaScanIntent = new Intent("android.intent.action.MEDIA_SCANNER_SCAN_FILE");
		File f = new File(srcPath);
	    Uri contentUri = Uri.fromFile(f);
	    mediaScanIntent.setData(contentUri);
	    this.sendBroadcast(mediaScanIntent);
	}
	
	public String getAlbumName(){
		return "CameraSample";
	}
	
	public File getAlbumStorageDir(String albumName) {
		return new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), albumName);
	}
	
	private File getAlbumDir() {
		File storageDir = null;
		if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
			storageDir = getAlbumStorageDir(getAlbumName());
			if (storageDir != null) {
				if (! storageDir.mkdirs()) {
					if (! storageDir.exists()){
						Log.d("CameraSample", "failed to create directory");
						return null;
					}
				}
			}
			
		} else {
			Log.v(getString(R.string.app_name), "External storage is not mounted READ/WRITE.");
		}
		
		return storageDir;
	}
	
	private File createImageFile() throws IOException {
		// Create an image file name
		String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
		String imageFileName = JPEG_FILE_PREFIX + timeStamp + "_";
		File albumF = getAlbumDir();
		File imageF = File.createTempFile(imageFileName, JPEG_FILE_SUFFIX, albumF);
		return imageF;
	}
	
	private File setUpPhotoFile() throws IOException {
		
		File f = createImageFile();
		mCurrentPhotoPath = f.getAbsolutePath();
		
		return f;
	}	
	
	/**
	 * There isn't enough memory to open up more than a couple camera photos
	 * So pre-scale the target bitmap into which the file is decoded
	 * 
	 * @param mImageView
	 * @param srcPath
	 */
	private void setPic(ImageView mImageView, String srcPath) {
		/* Get the size of the ImageView */
		int targetW = mImageView.getWidth();
		int targetH = mImageView.getHeight();

		/* Get the size of the image */
		BitmapFactory.Options bmOptions = new BitmapFactory.Options();
		bmOptions.inJustDecodeBounds = true;
		BitmapFactory.decodeFile(srcPath, bmOptions);
		int photoW = bmOptions.outWidth;
		int photoH = bmOptions.outHeight;
		
		/* Figure out which way needs to be reduced less */
		int scaleFactor = 2;
		if ((targetW > 0) || (targetH > 0)) {
			scaleFactor = Math.min(photoW/targetW, photoH/targetH);	
		}

		/* Set bitmap options to scale the image decode target */
		bmOptions.inJustDecodeBounds = false;
		bmOptions.inSampleSize = scaleFactor;
		bmOptions.inPurgeable = true;

		/* Decode the JPEG file into a Bitmap */
		Bitmap bitmap = BitmapFactory.decodeFile(srcPath, bmOptions);
		
		/* Associate the Bitmap to the ImageView */
		mImageView.setImageBitmap(bitmap);
	}	

	/**
	 * 上传图片
	 * 
	 * @param sourceFileUri 图片路径
	 * @param uploadUrl 上传
	 * 
	 * @return
	 */
	public int uploadFile(final String fileUrl, String uploadUrl) {

		String fileName = fileUrl.substring(fileUrl.lastIndexOf("/") + 1);

		HttpURLConnection conn = null;
		DataOutputStream dos = null;
		String lineEnd = "\r\n";
		String twoHyphens = "--";
		String boundary = "*****";
		int bytesRead, bytesAvailable, bufferSize;
		byte[] buffer;
		int maxBufferSize = 1 * 1024 * 1024;
		File sourceFile = new File(fileUrl);

		if (!sourceFile.isFile()) {
			dialog.dismiss();
			
			runOnUiThread(new Runnable() {
				
				public void run() {
					messageText.setText("Source File not exist :" + fileUrl);
				}
			});

			return 0;
		} 
		try {

			// open a URL connection to the Servlet
			FileInputStream fileInputStream = new FileInputStream(sourceFile);
			URL url = new URL(uploadUrl);

			// Open a HTTP connection to the URL
			conn = (HttpURLConnection) url.openConnection();
			conn.setDoInput(true); // Allow Inputs
			conn.setDoOutput(true); // Allow Outputs
			conn.setUseCaches(false); // Don't use a Cached Copy
			conn.setRequestMethod("POST");
			conn.setRequestProperty("Connection", "Keep-Alive");
			conn.setRequestProperty("ENCTYPE", "multipart/form-data");
			conn.setRequestProperty("Content-Type",
					"multipart/form-data;boundary=" + boundary);
			conn.setRequestProperty("uploaded_file", fileName);

			dos = new DataOutputStream(conn.getOutputStream());

			dos.writeBytes(twoHyphens + boundary + lineEnd);
			dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\""
					+ fileName + "\"" + lineEnd);

			dos.writeBytes(lineEnd);

			// create a buffer of maximum size
			bytesAvailable = fileInputStream.available();

			bufferSize = Math.min(bytesAvailable, maxBufferSize);
			buffer = new byte[bufferSize];

			// read file and write it into form...
			bytesRead = fileInputStream.read(buffer, 0, bufferSize);

			while (bytesRead > 0) {
				dos.write(buffer, 0, bufferSize);
				bytesAvailable = fileInputStream.available();
				bufferSize = Math.min(bytesAvailable, maxBufferSize);
				bytesRead = fileInputStream.read(buffer, 0, bufferSize);
			}

			// send multipart form data necesssary after file data...
			dos.writeBytes(lineEnd);
			dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

			// Responses from the server (code and message)
			serverResponseCode = conn.getResponseCode();
			String serverResponseMessage = conn.getResponseMessage();

			Log.i("uploadFile", "HTTP Response is : " + serverResponseMessage + ": " + serverResponseCode);

			if (serverResponseCode == 200) {
				runOnUiThread(new Runnable() {
					
					public void run() {
						Toast.makeText(UploadToServerActivity.this, "File Upload Complete.", Toast.LENGTH_SHORT).show();
					}
				});
			}

			// close the streams //
			fileInputStream.close();
			dos.flush();
			dos.close();

		} catch (MalformedURLException ex) {

			dialog.dismiss();
			ex.printStackTrace();

			runOnUiThread(new Runnable() {
				public void run() {
					messageText.setText("MalformedURLException Exception : check script url.");
					Toast.makeText(UploadToServerActivity.this, "MalformedURLException", Toast.LENGTH_SHORT).show();
				}
			});

			Log.e("Upload file to server", "error: " + ex.getMessage(), ex);
		} catch (Exception e) {

			dialog.dismiss();
			e.printStackTrace();

			runOnUiThread(new Runnable() {
				public void run() {
					messageText.setText("Got Exception : see logcat ");
					Toast.makeText(UploadToServerActivity.this, "Got Exception : see logcat2 ", Toast.LENGTH_SHORT).show();
				}
			});
			Log.e("Upload file to server Exception", "Exception : " + e.getMessage(), e);
		}
		dialog.dismiss();
		
		return serverResponseCode;
	}
	
	class FileUploadTask extends AsyncTask<Object, Integer, String> {

		private ProgressDialog dialog = null;
		
		HttpURLConnection connection = null;
		DataOutputStream outputStream = null;
		DataInputStream inputStream = null;

		String lineEnd = "\r\n";
		String twoHyphens = "--";
		String boundary = "*****";
		
		// the file path to upload
		String pathToOurFile;
		
		// the server address to process uploaded file
		String urlServer;
		
		// File
		File uploadFile;
		
		// File totalSize
		long totalSize;

		public FileUploadTask(String pathToOurFile, String urlServer) {
			this.pathToOurFile = pathToOurFile;
			this.urlServer = urlServer;		
			
			this.uploadFile= new File(pathToOurFile);
			this.totalSize= uploadFile.length(); // Get size of file, bytes
		}
		
		@Override
		protected void onPreExecute() {
			dialog = new ProgressDialog(UploadToServerActivity.this);
			dialog.setMessage("正在上传...");
			dialog.setIndeterminate(false);
			dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
			dialog.setProgress(0);
			dialog.show();
		}

		@Override
		protected String doInBackground(Object... arg0) {

			long length = 0;
			int progress;
			int bytesRead, bytesAvailable, bufferSize;
			byte[] buffer;
			int maxBufferSize = 256 * 1024;// 256KB

			try {
				FileInputStream fileInputStream = new FileInputStream(new File(
						pathToOurFile));

				URL url = new URL(urlServer);
				connection = (HttpURLConnection) url.openConnection();

				// Set size of every block for post
				connection.setChunkedStreamingMode(256 * 1024);// 256KB

				// Allow Inputs & Outputs
				connection.setDoInput(true);
				connection.setDoOutput(true);
				connection.setUseCaches(false);

				// Enable POST method
				connection.setRequestMethod("POST");
				connection.setRequestProperty("Connection", "Keep-Alive");
				connection.setRequestProperty("ENCTYPE", "multipart/form-data");
				connection.setRequestProperty("Content-Type",
						"multipart/form-data;boundary=" + boundary);
				connection.setRequestProperty("uploaded_file", pathToOurFile);

				outputStream = new DataOutputStream(
						connection.getOutputStream());
				outputStream.writeBytes(twoHyphens + boundary + lineEnd);
				outputStream
						.writeBytes("Content-Disposition: form-data; name=\" "+ "uploaded_file" + "\";filename=\""
								+ pathToOurFile + "\"" + lineEnd);
				outputStream.writeBytes(lineEnd);

				bytesAvailable = fileInputStream.available();
				bufferSize = Math.min(bytesAvailable, maxBufferSize);
				buffer = new byte[bufferSize];

				// Read file
				bytesRead = fileInputStream.read(buffer, 0, bufferSize);

				while (bytesRead > 0) {
					outputStream.write(buffer, 0, bufferSize);
					length += bufferSize;
					progress = (int) ((length * 100) / totalSize);
					publishProgress(progress);

					bytesAvailable = fileInputStream.available();
					bufferSize = Math.min(bytesAvailable, maxBufferSize);
					bytesRead = fileInputStream.read(buffer, 0, bufferSize);
				}
				outputStream.writeBytes(lineEnd);
				outputStream.writeBytes(twoHyphens + boundary + twoHyphens
						+ lineEnd);
				publishProgress(100);

				// Responses from the server (code and message)
				int serverResponseCode = connection.getResponseCode();
				String serverResponseMessage = connection.getResponseMessage();

				InputStream is = connection.getInputStream();
				InputStreamReader isr = new InputStreamReader(is, "utf-8");
				BufferedReader br = new BufferedReader(isr);
				String result = br.readLine();

				fileInputStream.close();
				outputStream.flush();
				outputStream.close();

				return result;
			} catch (Exception ex) {
				return "网络异常,上传失败";
			}
		}

		@Override
		protected void onProgressUpdate(Integer... progress) {
			dialog.setProgress(progress[0]);
		}

		@Override
		protected void onPostExecute(final String result) {

			if (dialog != null) {
				dialog.dismiss();
//				ShowTip.showTip(UserSignActivity.this, windowManager, view);
			}
			super.onPostExecute(result);

		}
	}	
	
	/**
	 * 
	 */
	class FileBase64UploadTask extends AsyncTask<String, Integer, String> {
		
		private ProgressDialog dialog = null;
		JSONParser jsonParser = null;
		
		// the file path to upload
		String pathToOurFile;
		
		// the server address to process uploaded file
		String urlServer;
		
		String base64String;
		
		public FileBase64UploadTask(String pathToOurFile, String urlServer, String base64String) {
			this.pathToOurFile = pathToOurFile;
			this.urlServer = urlServer;
			this.base64String = base64String;
		}
		
		@Override
		protected void onPreExecute() {
			dialog = new ProgressDialog(UploadToServerActivity.this);
			dialog.setMessage("正在上传...");
			dialog.setIndeterminate(false);
			dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
			dialog.setProgress(0);
			dialog.show();
		}
		
		@Override
		protected String doInBackground(String... args) {
			// Building Parameters
			List<NameValuePair> params = new ArrayList<NameValuePair>();

			// base64 image string
			params.add(new BasicNameValuePair("base64String", base64String));
			
			// image type png jpg
			params.add(new BasicNameValuePair("uploaded_file", pathToOurFile));

			// getting JSON string from URL
			jsonParser = new JSONParser();
			try {
				String a = jsonParser.makeHttpRequest(urlServer, "POST", params);
				

			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
			return null;
		}
		
		@Override
		protected void onProgressUpdate(Integer... progress) {
			dialog.setProgress(progress[0]);
		}

		@Override
		protected void onPostExecute(final String result) {

			if (dialog != null) {
				dialog.dismiss();
//				ShowTip.showTip(UserSignActivity.this, windowManager, view);
			}
			super.onPostExecute(result);

		}		
		
	}	
	
	
	
}

Base64Code.java

package com.androidexample.uploadtoserver.library;

public class Base64Code {

	// constructor
	public Base64Code() {
		
	}
	
	/**
     * Encode image to Base64 string
     * @param srcPath
     * @return Base64 encoded string
     */
    public static String encodeImageToBase64String(String srcPath) {
    	
    	Log.d("srcPath:", srcPath);
    	
        String imageString = null;
        InputStream inputStream;
		try {
			inputStream = new FileInputStream(srcPath);
			//You can get an inputStream using any IO API
	        byte[] bytes;
	        byte[] buffer = new byte[8192];
	        int bytesRead;
	        ByteArrayOutputStream output = new ByteArrayOutputStream();
	        try {
	            while ((bytesRead = inputStream.read(buffer)) != -1) {
	            	output.write(buffer, 0, bytesRead);
	            }
	            inputStream.close();
	     
		        bytes = output.toByteArray();	        
		        imageString = Base64.encodeToString(bytes, 0, bytes.length, Base64.NO_WRAP); 	
		        
	        } catch (IOException e) {
	        	e.printStackTrace();
	        }
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} 
        
        return imageString;
    }
}

第二步:布局 activity_upload_to_server.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" >

    <!-- Header aligned to top -->

    <RelativeLayout
        android:id="@+id/header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:gravity="center" >

        <LinearLayout
            android:id="@+id/linearLayout1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/pickPhotoButton"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="pick a photo" />

            <Button
                android:id="@+id/takePhotoButton"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Take a photo" />
        </LinearLayout>
    </RelativeLayout>

    <!-- Footer aligned to bottom -->

    <RelativeLayout
        android:id="@+id/footer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:gravity="center" >

        <LinearLayout
            android:id="@+id/linearLayout2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
            <Button
                android:id="@+id/uploadButton"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="upload" />

            <Button
                android:id="@+id/base64UploadButton"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="base64 upload" />

        </LinearLayout>
    </RelativeLayout>

    <!-- Scrollable Item below header and above footer -->

    <ScrollView
        android:id="@+id/scrollableContents"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@id/footer"
        android:layout_below="@id/header" >

        <!-- Inflate the contents of the ScrollView dynamicaly -->

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/abc_ab_bottom_solid_dark_holo"
            android:contentDescription="imageView" />
    </ScrollView>

</RelativeLayout>

第三步:AndroidMainfest.xml 赋予网络权限

    <!-- Allow to user camera -->
    <uses-feature android:name="android.hardware.camera" />

    <!-- Allow to connect with Internet -->
    <uses-permission android:name="android.permission.INTERNET" />

    <!-- Allow to write external storage -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

第四步:php服务器端
uploadToServer.php

<?php
    $file_path = "uploads/";

    $file_path = $file_path . basename( $_FILES['uploaded_file']['name']);
    if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path)) {
        echo "success";
    } else{
        echo "fail";
    }
?>

转载时请注明出处及相应链接,本文永久地址:https://blog.yayuanzi.com/1348.html


pay_weixin
pay_weixin
微信打赏
pay_weixin
支付宝打赏
感谢您对作者surfin的打赏,我们会更加努力!    如果您想成为作者,请点我

发表我的评论
取消评论

表情