相机无法使用Intent打开

问题描述 投票:0回答:3

请在下面找到我的代码。我想按按钮打开相机。我的问题是我只得到错误处理的Toast,即相机没有打开。为什么?你能帮助我吗?

我没有找到“try”-code没有执行的原因。

我将附上我的代码和清单文件。

谢谢!

public class FotoMachen extends Activity {

Button btn1;
ImageView iv1;
Intent bildIntent;

File bildFile = new File(Environment.getExternalStorageDirectory() + "/FotoApp/bild.png");
Uri bildUri = Uri.fromFile(bildFile);
int cameraCode = 15;
Bitmap bm1;

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

    btn1 = (Button) findViewById(R.id.button);
    iv1 = (ImageView) findViewById(R.id.imageView);

    if(bildFile.exists()) {
        bm1 = BitmapFactory.decodeFile(bildFile.getAbsolutePath());
        iv1.setImageBitmap(bm1);
    }

    btn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            try {
                bildIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                bildIntent.putExtra(MediaStore.EXTRA_OUTPUT, bildUri);
                startActivityForResult(bildIntent, cameraCode);
            } catch (Exception e) {
                e.printStackTrace();
                Toast.makeText(getApplicationContext(), "Kamera nicht unterstützt", Toast.LENGTH_SHORT).show();
            }
        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if(resultCode == RESULT_OK) {
        if(requestCode == cameraCode){
            Toast.makeText(getApplicationContext(), "Bild gespeichert unter: " + bildFile.getAbsolutePath(), Toast.LENGTH_SHORT).show();
            bm1 = BitmapFactory.decodeFile(bildFile.getAbsolutePath());
            iv1.setImageBitmap(bm1);
        }
    }
    super.onActivityResult(requestCode, resultCode, data);
}

}

表现:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.jochen.camera">

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera2" />


<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".FotoMachen">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

android android-intent camera android-camera-intent
3个回答
1
投票

您需要像下面一样询问权限运行时

  if (ContextCompat.checkSelfPermission(getContext(),
                Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
              //do your stuff
    }else {
                ActivityCompat.requestPermissions((Activity) getContext(), 
                        new String[]{Manifest.permission.CAMERA},
                        MY_PERMISSIONS_REQUEST_CAMERA);
            }

        }

0
投票

看看这个link,我认为它来自你的清单或putExtra方法。你还需要检查android 6或更高版本的权限。


0
投票

使用Intent documentation请求相机

将操作委托给其他应用程序的Android方式是调用描述您想要完成的操作的Intent。此过程涉及三个部分:Intent本身,启动外部Activity的调用,以及焦点返回到您的活动时处理图像数据的一些代码。

private static final int CAMERA_TAKE_PICTURE = 1;    
private Uri imageUri;

public void takePhoto(View view) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File photo = new File(Environment.getExternalStorageDirectory(),  "Pic.jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT,
        Uri.fromFile(photo));
imageUri = Uri.fromFile(photo);
startActivityForResult(intent, CAMERA_TAKE_PICTURE );
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case CAMERA_TAKE_PICTURE :
    if (resultCode == Activity.RESULT_OK) {
        Uri selectedImage = imageUri;
        getContentResolver().notifyChange(selectedImage, null);
        ImageView imageView = (ImageView) findViewById(R.id.ImageView);
        ContentResolver cr = getContentResolver();
        Bitmap bitmap;
        try {
             bitmap = android.provider.MediaStore.Images.Media
             .getBitmap(cr, selectedImage);

            imageView.setImageBitmap(bitmap);
            Toast.makeText(this, selectedImage.toString(),
                    Toast.LENGTH_LONG).show();
        } catch (Exception e) {
            Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT)
                    .show();
            Log.e("Camera", e.toString());
        }
    }
}
}
© www.soinside.com 2019 - 2024. All rights reserved.