Android的活动打开摄像头和上传图片到服务器 [英] Android activity to open camera and upload an image to a server

查看:162
本文介绍了Android的活动打开摄像头和上传图片到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/10679571/calling-camera-from-an-activity-capturing-an-image-and-uploading-to-a-server\">Calling相机的活动,捕捉图像,并上传到服务器


下面是code I互联网上得到了:

 包com.android.imageuploader;进口java.io.FileNotFoundException;
进口java.io.IOException异常;
进口的java.io.InputStream;进口com.android.imageuploader.R;进口android.app.Activity;
进口android.content.Intent;
进口android.graphics.Bitmap;
进口android.graphics.BitmapFactory;
进口android.os.Bundle;
进口android.view.View;
进口android.widget.Button;
进口android.widget.ImageView;公共类ImageUploaderActivity延伸活动{
    私有静态最终诠释REQUEST_ code = 1;
    私人按钮button_1;
    公众诠释TAKE_PICTURE = 1;
    私人ImageView的image_view;
    私人位图位图;    / **当第一次创建活动调用。 * /
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);        image_view =(ImageView的)findViewById(R.id.result);
        button_1 =(按钮)findViewById(R.id.button1);        button_1.setOnClickListener(新View.OnClickListener(){            公共无效的onClick(查看为arg0){                意向意图=新意图(android.media.action.IMAGE_CAPTURE);
                startActivityForResult(意向,TAKE_PICTURE);
            }
        });
    }    @覆盖
    保护无效的onActivityResult(INT申请code,INT结果code,意图数据){
        // TODO自动生成方法存根
        如果(要求code == REQUEST_ code和;&安培;结果code == Activity.RESULT_OK)
            尝试{
                //我们需要recyle未使用的位图
                如果(位图!= NULL){
                    bitmap.recycle();
                }
                InputStream的流= getContentResolver()。openInputStream(
                        data.getData());
                位= BitmapFactory.de codeStream(流);
                stream.close();
                image_view.setImageBitmap(位图);
            }赶上(FileNotFoundException异常五){
                e.printStackTrace();
            }赶上(IOException异常五){
                e.printStackTrace();
            }
        super.onActivityResult(要求code,结果code,数据);
    }
}

这显示了一个按钮和一个ImageView的(包含默认图像开始),当我点击按钮就带我到画廊,当我在任何图像给予的ImageView图像的点击。
我有两个问题在这里:
1.如何使按钮带我去拍照,当我拍摄图像
直接2.upload到一个web服务器

XML:

 &LT;?XML版本=1.0编码=UTF-8&GT?;
&LT; LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:方向=垂直&GT;    &LT;按钮
        机器人:ID =@ + ID /按钮1
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        安卓的onClick =pickImage
        机器人:文字=按钮&GT;
    &LT; /按钮&GT;    &LT; ImageView的
        机器人:ID =@ + ID /结果
        机器人:layout_width =50dp
        机器人:layout_height =50dp
        机器人:SRC =@绘制/ wic_logo_small&GT;
    &LT; / ImageView的&GT;&LT; / LinearLayout中&GT;


解决方案

当用户点击您需要通过意图打开相机按钮例如

 公众诠释TAKE_PICTURE = 1
    意向意图=新意图(android.media.action.IMAGE_CAPTURE);
    startActivityForResult(意向,TAKE_PICTURE);

和中的onActivityResult你会得到你从你的相机拍摄的形象。

现在你必须到该图像上传到服务器

PLZ通过以下网址

<一个href=\"http://stackoverflow.com/questions/9920967/android-post-base64-string-to-php/9921230#9921230\">Android后Base64的字符串作为PHP

Possible Duplicate:
Calling camera from an activity, capturing an image and uploading to a server

Here is the code i got on internet:

package com.android.imageuploader;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import com.android.imageuploader.R;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class ImageUploaderActivity extends Activity {
    private static final int REQUEST_CODE = 1;
    private Button button_1;
    public int TAKE_PICTURE = 1;
    private ImageView image_view;
    private Bitmap bitmap;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        image_view = (ImageView) findViewById(R.id.result);
        button_1 = (Button) findViewById(R.id.button1);

        button_1.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {

                Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
                startActivityForResult(intent, TAKE_PICTURE);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK)
            try {
                // We need to recyle unused bitmaps
                if (bitmap != null) {
                    bitmap.recycle();
                }
                InputStream stream = getContentResolver().openInputStream(
                        data.getData());
                bitmap = BitmapFactory.decodeStream(stream);
                stream.close();
                image_view.setImageBitmap(bitmap);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        super.onActivityResult(requestCode, resultCode, data);
    }
}

this shows a button and an imageView(contains default image initially) and when i click on the button it takes me to the gallery and when i click on any of the images that image is given to the imageView. I have two questions here: 1.how to make the button take me to the camera and when i capture an image 2.upload it directly to a web server

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="pickImage"
        android:text="Button" >
    </Button>

    <ImageView
        android:id="@+id/result"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@drawable/wic_logo_small" >
    </ImageView>

</LinearLayout>

解决方案

When user clicks the button you need to open a camera through intent e.g.

    public int TAKE_PICTURE =1
    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
    startActivityForResult(intent, TAKE_PICTURE);

and in onactivityresult you will get that image which you captured from your camera.

Now you have to upload that image to server

plz go through the following url

Android post Base64 String to PHP

这篇关于Android的活动打开摄像头和上传图片到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