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开发之将拍摄的图片传至服务器的更多相关文章

  1. 图解android开发在界面上显示图片

    图解android开发在界面上显示图片<申明:转自百度> <原文章地址:http://jingyan.baidu.com/article/49711c6153a277fa441b7c ...

  2. Android开发常用开源框架:图片处理

    https://blog.csdn.net/SGQ_CSDN/article/details/79910709 Android开发常用开源框架:图片处理 框架名称 功能描述 Android Unive ...

  3. Android开发ImageView控件缩放图片

    首先还是最基础的ImageView控件如何显示图片: <ImageView                Android:id="@+id/imgView"          ...

  4. android开发里跳过的坑——图片文件上传失败

    使用的apache的httpclient的jar包,做的http图片上传,上传时,服务器总返文件格式不对.后来发现,是由于在创建FileBody时,使用了默认的ContentType引起的.所以服务器 ...

  5. 5.21学习总结——android开发实现用户头像的上传

    最近在做个人头像的上传,具体是能调用摄像头和从相册进行选择.本篇文章参考的我的同学的博客,大家有兴趣可以去原作者那里去看看: Hi(.・∀・)ノ (cnblogs.com) 1.使用glide进行图片 ...

  6. android 开发 获取各种intent (图片、apk文件、excel、pdf等文件)

    public static Intent openFile(String filePath){ File file = new File(filePath); if(!file.exists()) r ...

  7. Android开发之自定义圆角矩形图片ImageView的实现

    android中的ImageView只能显示矩形的图片,这样一来不能满足我们其他的需求,比如要显示圆角矩形的图片,这个时候,我们就需要自定义ImageView了,其原理就是首先获取到图片的Bitmap ...

  8. Android开发之自定义圆角矩形图片ImageView的实现 - Jamy Cai

    android中的ImageView只能显示矩形的图片,这样一来不能满足我们其他的需求,比如要显示圆角矩形的图片,这个时候,我们就需要自定义ImageView了,其原理就是首先获取到图片的Bitmap ...

  9. Android开发技巧——定制仿微信图片裁剪控件

    拍照--裁剪,或者是选择图片--裁剪,是我们设置头像或上传图片时经常需要的一组操作.上篇讲了Camera的使用,这篇讲一下我对图片裁剪的实现. 背景 下面的需求都来自产品. 裁剪图片要像微信那样,拖动 ...

随机推荐

  1. HTML5 filesystem: 网址

    FileSystem API 使用新的网址机制,(即 filesystem:),可用于填充 src 或 href 属性.例如,如果您要显示某幅图片且拥有相应的 fileEntry,您可以调用 toUR ...

  2. Andriod源码搜集

    1.一个左侧抽屉式导航NavigationDraw 教程:http://developer.android.com/training/implementing-navigation/nav-drawe ...

  3. [leetcode]Word Ladder II @ Python

    [leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://b ...

  4. Word Ladder leetcode java

    题目: Given two words (start and end), and a dictionary, find the length of shortest transformation se ...

  5. 大数据开发实战:Hadoop数据仓库开发实战

    1.Hadoop数据仓库架构设计 如上图. ODS(Operation Data Store)层:ODS层通常也被称为准备区(Staging area),它们是后续数据仓库层(即基于Kimball维度 ...

  6. kaggle预测

    两个预测kaggle比赛 一 .https://www.kaggle.com/c/web-traffic-time-series-forecasting/overview Arthur Suilin• ...

  7. (转)Unity3d中的碰撞检测

    很多时候,当我们的主角与其他GameObject发生碰撞时, 我们需要做一些特殊的事情,比如:子弹击中敌人,敌人就得执行一系列的动作.这时,我们就需要检测到碰撞现象,即碰撞检测.这一篇,我来具体谈谈自 ...

  8. Web.config的Release版本和Debug版本不一样的奥秘

      VS编译完后,release版本的web.config(或者app.config) 为什么不一样那? 我们查看一下项目结构,会发现有两个版本的config文件存在: 打开web.Debug.con ...

  9. windows vs2017环境下编译webkit

    源码地址:https://github.com/BlzFans/wke 先看官方的说明: Web和Flash的嵌入式3D游戏,基于WebKit 建筑工作单元 VS2005: 1安装Visual Stu ...

  10. (算法)Game

    题目: Jeff loves playing games, Gluttonous snake( an old game in NOKIA era ) is one of his favourites. ...