用于打开相机并将图像上传到服务器的 Android 活动


Android activity to open camera and upload an image to a server

可能的重复项:
从活动中调用摄像头,捕获图像并上传到服务器

这是我在互联网上得到的代码:

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);
    }
}

这显示了一个按钮和一个图像视图(最初包含默认图像(,当我单击该按钮时,它会将我带到图库,当我单击任何图像时,该图像将提供给图像视图。我这里有两个问题:1.如何使按钮将我带到相机以及何时拍摄图像2.直接上传到网络服务器

.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>

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

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

在OnActivityresult中,您将获得从相机捕获的图像。

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

请浏览以下网址

Android 发布 Base64 字符串到 PHP