2016-07-07 10 views
0

"SaveImageTask"という名前のバックグラウンドで動作し、カメラから撮影した画像を保存する内部クラスがあります。Intentを「MainActivity2」という別のクラスに使用して、ここに画像を表示します。コードされています画像を保存して表示する

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); 
    } 
} 

はしかし、それは、私がイメージを取っていますし、それは間の5秒の遅れで表示している、あまりにも多くの時間を要しどのようにしてより効率的にする?

答えて

0

ソリューションは、(前に別のクラスにテントを呼び出す)、それを保存する前に画像を表示した

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); 
     } 
    } 
関連する問題