【问题标题】:Android activity to open camera and upload an image to a server [duplicate]Android活动打开相机并将图像上传到服务器[重复]
【发布时间】:2012-05-19 06:48:11
【问题描述】:

可能重复:
Calling camera from an activity, capturing an image and uploading 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);
    }
}

这显示了一个按钮和一个 imageView(最初包含默认图像),当我单击该按钮时,它会将我带到画廊,当我单击任何图像时,该图像将提供给 imageView。 我在这里有两个问题: 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>

【问题讨论】:

    标签: php android android-camera image-uploading


    【解决方案1】:

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

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

    在 onactivityresult 中,您将获得从相机拍摄的图像。

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

    请通过以下网址

    Android post Base64 String to PHP

    【讨论】:

    • 我不明白的一件事...使用要单击的按钮的代码在哪里???...在​​代码中我找不到任何按钮(这是应用程序中唯一的类) 但是当我运行它并单击 xml 文件中的按钮时,它会将我带到画廊
    • 您必须编辑该 xml 并添加一个按钮并将其设置为单击侦听器,在其中编写上述代码。
    • 不...我的意思是我粘贴在我的问题中的代码完全按照我解释的方式工作...但是整个代码中没有按钮...所以我怎么样可以点击它去画廊???
    • 你能粘贴那个 main.xml 文件吗
    • 在 xml 文件中查看 android:onClick="pickImage" 这是在你点击按钮时写的,它会调用 "pickImage" 方法。这是如何工作的,你可以查看stackoverflow.com/questions/4153517/…
    猜你喜欢
    • 2012-05-27
    • 1970-01-01
    • 1970-01-01
    • 2017-09-09
    • 2016-04-06
    • 2021-04-30
    • 2021-12-23
    • 2018-01-19
    • 2015-11-10
    相关资源
    最近更新 更多