1

We are trying to make an app that lets the user upload an image, write text over it, and then save the new image with the text.

Our current implementation is to use an ImageView to hold the image, then use TextViews to write on top of it.

What would be the best way to save the image as a whole? Below is our current code.

Thanks for the help! :)

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_generator);

    final EditText topTextInput = (EditText)findViewById(R.id.editTopText);
    final EditText bottomTextInput = (EditText)findViewById(R.id.editBottomText);
    final TextView topTextView = (TextView)findViewById(R.id.topText);
    final TextView bottomTextView = (TextView)findViewById(R.id.bottomText);

    imageView = (ImageView)findViewById(R.id.imageView2);
    Button pickImage = (Button) findViewById(R.id.button2);

    pickImage.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
            photoPickerIntent.setType("image/*");
            startActivityForResult(photoPickerIntent, SELECT_PHOTO);
        }
    });

    topTextInput.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            topTextView.setText(topTextInput.getText() + "");
        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    });

    bottomTextInput.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            bottomTextView.setText(bottomTextInput.getText() + "");
        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent);

    switch(requestCode) {
        case SELECT_PHOTO:
            if(resultCode == RESULT_OK){
                try {
                    final Uri imageUri = imageReturnedIntent.getData();
                    final InputStream imageStream = getContentResolver().openInputStream(imageUri);
                    final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
                    imageView.setImageBitmap(selectedImage);
                    imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }

            }
    }
}
Daniel Fullerton
  • 153
  • 1
  • 11

1 Answers1

0

use this:

  public Bitmap takeScreenshot() {
        View rootView = getView();
        rootView.setDrawingCacheEnabled(true);
        rootView.buildDrawingCache(true);
        Bitmap b1 = Bitmap.createBitmap(rootView.getDrawingCache(true));
        rootView.setDrawingCacheEnabled(false); // clear drawing cache
        return b1;
    }

and to save the bitmap to image:

public void saveBitmap(Bitmap bitmap) {
    if(bitmap==null){
        return;
    }
    File imagePath1 = new File(savePath,"screenshot.jpg");
    FileOutputStream fos;
    try {
        fos = new FileOutputStream(imagePath1);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
        fos.flush();
        fos.close();
    } catch (IOException e) {
        Log.e("GREC", e.getMessage(), e);
    }
}
JAAD
  • 12,349
  • 7
  • 36
  • 57