2016-07-07 21 views
0

Ich habe eine innere Klasse namens "SaveImageTask", die im Hintergrund arbeitet und ein aufgenommenes Bild von der Kamera speichern, dann verwende ich Intent zu einer anderen Klasse namens "MainActivity2", wo ich das Bild hier anzeigen möchte der Code ist:Ein Bild speichern und anzeigen

private class SaveImageTask extends AsyncTask<byte[], Void, Void> { 

    @Override 
    protected Void doInBackground(byte[]... data) { 
     FileOutputStream outStream = null; 
     try { 
      File sdCard = Environment.getExternalStorageDirectory(); 
      File dir = new File(sdCard.getAbsolutePath() + "/SelfieLightCamera"); 
      dir.mkdirs(); 
      String fileName = String.format("%d.jpg", System.currentTimeMillis()); 
      File outFile = new File(dir, fileName); 
      outStream = new FileOutputStream(outFile); 
      b = flip(BitmapFactory.decodeByteArray(data[0], 0, data[0].length)); 
      b.compress(Bitmap.CompressFormat.PNG, 100, outStream); 
      outStream.flush(); 
      outStream.close(); 
      Log.d("pictureTaken", "onPictureTaken - wrote bytes: " + data.length + " to " + outFile.getAbsolutePath()); 
      refreshGallery(outFile); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } finally { 
     } 
     CameraPreview.safeToTakePicture = true; 
     startActivity(new Intent(MainActivity.this, MainActivity2.class)); 
     return null; 
    } 

    public Bitmap flip(Bitmap bitmap) { 

     int w = bitmap.getWidth(); 
     int h = bitmap.getHeight(); 
     Matrix matrix = new Matrix(); 
     float[] mirrorY = {-1, 0, 0, 0, 1, 0, 0, 0, 1}; 
     Matrix matrixMirrorY = new Matrix(); 
     matrixMirrorY.setValues(mirrorY); 
     matrix.postConcat(matrixMirrorY); 
     matrix.postRotate(90); 
     return Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true); 
    } 
} 

MainActivity2.class:

public class MainActivity2 extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main2); 
     ImageView img = (ImageView) findViewById(R.id.imageView); 
     img.setImageBitmap(MainActivity.b); 
    } 
} 

es ist jedoch zu viel Zeit in Anspruch nimmt, ich das Bild nehme und dann wird es mit einer Verzögerung von 5 Sekunden zwischen der Anzeige, wie auch immer mach es effizienter?

Antwort

0

Lösung wurde Anzeige des Bildes, bevor es (Aufruf der Absicht auf die andere Klasse vor) zu speichern:

private class SaveImageTask extends AsyncTask<byte[], Void, Void> { 

     @Override 
     protected Void doInBackground(byte[]... data) { 
      FileOutputStream outStream = null; 
      try { 
       b = flip(BitmapFactory.decodeByteArray(data[0], 0, data[0].length)); 
       startActivity(new Intent(MainActivity.this, MainActivity2.class)); 
       File sdCard = Environment.getExternalStorageDirectory(); 
       File dir = new File(sdCard.getAbsolutePath() + "/SelfieLightCamera"); 
       dir.mkdirs(); 
       String fileName = "Picture_" + String.format("%d.jpg", System.currentTimeMillis()); 
       File outFile = new File(dir, fileName); 
       outStream = new FileOutputStream(outFile); 
       b.compress(Bitmap.CompressFormat.PNG, 100, outStream); 
       outStream.flush(); 
       outStream.close(); 
       Log.d("pictureTaken", "onPictureTaken - wrote bytes: " + data.length + " to " + outFile.getAbsolutePath()); 
       refreshGallery(outFile); 
      } catch (FileNotFoundException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } finally { 
      } 
      CameraPreview.safeToTakePicture = true; 
      return null; 
     } 

     public Bitmap flip(Bitmap bitmap) { 

      int w = bitmap.getWidth(); 
      int h = bitmap.getHeight(); 
      Matrix matrix = new Matrix(); 
      float[] mirrorY = {-1, 0, 0, 0, 1, 0, 0, 0, 1}; 
      Matrix matrixMirrorY = new Matrix(); 
      matrixMirrorY.setValues(mirrorY); 
      matrix.postConcat(matrixMirrorY); 
      matrix.postRotate(90); 
      return Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true); 
     } 
    }