0

I have been battling with a particular issue for over a week now. I want to be able to share images from my PicturesApp/File Explorer into my app and then post to a private server. Images are already being posted when the gallery is navigated to from inside the app with the help of a MultiImage Selector Library. I have tried all the answers i have come across on here to no avail including the renowned FileUtils Class referenced at here During the course of my research, I have discovered there are a lot of grey areas in terms of handling data in apps with the brevity (which eventually leads to ambiguity) of documentation and a consequent heap of unanswered questions in this regard. Now to some Code

    <provider
        android:name="android.support.v4.content.FileProvider"
        tools:replace= "android:authorities"
        android:authorities="com.myapp.PhotoViewerActivity"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            tools:replace= "android:resource"
            android:resource="@xml/file_paths" />
    </provider>

          <activity
        android:name="com.myapp.activity.AppActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.SEND" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="image/*" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.SEND_MULTIPLE" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="image/*" />
        </intent-filter>
        </activity>

Manifest

I included the File Provider as i want to know if that is overwriting my ability to read content:// uris or something..

where i handle the Intent;

            final Intent shareIntent = getIntent();
          if (shareIntent.getAction()!=null) {
                 final String intentAction = shareIntent.getAction();
                 final String intentFileType = shareIntent.getType();

                      if (intentAction.equals(Intent.ACTION_SEND)) {
                                 if (intentFileType.startsWith("image/")) {
                                    byte[] imageBytes = null;
                                    Uri uri = (Uri) shareIntent.getParcelableExtra(Intent.EXTRA_STREAM);
                                    try {
                                        InputStream inputStream = getContentResolver().openInputStream(uri);
                                        imageBytes= getBytes(inputStream);
                                    } catch (FileNotFoundException e) {
                                        e.printStackTrace();
                                    } catch (IOException e) {
                                        e.printStackTrace();
                                    }

                                    RequestBody requestFile = RequestBody.create(MediaType.parse("image/*"), imageBytes);
                                    image1 = MultipartBody.Part.createFormData("image1", "new_image", requestFile);
       }
    }
   }
     public byte[] getBytes(InputStream is) throws IOException{
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int buffSize =1024;
    byte[] buff = new byte[buffSize];
    int len=0;
    while((len =  is.read(buff))!= -1){
        baos.write(buff,0,len);
    }
    return baos.toByteArray();
}

Activity

Ps: I have already handled permissions. Like i said i can post pictures from the app presently. For this particular feature the server says "The File does not exist"

Itoooo
  • 1
  • 3

1 Answers1

0

On click of the gallery button, start startActivityForResult as follows:

startActivityForResult(new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI), GET_FROM_GALLERY);

Check this link

How to upload image from gallery in android

Sreejesh K Nair
  • 563
  • 1
  • 6
  • 16
  • I am not navigating to the gallery from inside the app. I am coming from the gallery or Pictures app to my app with the share button in the android gallery app. Thanks for the answer though – Itoooo Dec 26 '17 at 19:00