我正在尝试从 Internet 获取图片,将其保存到我的手机中,然后通过任何信使或邮件程序立即发送,而无需打开图库来选择图片。
活动:
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);
}
}
}
}
显现:
<?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>
布局:
<?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 to send opens, the picture is not in the attachment:

我检查了图库 - 图片已加载并正常打开。可能是什么?
我不会通过代码,我会这样回答:
用 FutureTask 包装你的线程将很快让你摆脱这里的标准,AsyncTask 的代码多一点,但 Rx 看起来更好。与毕加索共舞会消失,显然不是为了那个,并且在将图片写入文件结束时,您通常可以调用您的乐趣 sendImg()。为了快速检查,您可以执行以下操作:
虽然这很糟糕,但 startActivity 总是在 MainThread 上发布......所以它应该可以工作。
使用
RXJava 2.1.6和的解决方案Picasso 2.5.2。简而言之:
用于
Picasso将文件上传到本地存储。为了维护流程的顺序,我将其全部包装在
RXJava2.在文件下载结束时,我将其传递给意图发送。
接下来,选择您要发送此照片 的应用程序(从列表中) 。
链接到 github:ImageResender
代码本身的详细信息:
清单:(注意提供者和权限)
布局:(只需一个按钮即可开始接收和传输图片)。
提供者:(
provider_paths.xml沿途躺着res/xml)Gradle:(包括库)。
活动:
注释: 1)
14222是取自天花板的临时数字。2) B
photoURL- 图片的链接是从互联网上任意获取的,没有宣传或冒犯任何人的目的。也许这会为某人节省几个小时。