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的使用,这篇讲一下我对图片裁剪的实现. 背景 下面的需求都来自产品. 裁剪图片要像微信那样,拖动 ...
随机推荐
- Orchard之在前台显式一个属于自己的列表
一:当前现状 Orchard 并不提供筛选 Owner 的 Query,但是 Gallery 中有提供,那就是:Owner Queries. Install 之,然后在解决方案中引入该 Project ...
- Vim 常用操作、查找和替换
这篇文章来详细介绍 Vim 中查找相关的设置和使用方法. 包括查找与替换.查找光标所在词.高亮前景/背景色.切换高亮状态.大小写敏感查找等. 查找 在normal模式下按下/即可进入查找模式,输入要查 ...
- Dockerfile 指令汇总及解析
原文地址:http://www.maoyupeng.com/dockerfile-command-introduction.html 什么是Dockerfile Dockerfile是由一系列 ...
- 安装chrome
安装chrome 在suse12中安装chrome时,提示 lsb >= 4.0 is needed by google-chrome-stable 到http://rpm.pbone.net当 ...
- go语言之进阶篇通过select实现斐波那契数列
一.select作用 Go里面提供了一个关键字select,通过select可以监听channel上的数据流动. select的用法与switch语言非常类似,由select开始一个新的选择块,每个选 ...
- 简单使用Google Analytics监控网站浏览行为
之前对网页做用户转化率调查这块,找到了谷歌GA事件,现在有时间对使用方法和遇到问题做个简单记录.官方文档其实也介绍的比较清楚,可以查看官方文档. 首先,在官网申请UA-id,然后在主页加入如下代码: ...
- Python3 简单验证码识别思路及实例
1.介绍 在爬虫中经常会遇到验证码识别的问题,现在的验证码大多分计算验证码.滑块验证码.识图验证码.语音验证码等四种.本文就是识图验证码,识别的是简单的验证码,要想让识别率更高, 识别的更加准确就需要 ...
- Android -- Drag&&Drop
Android3.0提供了drag/drop框架,利用此框架可以实现使用拖放手势将一个view拖放到当前布局中的另外一个view中. 实现拖放的步骤 首先,我们先了解一下拖放过程,从官方文档可以知道, ...
- OpenGL ES 3.0 and libGLESv2
note that libGLESv2 is the recommended Khronos naming convention for the OpenGL ES 3.0 library. This ...
- ASP入门(五)- VBScript过程和函数
VBScript过程 被封装在Sub和End Sub语句之中的一系列语句 不具有返回值 可带参数 我们的SubFunction.asp中展示了Sub的用法,代码如下: <% Sub mySub( ...