2 gaoyouhuang gaoyouhuang 于 2016.01.25 20:09 提问

android 利用自带相机拍照 在现实出来

单独可以 放在项目里就不行 我也真的是无语了 就是在拍完照片 按完成的时候报错的
上代码
package com.android.workapp;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.StrictMode;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class Person_data_touxiang extends Activity {
private static final int PHOTO_CAPTURE = 1;
private static final int PHOTO_CAPTURE1 = 2;
private static String photoPath = "/sdcard/AnBo/";
private static String photoName = "";

private Button photo, sc_photo,sc_img;
private ImageView img_photo;
private Bitmap upbitmap;
//private String newName = "laoli.jpg";
//private String uploadFile = "/sdcard/AnBo/laol.jpg";
//private String actionUrl = "http://192.168.1.109:8080/Photo/photoServlet";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_person_data_touxiang);

    sc_img = (Button) findViewById(R.id.p_img);//本地
    sc_img.setOnClickListener(new sc_img());
    photo = (Button) findViewById(R.id.photo);//拍照
    sc_photo = (Button) findViewById(R.id.sc_photo);//上传
    sc_photo.setOnClickListener(new sc_photo());
    img_photo = (ImageView) findViewById(R.id.imt_photo);
    // android.os.NetworkOnMainThreadException
    StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites()
            .detectNetwork().penaltyLog().build());
    StrictMode.setVmPolicy(
            new StrictMode.VmPolicy.Builder().detectLeakedSqlLiteObjects().penaltyLog().penaltyDeath().build());

    photo.setOnClickListener(new photo());
}

// 本地
class sc_img implements View.OnClickListener {

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        Intent intent = new Intent(Intent.ACTION_PICK, null);
        intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
        startActivityForResult(intent, PHOTO_CAPTURE1);
    }

}

class sc_photo implements View.OnClickListener {

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        dialog();
    }

}

// 拍照
class photo implements View.OnClickListener {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
        File file = new File(photoPath);
        if (!file.exists()) { // 检查图片存放的文件夹是否存在
            file.mkdir(); // 不存在的话 创建文件夹
        }
        photoName=photoPath +System.currentTimeMillis() +".jpg";
        File photo = new File(photoName);
        Uri imageUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "image.jpg"));
        imageUri = Uri.fromFile(photo);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); // 这样就将文件的存储方式和uri指定到了Camera应用中
        startActivityForResult(intent, PHOTO_CAPTURE);

    }

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    String sdStatus = Environment.getExternalStorageState();
    switch (requestCode) {
    case PHOTO_CAPTURE:
        if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) {
            Log.i("内存卡错误", "请检查您的内存卡");
        } else {
            BitmapFactory.Options op = new BitmapFactory.Options();
            // 设置图片的大小
            System.out.println("a" +photoName);
            Bitmap bitMap = BitmapFactory.decodeFile(photoName);
            int width = bitMap.getWidth();
            int height = bitMap.getHeight();
            // 设置想要的大小
            int newWidth = 480;
            int newHeight = 640;
            // 计算缩放比例
            float scaleWidth = ((float) newWidth) / width;
            float scaleHeight = ((float) newHeight) / height;
            // 取得想要缩放的matrix参数
            Matrix matrix = new Matrix();
            matrix.postScale(scaleWidth, scaleHeight);
            // 得到新的图片
            bitMap = Bitmap.createBitmap(bitMap, 0, 0, width, height, matrix, true);
            // canvas.drawBitmap(bitMap, 0, 0, paint)
            // 防止内存溢出
            op.inSampleSize = 1; // 这个数字越大,图片大小越小.
            Bitmap pic = null;
            pic = BitmapFactory.decodeFile(photoName, op);
            img_photo.setImageBitmap(pic); // 这个ImageView是拍照完成后显示图片
            FileOutputStream b = null;
            ;
            try {
                b = new FileOutputStream(photoName);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            if (pic != null) {
                pic.compress(Bitmap.CompressFormat.JPEG, 50, b);
            }
        }
        break;
    case PHOTO_CAPTURE1:
        if (data != null) {

            img_photo.setImageURI(data.getData());
            System.out.println(getAbsoluteImagePath(data.getData()));
            System.out.println("1231");
        }
        break;
    default:
        return;
    }
}

private String getAbsoluteImagePath(Uri data) {
    // TODO Auto-generated method stub
    String[] proj = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(data, proj, // Which columns to return
            null, // WHERE clause; which rows to return (all rows)
            null, // WHERE clause selection arguments (none)
            null); // Order-by clause (ascending by name)

    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

protected void dialog() {
    AlertDialog.Builder builder = new Builder(Person_data_touxiang.this);
    builder.setMessage("确认上传图片吗?");

    builder.setTitle("提示");

    builder.setPositiveButton("确认", new OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {

        }
    });
    builder.setNegativeButton("取消", new OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();

        }
    });
    builder.create().show();
}

/* 显示Dialog的method */
private void showDialog(String mess) {
    new AlertDialog.Builder(Person_data_touxiang.this).setTitle("提示").setMessage(mess)
            .setNegativeButton("确定", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                }
            }).show();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}
图片说明

3个回答

ouchao0727
ouchao0727   2016.01.25 20:17
已采纳

manifest有声明权限打开摄像头没?

gaoyouhuang
gaoyouhuang 兄弟 谢了 还需要权限的啊 我以为 Intent哪里 就添加好了 原来 XML里面还要 查了那么多的资料 都没看到 权限这东西
接近 2 年之前 回复
ouchao0727
ouchao0727 还有sd卡存储之类的权限,如果都有,忽略我说的
接近 2 年之前 回复
Mr_dsw
Mr_dsw   Ds   Rxr 2016.01.25 21:07

权限你检查下,在检查下文件创建成功了没有,我的博客中有

gaoyouhuang
gaoyouhuang   2016.01.26 15:44
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
      如果 遇到和我一样的 就是少了这 三句话   觉得 第一句不是     哈哈  不过多总比少好 
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!