1 year ago

#165169

test-img

Nandini Agarwal

Saved image in paint application is not sharing & showing "Sharing failed, please try again later toast is showing"

"Sharing failed, please try again." toast is showing on clicking on the apps to share the image. The image is saving in my phone but it can't be shared. Made the xml directory & new resource file path.xml , added this to the mainfest file & created fun for sharing the image.But it is not working.

Manifest file.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.nandini.android.drawdoodle">

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.DrawDoodle">

        <activity
            android:name=".MainActivity"
            android:screenOrientation="portrait"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

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

    </application>
</manifest>

path.xml file in xml directory

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">

    <external-path
        name="Captured"
        path="Android/data/com.nandini.android.drawdoodle/files"/>
</paths>

MainActivity.kt file

    class MainActivity : AppCompatActivity() {
    
private var share_btn:ImageButton?=null

        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
            share_btn=findViewById(R.id.share_btn)
        
        fun isStorageAllowed():Boolean{
            val result=ContextCompat.checkSelfPermission(this,Manifest.permission.READ_EXTERNAL_STORAGE)
            return result==PackageManager.PERMISSION_GRANTED
        }
    
        fun requestStoragePermission(){
            if(ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.READ_EXTERNAL_STORAGE)){
                showRationaleDialog("Draw Doodle","Draw Doodle requires access to External Storage.")
            }else{
                requestPermission.launch(arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE))
            }
        }
    
    private fun getBitmapFromView(view:View): Bitmap? {
        val returnedBitmap=Bitmap.createBitmap(view.width,view.height,Bitmap.Config.ARGB_8888)
        val canvas=Canvas(returnedBitmap)
        val bgDrawable=view.background
        if(bgDrawable !=null){
            bgDrawable.draw(canvas)
        }else{
            canvas.drawColor(Color.WHITE)
        }
        view.draw(canvas)
        return returnedBitmap
    }
        private suspend fun saveBitmapFile(mBitmap: Bitmap?):String{
            var result =""
            withContext(Dispatchers.IO){
                if(mBitmap !=null){
                    try {
                        val bytes=ByteArrayOutputStream()
                        mBitmap.compress(Bitmap.CompressFormat.PNG,90,bytes)
    
                        val f = File(externalCacheDir?.absoluteFile?.toString()+ File.separator+"Draw_Doodle"+System.currentTimeMillis()/1000+".png")
    
                        val fo=FileOutputStream(f)
                        fo.write(bytes.toByteArray())
                        fo.close()
    
                        result=f.absolutePath
    
                        runOnUiThread{
                            cancelProgressDialog()
                            if(result.isNotEmpty()){
                                Toast.makeText(this@MainActivity,"File saved successfully : $result",Toast.LENGTH_LONG).show()
                                share_btn?.setOnClickListener{shareImage(result)}
                            }else{
                                Toast.makeText(this@MainActivity,"Something went wrong.",Toast.LENGTH_LONG).show()
                            }
                        }
                    }
                    catch (e:Exception){
                        result=""
                        e.printStackTrace()
                    }
                }
            }
            return result
        }
    
        private fun shareImage(result:String){
             MediaScannerConnection.scanFile(this, arrayOf(result),null){
                 path,uri->
                 val shareIntent=Intent()
                 shareIntent.action=Intent.ACTION_SEND
                 shareIntent.putExtra(Intent.EXTRA_STREAM,uri)
                 shareIntent.type="image/png"
                 startActivity(Intent.createChooser(shareIntent,"Share"))
             }
        }
    }

android

kotlin

android-activity

file-sharing

share-intent

0 Answers

Your Answer

Accepted video resources