相机无法使用Intent打开

时间:2017-12-25 11:50:47

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

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

我没有找到"尝试" -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>

3 个答案:

答案 0 :(得分: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);
            }

        }

答案 1 :(得分:0)

请参阅此link,我认为它来自您的清单或putExtra方法。您还需要检查Android 6或更高版本的权限。

答案 2 :(得分:0)

使用Intent documentation

请求相机
  

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

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());
        }
    }
}
}