我正在使用 JSON 从 URL 加载一些图像,并在新的 Activity 中以完整尺寸打开它们。
在该活动中,我想通过使用
ShareActionProvider
来分享它们。
Stack Overflow 上的一些问题建议首先将它们下载为临时文件,但它不适用于我的代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_full_screen);
Intent i = getIntent();
path = i.getStringExtra(TAG_FILEIMG);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.full_screen, menu);
MenuItem item = menu.findItem(R.id.menu_item_share);
mShareActionProvider = (ShareActionProvider) item.getActionProvider();
mShareActionProvider.setShareIntent(createShareIntent());
return true;
}
public File getTempFile(Context context, String url) {
url = path;
try {
String fileName = Uri.parse(url).getLastPathSegment();
file = File.createTempFile(fileName, null, context.getCacheDir());
}catch (IOException e) {
e.printStackTrace();
}
return file;
}
private Intent createShareIntent() {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, file);
shareIntent.setType("image/*");
return shareIntent;
}
全屏.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.habitodigital.FullScreenActivity" >
<item
android:id="@+id/menu_item_share"
android:showAsAction="ifRoom"
android:title="@string/share"
android:actionProviderClass="android.widget.ShareActionProvider"/>
</menu>
path 是我用来通过使用
ImageLoader
加载图像的 JSON 图像路径。您正在此处创建一个空文件。
public File getTempFile(Context context, String url) {
url = path;
try {
String fileName = Uri.parse(url).getLastPathSegment();
file = File.createTempFile(fileName, null, context.getCacheDir());
}catch (IOException e) {
e.printStackTrace();
}
return file;
}
您应该做的是从网站下载图像并将其写入该文件。
您可以使用FileOutputStream在文件上写入。
尝试https://www.imageurl.dev/。为我解决了问题