2015-03-31 4 views
0

Я хотел бы знать, как я могу контролировать, когда pciture взяты из камеры могут быть сохранены, прямо сейчас у меня есть этот код:Как сохранить изображение (сделанный с помощью камеры) вручную в Android

File storagePath = new File(Environment. 
        getExternalStorageDirectory() + "/PhotoAR/"); 
     storagePath.mkdirs(); 

     File myImage = new File(storagePath, 
        Long.toString(System.currentTimeMillis()) + ".jpg"); 

     try 
     { 
     FileOutputStream out = new FileOutputStream(myImage); 
     newImage.compress(Bitmap.CompressFormat.JPEG, 80, out); 


     out.flush(); 
     out.close(); 
     } 
     catch(FileNotFoundException e) 
     { 
     Log.d("In Saving File", e + "");  
     } 
     catch(IOException e) 
     { 
     Log.d("In Saving File", e + ""); 
     } 

Но сейчас предварительный просмотр не работает, я имею в виду, что изображение сохраняется в папке PhotoAR, но оно сохраняется автоматически, я бы хотел, чтобы у вас была кнопка или что-то еще, и сохраните или отмените вариант, есть ли способ улучшить это для достижения такого поведения?

Возможно, какой-нибудь пример?

благодарит заранее!

+0

после взятия фото –

+1

@MD почему вы сделали downvote? –

+0

Nono Я просто читаю ваш ответ, я заметил нисходящее движение, это потрясающий ответ, спасибо! – NeoVe

ответ

2

Создайте диалоговое окно с просьбой сохранить или отменить фотографию чуть выше кода (код для сохранения рисунка) и вызвать код, который вы описали только внутри прослушивателя onclick кнопки ok.

В следующем коде само приложение камеры по умолчанию запрашивает сохранить или отменить после захвата изображения диалогового Put

mCamera.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       if (taken_image == null) { 

        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
        Uri fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image 
        image_uri = fileUri; 
        intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name 
        startActivityForResult(intent, 1); 
        mEdit.setVisibility(View.VISIBLE); 
        dialog.cancel(); 
       } 

      } 
     }); 

@Override 
    public void onActivityResult(int requestCode, int resultCode, 
      final Intent data) { 

     super.onActivityResult(requestCode, resultCode, data); 

     if (resultCode == Activity.RESULT_OK && requestCode == 1) { 

      if (image_uri != null) 
       try { 
        ExifInterface exif = new ExifInterface(image_uri.getPath()); // Since API Level 5 
        int orientation = exif.getAttributeInt(
          ExifInterface.TAG_ORIENTATION, 
          ExifInterface.ORIENTATION_NORMAL); 
        // TODO 
        upload_image_hd = BitmapFactory.decodeFile(image_uri 
          .getPath()); 
        switch (orientation) { 
        case ExifInterface.ORIENTATION_ROTATE_90: 
         Rotate90Bitmap(upload_image_hd, 90); 
         break; 
        case ExifInterface.ORIENTATION_ROTATE_180: 
         Rotate180Bitmap(upload_image_hd, 180); 
         break; 
        case ExifInterface.ORIENTATION_ROTATE_270: 
         Rotate270Bitmap(upload_image_hd, 270); 
         break; 
        case ExifInterface.ORIENTATION_NORMAL: 
         RotateBitmap(upload_image_hd, 0); 
         break; 
        default: 
         RotateBitmap(upload_image_hd, 0); 
         break; 
        } 

       } catch (OutOfMemoryError e) { 
        Toast.makeText(getActivity(), 
          e + "\"memory exception occured\"", 
          Toast.LENGTH_LONG).show(); 

        taken_image = null; 
        round_Image = null; 
        upload_image_hd = null; 

       } catch (Exception e) { 
        Toast.makeText(getActivity(), e + "\"exception occured\"", 
          Toast.LENGTH_SHORT).show(); 
        taken_image = null; 
        round_Image = null; 
        upload_image_hd = null; 

       } 

public Bitmap RotateBitmap(Bitmap source, float angle) { 
     Matrix matrix = new Matrix(); 
     matrix.postRotate(angle); 

     round_Image = Bitmap.createBitmap(source, 0, 0, source.getWidth(), 
       source.getHeight(), matrix, true); 
     // get the base 64 string 
     imgString = Base64.encodeToString(getBytesFromBitmap(round_Image), 
       Base64.NO_WRAP); 
     uploadProfilePicture_bytes(); 
     return Bitmap.createBitmap(source, 0, 0, source.getWidth(), 
       source.getHeight(), matrix, true); 
    } 

    public Bitmap Rotate90Bitmap(Bitmap source, float angle) { 
     Matrix matrix = new Matrix(); 
     matrix.postRotate(angle); 

     round_Image = Bitmap.createBitmap(source, 0, 0, source.getWidth(), 
       source.getHeight(), matrix, true); 
     // get the base 64 string 
     imgString = Base64.encodeToString(getBytesFromBitmap(round_Image), 
       Base64.NO_WRAP); 
     uploadProfilePicture_bytes(); 
     return Bitmap.createBitmap(source, 0, 0, source.getWidth(), 
       source.getHeight(), matrix, true); 
    } 

public Bitmap Rotate180Bitmap(Bitmap source, float angle) { 
    Matrix matrix = new Matrix(); 
    matrix.postRotate(angle); 

    round_Image = Bitmap.createBitmap(source, 0, 0, source.getWidth(), 
      source.getHeight(), matrix, true); 
    // get the base 64 string 
    imgString = Base64.encodeToString(getBytesFromBitmap(round_Image), 
      Base64.NO_WRAP); 

    uploadProfilePicture_bytes(); 
    return Bitmap.createBitmap(source, 0, 0, source.getWidth(), 
      source.getHeight(), matrix, true); 
} 

public Bitmap Rotate270Bitmap(Bitmap source, float angle) { 
    Matrix matrix = new Matrix(); 
    matrix.postRotate(angle); 

    round_Image = Bitmap.createBitmap(source, 0, 0, source.getWidth(), 
      source.getHeight(), matrix, true); 
    // get the base 64 string 
    imgString = Base64.encodeToString(getBytesFromBitmap(round_Image), 
      Base64.NO_WRAP); 

    uploadProfilePicture_bytes(); 
    return Bitmap.createBitmap(source, 0, 0, source.getWidth(), 
      source.getHeight(), matrix, true); 
} 
+0

Я снимаюсь как разработчик Android, и это очень помогает мне! спасибо :) – NeoVe

+0

@NeoVe Извините! виноват. +1 вверх. –

+0

Большое спасибо @NeoVe –

Смежные вопросы