2016-09-13 41 views
0

openCamera()が終了した直後にopenGallery()を呼び出すにはどうすればよいですか?私はカメラを使って写真を撮りたい、ギャラリーに保存して(私ができる)、そしてすぐに撮影した写真を選択するためのギャラリーを表示する。 openCamera()が呼び出しを終了すると、openGallery()は決して表示されません。私がopenCamera(); openGallery();と書いても動作しません。 openCamera()をスキップし、代わりにopenGallery()を呼び出します。私を助けてください。openCamera()が終了した直後にAndroidがopenGallery()を呼び出した直後

@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     Log.i("request", requestCode + " result " + resultCode + " intent " 
       + data); 
     super.onActivityResult(requestCode, resultCode, data); 
     if(requestCode == RESULT_SAVE_ITEM){ 
      clearItemContents(); 
     } 
     else if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) 
     { 
      itemImage.setScaleType(ImageView.ScaleType.FIT_XY); 
      photo = (Bitmap) data.getExtras().get("data"); 
      itemImage.setImageBitmap(getRoundedCornerBitmap(photo,20)); 
     } 


     else { 
      if (gallery == true) { 
       if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK 
         && null != data) { 
        Uri selectedImage = data.getData(); 
        String[] filePathColumn = { MediaStore.Images.Media.DATA }; 
        Cursor cursor = getContentResolver().query(selectedImage, 
          filePathColumn, null, null, null); 
        cursor.moveToFirst(); 
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
        String picturePath = cursor.getString(columnIndex); 
        cursor.close(); 
        imageArray = imageToByteArray(new File(picturePath)); 
        setImagePicture(picturePath); 
       } else { 

       } 
      } else if (gallery == false) { 
       File file = new File(cameraImagePath); 
       if (file.exists()) { 
        imageArray = imageToByteArray(file); 
        setImagePicture(cameraImagePath); 
       } else { 

       } 

      } else { 
       this.finish(); 
       startActivity(new Intent(this, AddProductActivity.class)); 
      }} 
    } 

    public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) 
    { 
     Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap 
       .getHeight(), Config.ARGB_8888); 
     Canvas canvas = new Canvas(output); 

     final int color = 0xff424242; 
     final Paint paint = new Paint(); 
     final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); 
     final RectF rectF = new RectF(rect); 
     final float roundPx = pixels; 

     paint.setAntiAlias(true); 
     canvas.drawARGB(0, 0, 0, 0); 
     paint.setColor(color); 
     canvas.drawRoundRect(rectF, roundPx, roundPx, paint); 

     paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); 
     canvas.drawBitmap(bitmap, rect, rect, paint); 

     return output; 
    } 


    private byte[] imageToByteArray(File file) { 
     try { 
      FileInputStream fis = new FileInputStream(file); 
      // create FileInputStream which obtains input bytes from a file in a 
      // file system 
      // FileInputStream is meant for reading streams of raw bytes such as 
      // image data. For reading streams of characters, consider using 
      // FileReader. 

      ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
      byte[] buf = new byte[1024]; 

      for (int readNum; (readNum = fis.read(buf)) != -1;) { 
       // Writes to this byte array output stream 
       bos.write(buf, 0, readNum); 
       System.out.println("read " + readNum + " bytes,"); 
      } 
      byte[] bytes = bos.toByteArray(); 
      return bytes; 
     } catch (IOException ex) { 
      ex.printStackTrace(); 
     } 
     return null; 
    } 

    private void setImagePicture(String photoPath) { 
     editTextItemPic.setVisibility(ImageView.GONE); 
     itemImage.setVisibility(ImageView.VISIBLE); 
     BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inPreferredConfig = Bitmap.Config.ARGB_8888; 
     Bitmap bitmap = BitmapFactory.decodeFile(photoPath, options); 
     itemImage.setImageBitmap(bitmap); 
    } 



    public void showDialog() { 

     builder.setTitle("Choose Action!"); 
     builder.setItems(dialogItems, new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int item) { 
       // Do something with the selection 
       if (item == 0) { 
        openGallery(); 
       } else if(item == 1) { 
        openCamera(); 


       }else{ 
        imageArray = null; 
        editTextItemPic.setVisibility(ImageView.VISIBLE); 
        itemImage.setVisibility(ImageView.GONE); 
       } 
      } 
     }); 
     AlertDialog alert = builder.create(); 
     alert.show(); 
    } 

    public void openGallery() { 
     AddProductActivity.gallery = true; 
     Intent i = new Intent(Intent.ACTION_PICK, 
       MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
     startActivityForResult(i, AddProductActivity.RESULT_LOAD_IMAGE); 
    } 

    public void openCamera() { 
     USE_CAMERA=1; 
     Intent cameraIntent=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
     startActivityForResult(cameraIntent, CAMERA_REQUEST); 

    } 

    private String appFolderCheckandCreate() { 

     String appFolderPath = ""; 
     File externalStorage =Environment.getExternalStorageDirectory(); 


     if (externalStorage.canWrite()) { 
      appFolderPath = "/storage/emulated/0/DCIM/Camera/"; 
      File dir = new File(externalStorage+"/storage/emulated/0/DCIM/Camera/"); 


      if (!dir.exists()) { 
       dir.mkdirs(); 
      } 

     } else { 

     } 

     return appFolderPath; 
    } 

    @SuppressLint("SimpleDateFormat") private String getTimeStamp() { 

     final long timestamp = new Date().getTime(); 

     final Calendar cal = Calendar.getInstance(); 
     cal.setTimeInMillis(timestamp); 

     final String timeString = new SimpleDateFormat("HH_mm_ss_SSS") 
       .format(cal.getTime()); 

     return timeString; 
    } 

    public void saveImage() 
    { 
     File filename; 
     try 
     { 
      USE_CAMERA=0; 
      String path = Environment.getExternalStorageDirectory().toString(); 

      new File(path + "/QSHelper").mkdirs(); 
      filename = new File(path + "/QSHelper/image"+LASTINSERTEDID+".png"); 

      FileOutputStream out = new FileOutputStream(filename); 

      photo.compress(Bitmap.CompressFormat.PNG, 100, out); 
      out.flush(); 
      out.close(); 

      MediaStore.Images.Media.insertImage(getContentResolver(), 
        filename.getAbsolutePath(), filename.getName(), 
        filename.getName()); 
     } catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 
    } 
+3

コール 'onActivityResult(中' openGallery() ')'カメラのコールバックでopenGallery()方法 – Soham

答えて

1

コール即ちonActivityResult

@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     Log.i("request", requestCode + " result " + resultCode + " intent " 
       + data); 
     super.onActivityResult(requestCode, resultCode, data); 
     if(requestCode == RESULT_SAVE_ITEM){ 
      clearItemContents(); 
     } 
     else if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) 
     { openGallery(); 
     } 
//...rest code 
} 
関連する問題