【问题标题】:camera app that launches the native camera and then displays the image on the main page启动本机相机然后在主页上显示图像的相机应用程序
【发布时间】:2015-10-08 06:44:45
【问题描述】:

我是 android 的初学者,一直在尝试理解它的基本概念。我尝试用一​​个按钮和一个图像视图制作一个应用程序。该按钮启动相机,一旦您拍摄并保存了照片,它应该在图像视图中显示它。但是,它没有这样做,我尝试在线搜索代码,它或多或少完全相同。您能提供的任何帮助将不胜感激! 谢谢! 这是我的代码:

package com.example.camapp;

import java.net.URI;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import android.provider.MediaStore;

public class MainActivity extends ActionBarActivity {
Button btn;
ImageView imgview;
Bitmap b;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn=(Button) findViewById(R.id.button1);
        imgview=(ImageView) findViewById(R.id.imageView1);
        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent n=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(n, 0);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent){
        super.onActivityResult(requestCode, resultCode , intent);
        if(resultCode == Activity.RESULT_OK){
            b = (Bitmap) intent.getExtras().get("intent");
    //      Toast.makeText(getApplicationContext(), "Value Of B : " + intent.getData(), Toast.LENGTH_LONG).show();
    //      URI u=URI.create(intent.getData().toString());
            imgview.setImageBitmap(b);
        }
        else {
            Toast.makeText(getApplicationContext(), "Else", Toast.LENGTH_LONG).show();
        }

    }

}

【问题讨论】:

    标签: android android-intent bitmap camera uiimageview


    【解决方案1】:

    检查清单中是否有使用功能

    <uses-feature android:name="android.hardware.camera" android:required="true" />
    

    获取图片改为:

        Bundle extras = intent.getExtras();
        Bitmap imageBitmap = (Bitmap) extras.get("data");
    

    或者,写更多细节,哪个部分不起作用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-06
      • 1970-01-01
      • 2019-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多