Posts

Showing posts from February, 2018

Share an image from an ImageView

To share an image from an ImageView in your Sketchware Android project, in VIEW area add a Button button1, which will act as a share button. In the event of on button1 click, use an add source directly block and put following code in it: Bitmap bm = ((android.graphics.drawable.BitmapDrawable) imageview1.getDrawable()).getBitmap(); try { java.io.File file = new java.io.File(getExternalCacheDir() + "/image.jpg"); java.io.OutputStream out = new java.io.FileOutputStream(file); bm.compress(Bitmap.CompressFormat.JPEG, 100, out); out.flush(); out.close(); } catch (Exception e) { showMessage(e.toString()); } Intent iten = new Intent(android.content.Intent.ACTION_SEND); iten.setType("*/*"); iten.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new java.io.File(getExternalCacheDir() + "/image.jpg"))); startActivity(Intent.createChooser(iten, "Send image")); Now save and run the project. You should be able to share the image from imageview1.

Using TextToSpeech in Sketchware

Image
To know how to use of TextToSpeech class in Sketchware, for reading the text from an EditText field, follow the instructions given below. 1. In your Sketchware Android project, and an EditText edittext1 , and a Button button1 . 2. In LOGIC area, in onCreate event, use add source directly block and in the block put the code given below. It should be the last block in onCreate event. t1=new android.speech.tts.TextToSpeech(getApplicationContext(), new android.speech.tts.TextToSpeech.OnInitListener() { @Override public void onInit(int status) { if(status != android.speech.tts.TextToSpeech.ERROR) { t1.setLanguage(Locale.UK); } } }); } android.speech.tts.TextToSpeech t1; private void nothing(){ This code initializes the TextToSpeech engine. It can speak only after initialization. If the user performs some other Activity after initialization, then it will not speak. It has to be initialized again for it to speak. So place this code again at appropriate place so that it is ini

Convert image to base64 String and vice versa

Image to base64 String To convert image to base64 String use the code given below. 1. Define a new ByteArrayOutputStream. java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream(); 2. Convert image in ImageView to Bitmap. Bitmap bm = ((android.graphics.drawable.BitmapDrawable) imageview1.getDrawable()).getBitmap(); Or convert image in drawable folder to Bitmap. Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.image); 3. Convert bitmap image to byte array and encode it to String. bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); byte[] imageBytes = baos.toByteArray(); String imageString = android.util.Base64.encodeToString(imageBytes, android.util.Base64.DEFAULT); Base64 String to image 1. Decode String to Byte array byte[] imageBytes = android.util.Base64.decode(imageString, android.util.Base64.DEFAULT); 2. Convert Byte array to Bitmap Bitmap decodedImage = BitmapFactory.decodeByteArray(imageBytes, 0, ima