【发布时间】: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>
【问题讨论】:
-
在此处添加您的错误信息
标签: android android-intent camera android-camera-intent