I am trying to get a picture from the Internet, save it to my phone and send it right away through any instant messenger or mailer without opening the gallery to select a picture.
Activity:
public class MainActivity extends AppCompatActivity { private String remoteUrlString = "https://upload.wikimedia.org/wikipedia/commons/b/b5/Extreme_QR_code_to_Wikipedia_mobile_page.png"; private Button btnStart; private Context context; private static final int PERMISSIONS_REQUEST_SENDER = 14222; private File file = null; public File getFile() { return file; } public void setFile(File file) { this.file = file; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnStart = findViewById(R.id.button); btnStart.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { resendImage(); } }); } private void resendImage() { if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { System.out.println("PERMISSION_GRANTED == WRITE_EXTERNAL_STORAGE: true"); Picasso.with(this) .load(remoteUrlString) .into(target); sendImg(); } else { System.out.println("PERMISSION_GRANTED == WRITE_EXTERNAL_STORAGE: false"); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSIONS_REQUEST_SENDER); } } } private Target target = new Target() { @Override public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) { new Thread(new Runnable() { @Override public void run() { try { file = new File(Environment.getExternalStorageDirectory().getPath() + "/Extreme_QR_code_to_Wikipedia_mobile_page.jpg"); FileOutputStream ostream = null; ostream = new FileOutputStream(file); bitmap.compress(Bitmap.CompressFormat.JPEG, 75, ostream); ostream.close(); galleryAddPic(file); setFile(file); } catch (Exception e) { e.printStackTrace(); } } }).start(); } @Override public void onBitmapFailed(Drawable errorDrawable) { } @Override public void onPrepareLoad(Drawable placeHolderDrawable) { if (placeHolderDrawable != null) { } } }; private void galleryAddPic(File file) { Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); Uri contentUri = Uri.fromFile(file); mediaScanIntent.setData(contentUri); this.sendBroadcast(mediaScanIntent); } private void sendImg() { Intent sendIntent = new Intent(); sendIntent.setAction(Intent.ACTION_SEND); sendIntent.putExtra(Intent.EXTRA_STREAM, getFile()); sendIntent.setType("image/jpg"); startActivity(sendIntent); } @RequiresApi(api = Build.VERSION_CODES.M) @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { if (requestCode == PERMISSIONS_REQUEST_SENDER && grantResults.length == 1) { if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { resendImage(); } else { requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSIONS_REQUEST_SENDER); } } } } Manifest:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.devtolife.myapp"> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.USE_CREDENTIALS" /> <uses-permission android:name="android.permission.GET_ACCOUNTS" /> <uses-permission android:name="android.permission.READ_PROFILE" /> <uses-permission android:name="android.permission.READ_CONTACTS" /> <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/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> Layout:
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="START" /> </android.support.constraint.ConstraintLayout> When the choice of applications for sending is opened, the picture is not in the attachment: 
Checked the gallery - the picture was loaded and normally opens. What could be?
sendImg();without waiting for loading - in an intent, most likelynullsent. I wonder what is the gain fromPicassoin such a task? - woesssPicasso- but I refused, because I received the 405 error from the server I needed. Picaso works fine - the picture is downloading. - V.Marchnullinintent. How to make thesendImg()call occur after the picture is loaded? Inpicassofound acallback()used only when inserting a photo into anImageView. - V.MarchsetFile(file);. With Picasso, the code turns out to be confusing + the image at the output is not original, but recoded and, accordingly, the cost of decoding and compression. - woessssendImg()in any case, it will work out before the picture is loaded onto the phone - as a result,nullin theintent. - V.March