相机未按意图打开
Posted
技术标签:
【中文标题】相机未按意图打开【英文标题】:Camera not open with Intent 【发布时间】:2018-06-06 16:35:27 【问题描述】:请在下面找到我的代码。 我想通过按下按钮打开相机。 我的问题是我只得到错误处理的祝酒词,即相机没有打开。为什么?你能帮助我吗?
我没有找到不执行“try”代码的原因。
我将附上我的代码和清单文件。
谢谢!
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>
【问题讨论】:
在此处添加您的错误信息 【参考方案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);
【讨论】:
【参考方案2】:看到这个link,我认为它来自您的清单或putExtra
方法。此外,您需要检查android 6 或更高版本的权限。
【讨论】:
【参考方案3】:使用 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());
【讨论】:
以上是关于相机未按意图打开的主要内容,如果未能解决你的问题,请参考以下文章