安卓工作室拍照

时间:2015-09-19 03:32:53

标签: android android-camera android-camera-intent

嘿我跟着一个教程在Android工作室项目中拍照但我的应用程序崩溃没有任何日志错误。任何帮助,将不胜感激。如果有人知道如何将图库中的图片添加到图像视图中,那将是非常好的。

public class MainActivity extends Activity {
    Button bn;
    int REQUEST_CODE = 1;
    ImageView IMG;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bn = (Button) findViewById(R.id.bn);
        IMG = (ImageView) findViewById(R.id.img);
        bn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                if(i.resolveActivity(getPackageManager())!= null){
                    startActivityForResult(i,REQUEST_CODE);
                }
            }
        });
        }

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

    public void onActivityResult(int requestcode, int resultcode, Intent data){
        if(requestcode == REQUEST_CODE && resultcode == RESULT_OK)
        {
            Bundle extras = data.getExtras();
            Bitmap imageBitmap = (Bitmap) extras.get("data");
            IMG.setImageBitmap(imageBitmap);
        }
    }

} 

1 个答案:

答案 0 :(得分:0)

嗨我想你要从画廊或相机拍照,然后你想把那个图像设置为ImageView。如果我是对的,请按照以下代码

private void openCamera() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd_HH_mm_ss");
String currentTimeStamp = dateFormat.format(new Date());
f = new File(android.os.Environment.getExternalStorageDirectory(), currentTimeStamp);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
startActivityForResult(intent, 1);

}

private void openGallery() {
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent, "Select File"), REQUEST_GALLERY);

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
    if (requestCode == 1) {
        try {
            String click = f.getAbsolutePath();
            SQLiteDatabase db = dbHelper.getWritableDatabase();
            ContentValues values = new ContentValues();
            values.put(User_Detail.KEY_ALBUM_IMAGE_PATH, click);
            values.put(User_Detail.USER_ID, Utlity.user_id);
            values.put(User_Detail.KEY_ID_ALBUM_REF, album_id);
            long loginStat = db.insert(User_Detail.TABLE_4, null, values);
            loadData(album_id);

        } catch (Exception e) {
            e.printStackTrace();
        }
 } else if (requestCode == 2) {

        Uri selectedImage = data.getData();
        String[] filePath = { MediaStore.Images.Media.DATA };
        Cursor c = getContentResolver().query(selectedImage, filePath, null, null, null);
        c.moveToFirst();
        int columnIndex = c.getColumnIndex(filePath[0]);
        picturePath = c.getString(columnIndex);
        if (picturePath != null && picturePath.length() > 1) {
            ContentValues va = new ContentValues();
        } else {
        }
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        String image_path = picturePath;
        ContentValues values = new ContentValues();
        values.put(User_Detail.KEY_ALBUM_IMAGE_PATH, image_path);
        values.put(User_Detail.USER_ID, Utlity.user_id);
        values.put(User_Detail.KEY_ID_ALBUM_REF, album_id);
        long loginStat = db.insert(User_Detail.TABLE_4, null, values);
        loadData(album_id);
        c.close();
        Log.w("path of image from gallery......******************.........", picturePath + "");
    }
}

}

现在您获得了选择图像或捕获图像的ImagePath。现在你需要在你的Image View中设置Crate a Adapter类,它扩展了BaseAdapter并在那个适配器类中你可以通过将Imagepath转换为Bitmap来设置这个图像