gpt4 book ai didi

android - 使用 Intent 打开相机

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:54:33 24 4
gpt4 key购买 nike

我想在 Android 中使用 intent 打开 camera

我使用了以下代码,但是当我按下按钮(其操作是onclick() 函数时,应用程序自行关闭。

 public void onclick(int actionCode){
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePictureIntent, actionCode);
}
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;
}

如果有人可以帮助我。

最佳答案

File destination;
Uri selectedImage;
public static String selectedPath1 = "NONE";
private static final int PICK_Camera_IMAGE = 2;
private static final int SELECT_FILE1 = 1;
public static Bitmap bmpScale;
public static String imagePath;



mcamera = (Button) findViewById(R.id.button1);
mcamera.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

String name = dateToString(new Date(), "yyyy-MM-dd-hh-mm-ss");
destination = new File(Environment
.getExternalStorageDirectory(), name + ".jpg");

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(destination));
startActivityForResult(intent, PICK_Camera_IMAGE);

}
});

// ......................gallery_function..........//
mgallery = (Button) findViewById(R.id.button2);
mgallery.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
openGallery(SELECT_FILE1);
}
});

为了 Intent //...............................图库功能................................//

    public void openGallery(int SELECT_FILE1) {

Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(
Intent.createChooser(intent, "Select file to upload "),
SELECT_FILE1);

}

//……...... Intent …………

                // ..................
protected void onActivityResult(int requestCode, int resultCode,
Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
Uri selectedImageUri = null;
String filePath = null;
switch (requestCode) {
case SELECT_FILE1:
if (resultCode == Activity.RESULT_OK) {
selectedImage = imageReturnedIntent.getData();

if (requestCode == SELECT_FILE1) {
selectedPath1 = getPath(selectedImage);
// mimagepath.setText(selectedPath1);
// Toast.makeText(Camera.this, "" + selectedPath1 + "",
// 500).show();

if (selectedPath1 != null) {

BitmapFactory.Options options = new BitmapFactory.Options();

options.inJustDecodeBounds = true;
// image path `String` where your image is located
BitmapFactory.decodeFile(selectedPath1, options);



// Log.d("setpath ", "setpath " + selectedPath1);
;

}
}

break;
case PICK_Camera_IMAGE:
if (resultCode == RESULT_OK) {

try {
in = new FileInputStream(destination);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;
imagePath = destination.getAbsolutePath();

// Toast.makeText(Camera.this, "" + imagePath +
// "",Toast.LENGTH_LONG).show();

break;

}

}

关于android - 使用 Intent 打开相机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16799818/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com