2016-10-31 2 views
0

Ich habe eine CardActivity, die eine CameraActivity öffnet. Ich habe einen Bildknopf, den ich drücke und dann öffnet sich die native Kamera-App und ich kann ein Bild machen. Ich versuche, dies mit einer Absicht mit einem ByteArray an meine CardActivity zurückzugeben. Aber es gibt mir einen leeren weißen Bildschirm. Es fügt nichts in die Bildansicht ein. Das Bitmap-Element ist nicht null, es hat etwas.SetImageBitmap zeigt weiße leere Vollbild

Dieser meinen Schalter ist die Kamera-Aktivität für das Starten und Einstellen von Bild:

switch (v.getId()) { 
       case R.id.pButton1: 
        Intent cam = new Intent(CardActivity.this, CameraActivity.class); 
        startActivity(cam); 
        returnImage2(); 
        mImageView = (ImageView)findViewById(R.id.imageView); 
        mImageView.setImageBitmap(bitmap); 
        mImageView.setRotation(180); 
        break; 

Dies ist mein returnImage2();

public void returnImage2() { 

     try { 
      bitmap = BitmapFactory.decodeStream(this.openFileInput("myImage")); 
     } 
     catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

Das ist mein cameraActivity:

public class CameraActivity extends Activity { 
     private String mCurrentPhotoPath; 
     private ImageView mImageView; 
     private Bitmap mImageBitmap; 
     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      dispatchTakePictureIntent(); 
     } 
     public void dispatchTakePictureIntent() { 
      Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
      // Ensure that there's a camera activity to handle the intent 
      if (takePictureIntent.resolveActivity(getPackageManager()) != null) { 
       // Create the File where the photo should go 
       File photoFile = null; 
       try { 
        photoFile = createImageFile(); 
       } catch (IOException ex) { 
        // Error occurred while creating the File 
        System.out.println("ERR CANNOT CREATE FILE"); 
       } 
       // Continue only if the File was successfully created 
       if (photoFile != null) { 
        Uri photoURI = FileProvider.getUriForFile(this, 
          "com.example.android.fileprovider", 
          photoFile); 
        takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI); 
        startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO); 
       } 
      } 
     } 
     private File createImageFile() throws IOException { 
      // Create an image file name 
      String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
      String imageFileName = "JPEG_" + timeStamp + "_"; 
      File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES); 

      File image = File.createTempFile(
        imageFileName, /* prefix */ 
        ".jpg",   /* suffix */ 
        storageDir  /* directory */ 
      ); 

      // Save a file: path for use with ACTION_VIEW intents 
      mCurrentPhotoPath = "file:" + image.getAbsolutePath(); 
      return image; 
     } 
     private File createImageFile2() throws IOException { 
      // Create an image file name 
      String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
      String imageFileName = "JPEG_" + timeStamp + "_"; 
      File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES); 

      File image = File.createTempFile(
        imageFileName, /* prefix */ 
        ".jpg",   /* suffix */ 
        storageDir  /* directory */ 
      ); 

      // Save a file: path for use with ACTION_VIEW intents 
      mCurrentPhotoPath = "file:" + image.getAbsolutePath(); 
      return image; 
     } 
     @Override 
     protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
      if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) { 
       try { 
        mImageBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse(mCurrentPhotoPath)); 
        cImageFromBitmap(mImageBitmap); 
        //mImageView = (ImageView)findViewById(R.id.imageView); 
        //mImageView.setImageBitmap(mImageBitmap); 
        //mImageView.setRotation(180); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 
     } 
     public String cImageFromBitmap(Bitmap bitmap){ 
      String fileName = "myImage";//no .png or .jpg needed 
      try { 
       ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
       bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes); 
       FileOutputStream fo = openFileOutput(fileName, Context.MODE_PRIVATE); 
       fo.write(bytes.toByteArray()); 
       // remember close file output 
       fo.close(); 
      } catch (Exception e) { 
       e.printStackTrace(); 
       fileName = null; 
      } 
      return fileName; 
     } 
} 

Antwort

0

ich bemerkt, dass Sie REQUEST_TAKE_PHOTO für den Start Ihrer Tätigkeit

   startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO) 

aber Sie überprüfen für REQUEST_IMAGE_CAPTURE auf onActivityResult

   if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) { 
012 verwenden

ALSO:

Ich weiß nicht, warum Sie durch all diese Arbeit gehen, um eine Datei zu erstellen. Wenn Sie ACTION_IMAGE_CAPTURE verwenden die Bitmap selbst kommt zurück in das Ergebnis Absicht:

  if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) { 
       try { 
        mImageBitmap = (Bitmap) data.getExtras().get("data"); 
+0

Der Grund, warum ich durch all diese Arbeit gehen über 1 mb Absichten behandeln:/Ich bekomme immer noch den weißen Bildschirm, wenn ich diesen Teil entfernen der if-Check – NicklasN

Verwandte Themen