Ich habe das jetzt getestet aber meine App stürzt jetzt direkt wenn ich die Activity öffne ab
Java
import java.io.File;
import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class AalBild extends Activity {
Button btn1;
ImageView iv1;
Intent bildintent;
int RQ = 45;
int RQ2 =23;
File bildfile;
Uri bilduri;
int Kameracode = 15;
Bitmap bm1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bildfile = new File(Environment.getExternalStorageDirectory() + "/FotoApp/bild.png");
bilduri = Uri.fromFile(bildfile);
btn1 = (Button) findViewById(R.id.btn2);
iv1 = (ImageView) findViewById(R.id.imageView4);
if (bildfile.exists()){
bm1 = BitmapFactory.decodeFile(bildfile.getAbsolutePath());
iv1.setImageBitmap(bm1);
}
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (ActivityCompat.checkSelfPermission(AalBild.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(AalBild.this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED){
foto();
}else {
ActivityCompat.requestPermissions(AalBild.this, new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE},RQ);
ActivityCompat.requestPermissions(AalBild.this, new String[] {Manifest.permission.READ_EXTERNAL_STORAGE},RQ2);
}
}
});
}
private void foto(){
try{
bildintent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
bildintent.putExtra(MediaStore.EXTRA_OUTPUT, bilduri);
startActivityForResult(bildintent, Kameracode);
}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 == Kameracode){
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);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == RQ && grantResults.length >0 && grantResults[0] == PackageManager.PERMISSION_GRANTED && requestCode == RQ2 && grantResults.length >0 && grantResults[0] ==PackageManager.PERMISSION_GRANTED){
foto();
}
}
}
Alles anzeigen