-1

okay so I wanna share an image which is loaded into image view . for that I made a method and but after executing the program I am getting some error will link the error and the code which I am using to share the image. I think its a runtime permission error but not sure what to do

ps= yes I have mentioned read and write storage permissions in manifest

''' error Log '''

  java.io.FileNotFoundException: /sys/class/thermal/thermal_message/sconfig (Permission denied)
        at java.io.FileOutputStream.open0(Native Method)
        at java.io.FileOutputStream.open(FileOutputStream.java:308)
        at java.io.FileOutputStream.<init>(FileOutputStream.java:238)
        at java.io.FileOutputStream.<init>(FileOutputStream.java:119)
        at java.io.FileWriter.<init>(FileWriter.java:63)
        at org.pixelexperience.thermalcontroller.ThermalProfiles.writeProfile(ThermalProfiles.java:76)
        at org.pixelexperience.thermalcontroller.Receiver.onReceive(Receiver.java:45)
        at android.app.ActivityThread.handleReceiver(ActivityThread.java:3418)
        at android.app.ActivityThread.access$1200(ActivityThread.java:201)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1668)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6746)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:495)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
1564713554.991 2659-2659 E/ThermalController:ThermalProfiles: Failed to write profile
    java.io.FileNotFoundException: /sys/class/thermal/thermal_message/sconfig (Permission denied)
        at java.io.FileOutputStream.open0(Native Method)
        at java.io.FileOutputStream.open(FileOutputStream.java:308)
        at java.io.FileOutputStream.<init>(FileOutputStream.java:238)
        at java.io.FileOutputStream.<init>(FileOutputStream.java:119)
        at java.io.FileWriter.<init>(FileWriter.java:63)
        at org.pixelexperience.thermalcontroller.ThermalProfiles.writeProfile(ThermalProfiles.java:76)
        at org.pixelexperience.thermalcontroller.Receiver.onReceive(Receiver.java:45)
        at android.app.ActivityThread.handleReceiver(ActivityThread.java:3418)
        at android.app.ActivityThread.access$1200(ActivityThread.java:201)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1668)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6746)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:495)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
1564713556.288 2659-2659 E/ThermalController:ThermalProfiles: Failed to write profile
    java.io.FileNotFoundException: /sys/class/thermal/thermal_message/sconfig (Permission denied)
        at java.io.FileOutputStream.open0(Native Method)
        at java.io.FileOutputStream.open(FileOutputStream.java:308)
        at java.io.FileOutputStream.<init>(FileOutputStream.java:238)
        at java.io.FileOutputStream.<init>(FileOutputStream.java:119)
        at java.io.FileWriter.<init>(FileWriter.java:63)
        at org.pixelexperience.thermalcontroller.ThermalProfiles.writeProfile(ThermalProfiles.java:76)
        at org.pixelexperience.thermalcontroller.Receiver.onReceive(Receiver.java:45)
        at android.app.ActivityThread.handleReceiver(ActivityThread.java:3418)
        at android.app.ActivityThread.access$1200(ActivityThread.java:201)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1668)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6746)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:495)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
1564713560.780 2659-2659 E/ThermalController:ThermalProfiles: Failed to write profile
    java.io.FileNotFoundException: /sys/class/thermal/thermal_message/sconfig (Permission denied)
        at java.io.FileOutputStream.open0(Native Method)
        at java.io.FileOutputStream.open(FileOutputStream.java:308)
        at java.io.FileOutputStream.<init>(FileOutputStream.java:238)
        at java.io.FileOutputStream.<init>(FileOutputStream.java:119)
        at java.io.FileWriter.<init>(FileWriter.java:63)
        at org.pixelexperience.thermalcontroller.ThermalProfiles.writeProfile(ThermalProfiles.java:76)
        at org.pixelexperience.thermalcontroller.Receiver.onReceive(Receiver.java:45)
        at android.app.ActivityThread.handleReceiver(ActivityThread.java:3418)
        at android.app.ActivityThread.access$1200(ActivityThread.java:201)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1668)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6746)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:495)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
