2

I need to upload contact image to server but not able to get real path. Please anyone help me.

I'm getting below uri.

URI: content://com.android.contacts/display_photo/2,

Sachin
  • 1,307
  • 13
  • 23
  • Well use that uri to upload that image. Yoh dont need a real file path if that even existed. – greenapps Mar 20 '17 at 07:24
  • @greenapps When im trying to send image to server using multipart im getting File not found Exception. – Sachin Mar 20 '17 at 07:28
  • And? Which statement is causing that? Cannot have to do anything with multipart. But only happens if you try to use that content scheme as if it was a file path. – greenapps Mar 20 '17 at 07:32

1 Answers1

2

first get InputStream from Contact.Write the Inpustream in file and save as Image File. Finally upload that image path to server after success simply delete that Image File. See below code.

First I get InputStream from Contact by using Contact Id.

getContactInputStream("58");//58 is my contact id.

 public void getContactInputStream(String contactId)
{
    Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long.parseLong(contactId));
    InputStream stream = ContactsContract.Contacts.openContactPhotoInputStream(getContentResolver(), uri);
    saveContactImage(stream);
}

After getting InputStream write as a file in Internal Storage.

public void saveContactImage(InputStream inputStream) {
    try {
        File file = new File(Environment.getExternalStorageDirectory().getPath(), "contactImage.png");
        OutputStream output = new FileOutputStream(file);
        try {
            try {
                byte[] buffer = new byte[4 * 1024]; // or other buffer size
                int read;

                while ((read = inputStream.read(buffer)) != -1) {
                    output.write(buffer, 0, read);
                }
                output.flush();
            } finally {
                output.close();
            }
        } catch (Exception e) {
            e.printStackTrace(); // handle exception, define IOException and others
        }
        Log.d(TAG," Contact Image Path ===>"+file.getAbsolutePath());
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}

After successfully File Write then use that File Url to upload.

file.getAbsolutePath()// Output : /storage/emulated/0/contactImage.png

Following Permission are required for above Task :

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />

Another way without saving Image file in internal storage you can upload inputStream as FileInputStream using TypeFile class in Retrofit. For more info see the Link TypeFile in Retrofit for upload file as InputStream

Community
  • 1
  • 1
Magesh Pandian
  • 8,789
  • 12
  • 45
  • 60
  • 1
    `After getting InputStream write as a file in Internal Storage.`. That is a very bad advice. Once you have an InputStream you can use it to upload the image directly. An InputStream as replacement of a FileInputStream that OP uses now. That's all. – greenapps Mar 20 '17 at 10:28
  • 1
    No need to update your answer. In fact it is also bad to update your post with it as it is not your idea and everybody can read my comment. Instead i would have expected you to admit that it was indeed a bad idea. – greenapps Mar 20 '17 at 10:50