App sharing button in sketchware | tutorial

App sharing button in sketchware | tutorial

How to add a app sharing button in sketchware ?

Hi friends welcome to another sketchware tutorial. Today I will show you, how to create a button that shares your app ina apk format. This tutorial is going to be very short and interesting. The coding part is very less and not very complex. Before that you can check this tutorial to share any text of your app. A common sharing button in sketchware.

Remember do not mess or change this codes. Unless you know coding. Editing code without proper knowledge may crash your app.

Follow this below steps

Step No. 1 : setup your main.xml file or the view

In sketchware you can use a image or any button to act as a button. I have used imageView to make the app sharing button in sketchware. You can can go with any option.

Now go to the ImageView on click event and set this code using the add source directly block. Code:

shareApplication();

Step No. 2 : create the required more blocks

Create a more block called share and paste the below code using the add source directly block.

}private void shareApplication() {
android.content.pm.ApplicationInfo app
getApplicationContext().getApplicationInfo();
String appLoc = app.sourceDir;
Intent intent = new
Intent(Intent.ACTION_SEND);
intent.setType("/");
java.io.File originalApk = new
java.io.File(appLoc);
try {
java.io.File tempFile = new java.io.File(getExternalCacheDir() + "/ExtractedApk");
if (!tempFile.isDirectory())
if (!tempFile.mkdirs())
return;
tempFile = new
java.io.File(tempFile.getPath() + "/" +"your app name.apk");
if (!tempFile.exists())
{
try{
if (!tempFile.createNewFile()){
return; }
}
catch (java.io.IOException e){}
}
java.io.InputStream in = new
java.io.FileInputStream (originalApk);
java.io.OutputStream out = new
java.io.FileOutputStream(tempFile);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
intent.putExtra(Intent.EXTRA_STREAM,
Uri.fromFile(tempFile));
startActivity(Intent.createChooser(intent, "Share app via"));
}
catch (java.io.IOException e)
{ showMessage(e.toString());
}
}
{

You can replace the name.apk with your app’s name. This name will be shown when you will share the app

Step No. 3 : setup on create event

Go to on create event and paste this code.

StrictMode.VmPolicy.Builder builder
new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
if(Build.VERSION.SDK_INT>=24){
try{
java.lang.reflect.Method m =
StrictMode.class.getMethod(
"disableDeathOnFileUriExposure");
m.invoke(null);
}
catch(Exception e){
showMessage(e.toString());
}
}

Now just run the application. Hope every thing will go fine. if you got any error, then try to remove the spaces from the codes. Maybe while coping the codes. spaces are also gets copied. And shows you error.

Also note that it will also save the application as an apk file in your local cached directory.

For me it is Storage/emulated/0/Android/data/com.android.newProject/cache/ExtractedApk

Please

Let me know your views, suggestions and problems in the comment section.

Thanks for reading.

This Post Has 5 Comments

  1. Adebisi-Lawal

    It brought out error asking me to add “;” to complete block statement.

    1. Kaushik

      Hi there please remember while coping code, check that whether you have copied the whole code or not. Sometimes when you long press and try to copy the code, the “;” symbol may not get copied.
      I think when copied this part “shareApplication();” you have missed that “;”. Please recheck and confirm whether you still get errors or not. Thank you

  2. Adebisi-Lawal

    I have done it. But when I built the apk. I was told no app cam perform the action

    1. Kaushik

      Ooo that’s good. You can share it here so that Other users can get benifit of it. Thanks.

Leave a Reply