abhishek
abhishek

Reputation: 145

How can i share apk file in my app (Share the apk file of the same app)

App crashes while clicking button I made a simple app which share its apk file while clicked the button. I want to implement it in my primary app but don't know whats the error

package com.studenthelper.share;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import java.io.File;

public class MainActivity extends AppCompatActivity {
    Button shr;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        shr = (Button) findViewById(R.id.but);
        shr.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                ApplicationInfo api = getApplicationContext().getApplicationInfo();
                String filePath = api.sourceDir;

                Intent intent = new Intent(Intent.ACTION_SEND);
                intent.setType("application/vnd.android.package-archive");

                intent.putExtra(Intent.EXTRA_STREAM,
                        Uri.fromFile(new File(filePath)));
                startActivity(Intent.createChooser(intent, "Share Using"));
            }
        });
    }
}

log

2020-03-01 17:26:53.758 4741-4741/com.studenthelper.share E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.studenthelper.share, PID: 4741
    android.os.FileUriExposedException: file:///data/app/com.studenthelper.share-s51_qOKFxmQ8bo6zuBgGow%3D%3D/base.apk exposed beyond app through ClipData.Item.getUri()
        at android.os.StrictMode.onFileUriExposed(StrictMode.java:1978)
        at android.net.Uri.checkFileUriExposed(Uri.java:2371)
        at android.content.ClipData.prepareToLeaveProcess(ClipData.java:963)
        at android.content.Intent.prepareToLeaveProcess(Intent.java:10228)
        at android.content.Intent.prepareToLeaveProcess(Intent.java:10234)
        at android.content.Intent.prepareToLeaveProcess(Intent.java:10213)
        at android.app.Instrumentation.execStartActivity(Instrumentation.java:1669)
        at android.app.Activity.startActivityForResult(Activity.java:4590)
        at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:676)
        at android.app.Activity.startActivityForResult(Activity.java:4548)
        at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:663)
        at android.app.Activity.startActivity(Activity.java:4909)
        at android.app.Activity.startActivity(Activity.java:4877)
        at com.studenthelper.share.MainActivity$1.onClick(MainActivity.java:33)
        at android.view.View.performClick(View.java:6605)
        at android.view.View.performClickInternal(View.java:6582)
        at android.view.View.access$3100(View.java:778)
        at android.view.View$PerformClick.run(View.java:25897)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6692)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
2020-03-01 17:26:53.787 4741-4741/com.studenthelper.share I/Process: Sending signal. PID: 4741 SIG: 9

Upvotes: 1

Views: 379

Answers (1)

Zain
Zain

Reputation: 40830

android.os.FileUriExposedException is raised for newer APIs because the file URI scheme of newer APIs is different than that of old APIs.

To solve this replace the below line of code:

intent.putExtra(Intent.EXTRA_STREAM,
                    Uri.fromFile(new File(filePath)));

With:

intent.putExtra(Intent.EXTRA_STREAM, 
                    Uri.parse(filePath));

Upvotes: 1

Related Questions