Search code examples
androidimagearraysandroid-image

How to download an image from server side to android using bytearray?


My Android colleague wants an image to be sent from Java Server side code, I tried using byte-array-output stream, which is the best way to send an image to Android client. How should the android code be to receive byte-array.


Solution

  • I have done this things by byte array. This is my code for your reference :

    String base64String = (String)sp.toString();
    byte[] mediaData = Base64.decode(base64String, 0);                              write(notesList.get(position).getContent(), mediaData);
                                            File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),
                                                    notesList.get(position).getContent());
                                            Uri path = Uri.fromFile(file);
                                            if(notesList.get(position).getContent().contains(".pdf"))
                                            {
                                                Intent pdfOpenintent = new Intent(Intent.ACTION_VIEW);
                                                pdfOpenintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                                pdfOpenintent.setDataAndType(path, "application/pdf");
                                                try {
                                                    context.startActivity(pdfOpenintent);
                                                } catch (ActivityNotFoundException e) {
                                                }
                                            }
                                            else if(notesList.get(position).getContent().contains(".jpg") || notesList.get(position).getContent().contains(".jpeg") || notesList.get(position).getContent().contains(".png"))
                                            {
                                                Intent intent = new Intent();
                                                intent.setAction(Intent.ACTION_VIEW);
                                                intent.setDataAndType(Uri.parse("file://" + path), "image/*");
                                                context.startActivity(intent);
                                            }
                                            else if(notesList.get(position).getContent().contains(".xls"))
                                            {
                                                PackageManager packageManager = context.getPackageManager();
                                                Intent intent = new Intent(Intent.ACTION_VIEW);
                                                intent.setType("application/*");
                                                List list = packageManager.queryIntentActivities(intent,
                                                    PackageManager.MATCH_DEFAULT_ONLY);
    
    
                                                if (list.size() > 0) {
                                                    intent.setDataAndType(path, "application/*");
                                                    context.startActivity(intent);
                                                }
                                            }
    

    Here, sp is the response from the server.