1564713564.021 12113-12113 E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.akatski.memoii, PID: 12113
    android.os.FileUriExposedException: file:///storage/emulated/0/Download/share_image_1564713563904.png 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:4587)
        at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:767)
        at android.app.Activity.startActivityForResult(Activity.java:4545)
        at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:754)
        at android.app.Activity.startActivity(Activity.java:4906)
        at android.app.Activity.startActivity(Activity.java:4874)
        at com.akatski.memoii.UMWFullSizeImage.getLocalBitmapUri(UMWFullSizeImage.java:102)
        at com.akatski.memoii.UMWFullSizeImage.access$100(UMWFullSizeImage.java:25)
        at com.akatski.memoii.UMWFullSizeImage$1.onClick(UMWFullSizeImage.java:45)
        at android.view.View.performClick(View.java:6597)
        at android.view.View.performClickInternal(View.java:6574)
        at android.view.View.access$3100(View.java:778)
        at android.view.View$PerformClick.run(View.java:25889)
        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:6746)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:495)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
1564713564.130 2659-2659 E/ThermalController:ThermalProfiles: Failed to write profile
    java.io.FileNotFoundException: /sys/class/thermal/thermal_message/sconfig (Permission denied)
        at java.io.FileOutputStream.open0(Native Method)
        at java.io.FileOutputStream.open(FileOutputStream.java:308)
        at java.io.FileOutputStream.<init>(FileOutputStream.java:238)
        at java.io.FileOutputStream.<init>(FileOutputStream.java:119)
        at java.io.FileWriter.<init>(FileWriter.java:63)
        at org.pixelexperience.thermalcontroller.ThermalProfiles.writeProfile(ThermalProfiles.java:76)
        at org.pixelexperience.thermalcontroller.Receiver.onReceive(Receiver.java:45)
        at android.app.ActivityThread.handleReceiver(ActivityThread.java:3418)
        at android.app.ActivityThread.access$1200(ActivityThread.java:201)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1668)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6746)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:495)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
1564713564.456 12257-12257 W/.akatski.memoi: JIT profile information will not be recorded: profile file does not exits.
1564713564.458 12257-12257 W/.akatski.memoi: JIT profile information will not be recorded: profile file does not exits.
1564713624.389 2659-2659 E/ThermalController:ThermalProfiles: Failed to write profile
    java.io.FileNotFoundException: /sys/class/thermal/thermal_message/sconfig (Permission denied)
        at java.io.FileOutputStream.open0(Native Method)
        at java.io.FileOutputStream.open(FileOutputStream.java:308)
        at java.io.FileOutputStream.<init>(FileOutputStream.java:238)
        at java.io.FileOutputStream.<init>(FileOutputStream.java:119)
        at java.io.FileWriter.<init>(FileWriter.java:63)
        at org.pixelexperience.thermalcontroller.ThermalProfiles.writeProfile(ThermalProfiles.java:76)
        at org.pixelexperience.thermalcontroller.Receiver.onReceive(Receiver.java:45)
        at android.app.ActivityThread.handleReceiver(ActivityThread.java:3418)
        at android.app.ActivityThread.access$1200(ActivityThread.java:201)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1668)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6746)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:495)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

''' Code '''

