Android开发之将拍摄的图片传至服务器
package com.example.oldtab; import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL; import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView; public class Group extends ActionBarActivity {
private static final int TAKE_PICTURE = 0x0; private static final String TAG_SEP = "----ninesofttestpostfile"; private ImageView imageview;
private Button button;
private Uri outputFileUri;
private Bitmap bitmap;
private File file; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_group); imageview = (ImageView) findViewById(R.id.imageView1);
button = (Button) findViewById(R.id.button1); button.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
File file = new File(Environment.getExternalStorageDirectory(),
"test.jpg");
outputFileUri = Uri.fromFile(file); Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); startActivityForResult(intent, TAKE_PICTURE);
} }); } @Override
protected void onDestroy() {
if (bitmap != null && !bitmap.isRecycled()) {
bitmap.recycle();
}
super.onDestroy();
} @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == TAKE_PICTURE) {
if (data == null) {
if (bitmap != null) {
if (!bitmap.isRecycled()) {
bitmap.recycle();
}
}
int width = 1024;
int height = 768; file = new File(Environment.getExternalStorageDirectory(),
"test.jpg"); outputFileUri = Uri.fromFile(file); BitmapFactory.Options factoryoption = new BitmapFactory.Options();
factoryoption.inJustDecodeBounds = true; BitmapFactory
.decodeFile(outputFileUri.getPath(), factoryoption); int outwidth = factoryoption.outWidth;
int outheight = factoryoption.outHeight; int scale = Math.min(outwidth / width, outheight / height); factoryoption.inJustDecodeBounds = false;
factoryoption.inSampleSize = scale;
factoryoption.inPurgeable = true; bitmap = BitmapFactory.decodeFile(outputFileUri.getPath(),
factoryoption);
imageview.setImageBitmap(bitmap); saveToService();
}
}
super.onActivityResult(requestCode, resultCode, data);
} private void saveToService() {
new Thread(new Runnable() { @Override
public void run() {
try {
URL url = new URL("http://172.16.101.79/PostFile.ashx");
HttpURLConnection conn = (HttpURLConnection) url
.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "keep-alive");
conn.setRequestProperty("Accept", "*/*");
conn.setRequestProperty("Content-Type",
"multipart/form-data;boundary=" + TAG_SEP);
conn.setDoOutput(true);
OutputStream out = conn.getOutputStream();
out.write(("--" + TAG_SEP + "\r\n").getBytes());
out.write(("Content-Disposition: form-data; name=\"postfile\"; filename=\"test.jpg\"\r\n")
.getBytes());
out.write(("Content-Type:image/jpeg\r\n\r\n").getBytes());
ByteArrayOutputStream fr = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 50, fr);
out.write(fr.toByteArray());
out.write(("\r\n\r\n--" + TAG_SEP + "--\r\n").getBytes());
out.flush();
out.close();
conn.getResponseCode();
fr.close(); } catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} }).start(); }
}
<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=".Group" > <ImageView
android:id="@+id/imageView1"
android:layout_width="500dp"
android:layout_height="300dp" /> <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:text="Button" /> </RelativeLayout>
Android开发之将拍摄的图片传至服务器的更多相关文章
- 图解android开发在界面上显示图片
图解android开发在界面上显示图片<申明:转自百度> <原文章地址:http://jingyan.baidu.com/article/49711c6153a277fa441b7c ...
- Android开发常用开源框架:图片处理
https://blog.csdn.net/SGQ_CSDN/article/details/79910709 Android开发常用开源框架:图片处理 框架名称 功能描述 Android Unive ...
- Android开发ImageView控件缩放图片
首先还是最基础的ImageView控件如何显示图片: <ImageView Android:id="@+id/imgView" ...
- android开发里跳过的坑——图片文件上传失败
使用的apache的httpclient的jar包,做的http图片上传,上传时,服务器总返文件格式不对.后来发现,是由于在创建FileBody时,使用了默认的ContentType引起的.所以服务器 ...
- 5.21学习总结——android开发实现用户头像的上传
最近在做个人头像的上传,具体是能调用摄像头和从相册进行选择.本篇文章参考的我的同学的博客,大家有兴趣可以去原作者那里去看看: Hi(.・∀・)ノ (cnblogs.com) 1.使用glide进行图片 ...
- android 开发 获取各种intent (图片、apk文件、excel、pdf等文件)
public static Intent openFile(String filePath){ File file = new File(filePath); if(!file.exists()) r ...
- Android开发之自定义圆角矩形图片ImageView的实现
android中的ImageView只能显示矩形的图片,这样一来不能满足我们其他的需求,比如要显示圆角矩形的图片,这个时候,我们就需要自定义ImageView了,其原理就是首先获取到图片的Bitmap ...
- Android开发之自定义圆角矩形图片ImageView的实现 - Jamy Cai
android中的ImageView只能显示矩形的图片,这样一来不能满足我们其他的需求,比如要显示圆角矩形的图片,这个时候,我们就需要自定义ImageView了,其原理就是首先获取到图片的Bitmap ...
- Android开发技巧——定制仿微信图片裁剪控件
拍照--裁剪,或者是选择图片--裁剪,是我们设置头像或上传图片时经常需要的一组操作.上篇讲了Camera的使用,这篇讲一下我对图片裁剪的实现. 背景 下面的需求都来自产品. 裁剪图片要像微信那样,拖动 ...
随机推荐
- ImportError: No module named caffe.proto解决办法
原文 https://blog.csdn.net/lanyuelvyun/article/details/73628152 在用自己的数据训练基于caffe的SSD模型的时候,我们需要将图片数据转 ...
- 为sharepoint的内部页面添加后台代码
我们知道,存储在数据库里的SharePoint页面是不能直接添加后台代码的,这给我们带来了很多的不方便,比如想要在页面上实现一些东西,都必 须使用Webpart或者自定义控件的方式,哪怕仅仅是很简单的 ...
- [leetcode]Best Time to Buy and Sell Stock III @ Python
原题地址:https://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/ 题意: Say you have an array ...
- Spring Boot集成MyBatis开发Web项目
1.Maven构建Spring Boot 创建Maven Web工程,引入spring-boot-starter-parent依赖 <project xmlns="http://mav ...
- Ext4中获取下拉框的值
var supplierCombo = Ext.getCmp("rkSupplierCombo_id"); var supplierId = supplierCombo.getV ...
- R 分组计算描述性统计量
统计学区内各个小区的房价均值 数据格式 id|community_name|house_area|house_structure|house_total|house_avg|agency_name|h ...
- SSAS知识回放之订单数据分析
1:目标 基于已经做好的DW,利用SSAS实现一个多维数据模型的创建,通过浏览可以简单的实现订单数据的分析 2:步骤 2.1:添加数据源 如下图所示,创建一个数据仓库层的数据源连接 2.2:添加数据源 ...
- (转)Unity3D研究院之手游开发中所有特殊的文件夹(assetbundle与Application.persistentDataPath)
这里列举出手游开发中用到了所有特殊文件夹. 1.Editor Editor文件夹可以在根目录下,也可以在子目录里,只要名子叫Editor就可以.比如目录:/xxx/xxx/Editor 和 /Edi ...
- Maven hive-jdbc教程
由于项目中需要用到hive-jdbc从数据仓库拉数据下来,所以简单的学一下hive,hive数据仓库建构在hadoop集群之上,数据存在hdfs文件系统中,hive中执行的操作会装换成mapreduc ...
- Active Directory 域服务(AD DS)
本文内容 概述 工作组架构与域架构 名称空间(Namespace) 对象(Object).容器(Container)与组织单位(Organization Units,OU) 域树(Domain Tre ...