从活动中调用摄像头,捕捉的图像,并上传到服务器 [英] Calling camera from an activity, capturing an image and uploading to a server

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

问题描述

在我的应用程序,我有一个按钮1其中要求摄像头,拍摄图像后,必须将其保存到设备画廊。当我点击按钮2,就必须打开库,并要求选择一个图片。选中时,它必须被显示在下面这些按钮ImageView的

In my app, I have a button1 which calls camera, and after capturing the image it must be saved to the device gallery. When I click on button2, it must open the gallery, and ask to select a picture. When selected, it must be shown on the imageView below these buttons.

下面是我的code:

package com.android.imageuploading;

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

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class ImageUploadingActivity extends Activity {
    private static final int REQUEST_CODE = 1;
    private Bitmap bitmap;
    private ImageView imageView;
    private Button button_1;
    public int TAKE_PICTURE = 1;
    private Button button_2;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        imageView = (ImageView) findViewById(R.id.image);
        button_1 = (Button) findViewById(R.id.button1);
        button_1.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

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

            }
        });
        button_2 = (Button) findViewById(R.id.button2);
        button_2.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                pickImage(getCurrentFocus());
            }
        });
    }

    public void pickImage(View view) {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        startActivityForResult(intent, REQUEST_CODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        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();
                imageView.setImageBitmap(bitmap);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        super.onActivityResult(requestCode, resultCode, data);
    }

    public static boolean isIntentAvailable(Context context, String action) {
        final PackageManager packageManager = context.getPackageManager();
        final Intent intent = new Intent(action);
        List<ResolveInfo> list = packageManager.queryIntentActivities(intent,
                PackageManager.MATCH_DEFAULT_ONLY);
        return list.size() > 0;
    }
}

现在的问题是,当我捕捉到的图像,它要求保存或放弃。当我点击保存,我的应用程序崩溃,他说:

The problem is when I capture the image, it asks for save or discard. When I click on save, my app crashes, saying:

java.lang.RuntimeException的:不提供结果ResultInfo {谁= NULL,请求= 1,结果= -1,数据=意向{行为=内联数据(有群众演员)}}到活动{com.android.imageuploading /com.android.imageuploading.ImageUploadingActivity}:显示java.lang.NullPointerException

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.android.imageuploading/com.android.imageuploading.ImageUploadingActivity}: java.lang.NullPointerException

谁能帮我,我需要改变code?

Can anyone help me where i need to change the code?

推荐答案

在我来说,我使用这样的:当我点击保存按钮PIC被保存并返回我的路径文件路径变量

in my case i use this: when i click on save button pic is save and return me path in filePath variable.

String filePath =
        Environment.getExternalStorageDirectory() +"/your_image_name.jpeg";
File file = new File(filePath);
Uri output = Uri.fromFile(file);
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, output);

在onactivityresul()我用这个文件路径。

and in onactivityresul() i use this "filePath" .

这篇关于从活动中调用摄像头,捕捉的图像,并上传到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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