private void getLocalBitmapUri(ImageView imageView, TextView caption) {

        ProgressDialog dialog = new ProgressDialog(mContext);
        dialog.setMessage("Preparing your stuff");
        dialog.show();
        // Extract Bitmap from ImageView drawable
        Drawable drawable = imageView.getDrawable();
        Bitmap bmp = null;
        if (drawable instanceof BitmapDrawable) {
            bmp = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
        } else {
            // null
            dialog.dismiss();
            Toast.makeText(mContext, "Error occurred\nPlease try again", Toast.LENGTH_SHORT).show();
        }
        // Store image to default external storage directory
        Uri bmpUri = null;

        try {
            File file = new File(Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_DOWNLOADS), "share_image_" +System.currentTimeMillis() + ".png");
            // above commented file name is because i want to replace the single file everytime
            file.getParentFile().mkdirs();
            FileOutputStream out = new FileOutputStream(file);
            bmp.compress(Bitmap.CompressFormat.PNG, 100, out);
            out.close();
            bmpUri = Uri.fromFile(file);

            // considering I have the file URI
            if (bmpUri != null) {
                // Construct a ShareIntent with link to image
                Intent shareIntent = new Intent();
                shareIntent.setAction(Intent.ACTION_SEND);
                try {
                    shareIntent.putExtra(Intent.EXTRA_TEXT, "Caption : " + caption.getText().toString() + "\n\nDownload memoi for tons of other memes :)\nLink : "+ mContext.getResources().getString(R.string.app_name));
                } catch (Exception e) {
                    shareIntent.putExtra(Intent.EXTRA_TEXT, "Download memoi for tons of other memes :)\nLink : " + mContext.getResources().getString(R.string.app_name));
                }

                shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
                shareIntent.setType("image/*");
                // Launch sharing dialog for image
                dialog.dismiss();
                mContext.startActivity(Intent.createChooser(shareIntent, "Share smiles via"));
            } else {
                dialog.dismiss();
                Toast.makeText(mContext, "Error occurred\nPlease try again", Toast.LENGTH_SHORT).show();
            }


        } catch (IOException e) {
            e.printStackTrace();
            dialog.dismiss();
            Toast.makeText(mContext, "Error occurred\nPlease try again", Toast.LENGTH_SHORT).show();
        }
    }

''' The code i used to get Run time permision '''

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (ContextCompat.checkSelfPermission(mContext, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions((Activity) mContext, new String[]{
                            Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
                } else {

                    getLocalBitmapUri(umw_ImageView);
                }  
  • For only sharing file you do not need `WRITE_EXTERNAL_STORAGE` permission .. Just save your file to cache dir by using `getCacheDir()`.. See [This](https://stackoverflow.com/a/35285667/4168607). – ADM Aug 02 '19 at 03:53
  • You are trying to "Share" it. Do you mean save it to storage? Are you getting permission dialog? – Richard Dapice Aug 02 '19 at 03:56
  • I have attached listener to a button, as soon as I click on the button the app crashes – Neeraj Butola Aug 02 '19 at 04:01

2 Answers2

1

If you are targeting Android API 24 or higher, private File URI resources (file:///) cannot be shared. We must instead wrap the File object as a content provider (content://) using the FileProvider

Declare your FileProvider in AndroidManifest.xml

<application>

  <provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="com.example.fileprovider"
    android:exported="false"
    android:grantUriPermissions="true">
      <meta-data
          android:name="android.support.FILE_PROVIDER_PATHS"
          android:resource="@xml/fileprovider" />
  </provider>

</application>

Create fileprovider.xml inside res/xml folder

<?xml version="1.0" encoding="utf-8"?>
<paths>

    <external-path name="images" path="Downloads" />

</paths>

Get the Uri for the image file

Uri imageUri = getUriForFile(getContext(), "com.example.fileprovider", imageFile);

Use this Uri to share the image outside your app. For more details follow the link above. And also to share the image in your external storage you need to get file READ/WRITE permissions.

Hussain
  • 1,243
  • 12
  • 21
0

You can use this code for getting Uri from File

val imgUri =FileProvider.getUriForFile(it, "${activity.packageName}.file_provider", file)

In Android manifest for AndroidX

    <application>
      .....
        <provider
          android:name="androidx.core.content.FileProvider"
          android:authorities="${applicationId}.file_provider"
          android:exported="false"
          android:grantUriPermissions="true">
            <meta-data
              android:name="android.support.FILE_PROVIDER_PATHS"
              android:resource="@xml/provider_paths" />
        </provider>
      .....
    </application>

Create an XML path depends on your file path

<?xml version="1.0" encoding="utf-8"?>
<paths>
  <external-files-path
    name="files"
    path="/" />
  <external-path
    name="external_files"
    path="." />
</paths>
Harsh Mittal
  • 449
  • 3
  • 10