0

I want to know if there is any efficient way to make app to app communication using IPC. I went to the guide of services that uses AIDL from the docs. But what I really want is to have an image to transfer between them.(Images are high quality) The only issue is android only let us use 1024 kB or even less to pass data through bundle. if you do more than that you'll end up with TransactionTooLargeException. Right now I'm compressing the image and passing base64 string between the two apps and it works fine. But sometimes some images can not be compressed at all. How can I do something like that. I'm compressing image using

bitmap.compress(imageformat=webp,quality=90,compress.nowrap)

the quality will reduce by 10 if I get TransactionTooLargeException. Any ideas on how to do something like that working on android?

But the thing is I don't want to open any other app. The image that I'm receiving will be processed and send a status to the image that was sent from the application. Like 'very cool image' 'very bad image'.in a string status.

Aidl link Thanks.

var quality = 100
    
    
    private fun sendImageToDevice(quality: Int, icon: Bitmap) {
        Log.e(TAG, "quality of image is $quality ")

        runOnUiThread {
            Toast.makeText(
                this@MainActivity,
                "Image Quality $quality",
                Toast.LENGTH_SHORT
            )
                .show()
        }



        if (quality > 0)
            try {
                mProcessImageService?.sendImageFormat(icon.toBase64(quality))
            } catch (e: TransactionTooLargeException) {
                e.message
                this.quality = quality - 20

                sendImageToDevice(this.quality, icon)
            }
        else {

        }
    }



fun Bitmap.toBase64(quality: Int): String {
    val outputStream = ByteArrayOutputStream()
    this.compress(Bitmap.CompressFormat.JPEG, quality, outputStream)
    val base64String: String = Base64.encodeToString(outputStream.toByteArray(), Base64.NO_WRAP)
    Log.d("MainActivity", "outputstream size is ${outputStream.size()}")
    return base64String
}
Ashutosh Soni
  • 855
  • 3
  • 9
  • 22
  • iirc, android wont allow inter process communication but I maybe wrong. instead of this, you might want to break the data into multiple chunks for this. And is it really two apps, you used bundle...it sounds to me like you are trying to do inter activity communication – Abhiroj Panwar Sep 12 '20 at 06:01
  • I've updated the question with link. Kindly see. – Ashutosh Soni Sep 12 '20 at 06:03
  • App to app communication with apps on the same device? – blackapps Sep 12 '20 at 06:58
  • Why encoding to base64? You have 30% more bytes to transfer then. – blackapps Sep 12 '20 at 06:59
  • Save your data to file and only transfer file path. – blackapps Sep 12 '20 at 06:59
  • `only issue is android only let us use 1024 kB or even less to pass data through bundle.` Please show your code as all is pretty vague. – blackapps Sep 12 '20 at 07:01
  • @blackapps updated with code. – Ashutosh Soni Sep 12 '20 at 07:08
  • Any reason not to react on my other remarks and questions? – blackapps Sep 12 '20 at 07:12
  • @blackapps sorry, encoding to base64 because I'm not using parcel for the bitmap in aidl files currently. Other apps might take leverage of the file uri security concerns. But I'll look into it. Nothing should be saved on the internal storage(shouldn't be accessed by filemanager) is it possible? – Ashutosh Soni Sep 12 '20 at 07:20
  • 1
    Save in getFilesDir() and use FileProvider to serve your file. Transfer the uri.toString() of the uri obtained from FileProvider for a file. I dont know if a read permisdion can be given else use your own content provider. – blackapps Sep 12 '20 at 07:40
  • Further you are not base64 encoding a bitmap but a jpg file. So i do not understand why you talk about a parcel or a parcel for a bitmap. – blackapps Sep 12 '20 at 07:42
  • 1
    If you are just doing image transfer between authorized apps: [AndroidX FileProvider](https://developer.android.com/training/secure-file-sharing/setup-sharing) or [custom Content Provider](https://developer.android.com/guide/topics/providers/content-provider-basics) – Morrison Chang Sep 12 '20 at 07:46
  • 1
    `If you are just doing image transfer` We will read that as `If you are just doing file transfer` @Morrison Chang – blackapps Sep 12 '20 at 07:59

0 Answers0