共享 viewPager 图像的意图
share intent for viewPager images
我使用 ViewPager
和 Picasso 制作了图像滑块。我在它下面添加了 XML 用于共享按钮,我还在其中实现了 onclick
,但我不知道之后该怎么做。
我想在按下分享按钮时分享图片。请帮我。我做不到。
在activity_main.xml中,我添加了viewpager和名为share的按钮。 Tts ID 是 share_image.
这是主要的activity:
public class MainActivity extends AppCompatActivity {
ViewPager viewPager;
private int[] imageUrls = new int[]{
R.raw.a,
R.raw.b,
R.raw.c //and many others
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = findViewById(R.id.view_pager);
ViewPageAdapter adapter = new ViewPageAdapter(this, imageUrls);
viewPager.setAdapter(adapter);
}
}
这个viewPagerAdapter:
public class ViewPageAdapter extends PagerAdapter {
private Context context;
private int[] imageUrls;
ViewPageAdapter(Context context, int[] imageUrls) {
this.context = context;
this.imageUrls = imageUrls;
}
@Override
public int getCount() {
return imageUrls.length;
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view == object;
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
ImageView imageView = new ImageView(context);
Picasso.get()
.load(imageUrls[position])
.into(imageView);
container.addView(imageView);
return imageView;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((View) object);
}
}
如果你的按钮设置好了,你可以在 OnClick 方法中添加这段代码,
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("image/*");
sharingIntent.putExtra(Intent.EXTRA_STREAM, imagePath);
startActivity(Intent.createChooser(sharingIntent, "Share Image via"));
创建分享意向
设置分享类型,分享方式
如果还没有创建要共享的项目
解析要分享的主题到创建的意图
解析要分享的消息正文
让用户有机会决定分享方式,
//intent.createChooser
由于没有用于 ViewPager 幻灯片的代码,我将不得不假设您在 ImageView 中显示图像。假设 ImageView 有一个 id picture_view.
当您在幻灯片视图(片段或 ViewGroup)中引用它时,您将其引用为:
ImageView pictureView = findViewById(R.id.picture_view)
要分享它,您必须做两件事:
- 要共享图像,您必须将其保存在本地。
您需要从 imageView 中获取位图。
Bitmap bmp = ((BitmapDrawable) pictureView.getDrawable()).getBitmap();
创建一个新文件。
然后您需要将位图的压缩版本写入输出流。
FileOutputStream out = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
out.close();
最后使用以下方法获取图像 uri:
// NOTE: authority here should match authority in manifest declaration
Uri photoURI = FileProvider.getUriForFile(context,
"authority",
file);
为了在您的隐式意图中使用保存的图像,您需要提供
一个Uri。让我们使用 photoURI。
intent.putExtra(Intent.EXTRA_STREAM, photoURI);
如果您仍然无法解决,请告诉我,我会提供帮助。
您的 ViewPager
适配器:
public class ViewPageAdapter extends PagerAdapter {
private Context context;
private int[] imageUrls;
public View currentImageView;
ViewPageAdapter(Context context, int[] imageUrls) {
this.context = context;
this.imageUrls = imageUrls;
}
@Override
public int getCount() {
return imageUrls.length;
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view == object;
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
ImageView view = new ImageView(context);
Picasso.get()
.load(imageUrls[position])
.into(view);
container.addView(view);
return view;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((View) object);
}
@Override
public void setPrimaryItem(ViewGroup container, int position, Object object) {
currentImageView = (View)object;
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
ViewPager viewPager;
ProgressBar progressBar;
private int[] imageUrls = new int[]{
R.drawable.after_cookie,
R.drawable.before_cookie,
R.drawable.androidparty
};
// private WebBackForwardList viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = findViewById(R.id.progress);
viewPager = findViewById(R.id.view_pager);
final ViewPageAdapter adapter = new ViewPageAdapter(this, imageUrls);
viewPager.setAdapter(adapter);
ImageView NextButton = findViewById(R.id.btnNext);
NextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
viewPager.setCurrentItem(getItem(+1), true); //getItem(+1) for next
}
});
ImageView PreviousButton = findViewById(R.id.btnPrevious);
PreviousButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
viewPager.setCurrentItem(getItem(-1), true);
}
});
ImageView btnShare = findViewById(R.id.btnShare);
btnShare.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (adapter.currentImageView == null) {
// Error: cannot get current image to share
return;
}
progressBar.setVisibility(View.VISIBLE);
Bitmap bitmap = Bitmap.createBitmap(adapter.currentImageView.getWidth(), adapter.currentImageView.getHeight(), Bitmap.Config.ARGB_8888);
adapter.currentImageView.draw(new Canvas(bitmap));
Uri uri = saveImage(bitmap);
shareImageUri(uri);
}
});
}
private void shareImageUri(Uri uri) {
Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_STREAM, uri);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setType("image/png");
startActivity(intent);
progressBar.setVisibility(View.GONE);
}
private Uri saveImage(Bitmap image) {
//TODO - Should be processed in another thread
File imagesFolder = new File(getCacheDir(), "images");
Uri uri = null;
try {
imagesFolder.mkdirs();
File file = new File(imagesFolder, "shared_image.png");
FileOutputStream stream = new FileOutputStream(file);
image.compress(Bitmap.CompressFormat.PNG, 90, stream);
stream.flush();
stream.close();
uri = FileProvider.getUriForFile(this, "com.example.recyclerview_codelabs.fileprovider", file);
} catch (IOException e) {
e.printStackTrace();
}
return uri;
}
private int getItem(int i) {
return viewPager.getCurrentItem() + i;
}
}
在你的activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/main_image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
tools:ignore="MissingConstraints" />
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:context="com.example.recyclerview_codelabs.ZoomableImageView" />
<ImageView
android:id="@+id/btnShare"
android:layout_width="0dp"
android:adjustViewBounds="true"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:background="@drawable/circle_button"
android:contentDescription="Share"
android:foreground="?android:attr/selectableItemBackground"
android:scaleType="centerCrop"
android:src="@drawable/ic_share_black_24dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/btnPrevious"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintHorizontal_chainStyle="spread"
app:layout_constraintStart_toStartOf="parent" />
<ImageView
android:id="@+id/btnPrevious"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:background="@drawable/circle_button"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/btnNext"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/btnShare"
app:srcCompat="@android:drawable/ic_media_previous" />
<ImageView
android:id="@+id/btnNext"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:layout_marginEnd="10dp"
android:background="@drawable/circle_button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/btnDownload"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/btnPrevious"
app:srcCompat="@android:drawable/ic_media_next" />
<ImageView
android:id="@+id/btnDownload"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:adjustViewBounds="true"
android:background="@drawable/circle_button"
android:scaleType="fitXY"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/btnNext"
app:srcCompat="@android:drawable/stat_sys_download" />
<ProgressBar
android:id="@+id/progress"
android:visibility="invisible"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_width="wrap_content"
tools:visibility="visible"
android:layout_height="wrap_content" />
</android.support.constraint.ConstraintLayout>
file_paths.xml 在 res>xml 文件夹中:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<cache-path name="shared_images" path="images/"/>
</paths>
</resources>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.recyclerview_codelabs">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:ignore="GoogleAppIndexingWarning">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.recyclerview_codelabs.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
</application>
</manifest>
我使用 ViewPager
和 Picasso 制作了图像滑块。我在它下面添加了 XML 用于共享按钮,我还在其中实现了 onclick
,但我不知道之后该怎么做。
我想在按下分享按钮时分享图片。请帮我。我做不到。
在activity_main.xml中,我添加了viewpager和名为share的按钮。 Tts ID 是 share_image.
这是主要的activity:
public class MainActivity extends AppCompatActivity {
ViewPager viewPager;
private int[] imageUrls = new int[]{
R.raw.a,
R.raw.b,
R.raw.c //and many others
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = findViewById(R.id.view_pager);
ViewPageAdapter adapter = new ViewPageAdapter(this, imageUrls);
viewPager.setAdapter(adapter);
}
}
这个viewPagerAdapter:
public class ViewPageAdapter extends PagerAdapter {
private Context context;
private int[] imageUrls;
ViewPageAdapter(Context context, int[] imageUrls) {
this.context = context;
this.imageUrls = imageUrls;
}
@Override
public int getCount() {
return imageUrls.length;
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view == object;
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
ImageView imageView = new ImageView(context);
Picasso.get()
.load(imageUrls[position])
.into(imageView);
container.addView(imageView);
return imageView;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((View) object);
}
}
如果你的按钮设置好了,你可以在 OnClick 方法中添加这段代码,
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("image/*");
sharingIntent.putExtra(Intent.EXTRA_STREAM, imagePath);
startActivity(Intent.createChooser(sharingIntent, "Share Image via"));
创建分享意向 设置分享类型,分享方式 如果还没有创建要共享的项目 解析要分享的主题到创建的意图 解析要分享的消息正文 让用户有机会决定分享方式, //intent.createChooser
由于没有用于 ViewPager 幻灯片的代码,我将不得不假设您在 ImageView 中显示图像。假设 ImageView 有一个 id picture_view.
当您在幻灯片视图(片段或 ViewGroup)中引用它时,您将其引用为:
ImageView pictureView = findViewById(R.id.picture_view)
要分享它,您必须做两件事:
- 要共享图像,您必须将其保存在本地。
您需要从 imageView 中获取位图。
Bitmap bmp = ((BitmapDrawable) pictureView.getDrawable()).getBitmap();
创建一个新文件。
然后您需要将位图的压缩版本写入输出流。
FileOutputStream out = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
out.close();
最后使用以下方法获取图像 uri:
// NOTE: authority here should match authority in manifest declaration
Uri photoURI = FileProvider.getUriForFile(context,
"authority",
file);
为了在您的隐式意图中使用保存的图像,您需要提供 一个Uri。让我们使用 photoURI。
intent.putExtra(Intent.EXTRA_STREAM, photoURI);
如果您仍然无法解决,请告诉我,我会提供帮助。
您的 ViewPager
适配器:
public class ViewPageAdapter extends PagerAdapter {
private Context context;
private int[] imageUrls;
public View currentImageView;
ViewPageAdapter(Context context, int[] imageUrls) {
this.context = context;
this.imageUrls = imageUrls;
}
@Override
public int getCount() {
return imageUrls.length;
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view == object;
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
ImageView view = new ImageView(context);
Picasso.get()
.load(imageUrls[position])
.into(view);
container.addView(view);
return view;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((View) object);
}
@Override
public void setPrimaryItem(ViewGroup container, int position, Object object) {
currentImageView = (View)object;
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
ViewPager viewPager;
ProgressBar progressBar;
private int[] imageUrls = new int[]{
R.drawable.after_cookie,
R.drawable.before_cookie,
R.drawable.androidparty
};
// private WebBackForwardList viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = findViewById(R.id.progress);
viewPager = findViewById(R.id.view_pager);
final ViewPageAdapter adapter = new ViewPageAdapter(this, imageUrls);
viewPager.setAdapter(adapter);
ImageView NextButton = findViewById(R.id.btnNext);
NextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
viewPager.setCurrentItem(getItem(+1), true); //getItem(+1) for next
}
});
ImageView PreviousButton = findViewById(R.id.btnPrevious);
PreviousButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
viewPager.setCurrentItem(getItem(-1), true);
}
});
ImageView btnShare = findViewById(R.id.btnShare);
btnShare.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (adapter.currentImageView == null) {
// Error: cannot get current image to share
return;
}
progressBar.setVisibility(View.VISIBLE);
Bitmap bitmap = Bitmap.createBitmap(adapter.currentImageView.getWidth(), adapter.currentImageView.getHeight(), Bitmap.Config.ARGB_8888);
adapter.currentImageView.draw(new Canvas(bitmap));
Uri uri = saveImage(bitmap);
shareImageUri(uri);
}
});
}
private void shareImageUri(Uri uri) {
Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_STREAM, uri);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setType("image/png");
startActivity(intent);
progressBar.setVisibility(View.GONE);
}
private Uri saveImage(Bitmap image) {
//TODO - Should be processed in another thread
File imagesFolder = new File(getCacheDir(), "images");
Uri uri = null;
try {
imagesFolder.mkdirs();
File file = new File(imagesFolder, "shared_image.png");
FileOutputStream stream = new FileOutputStream(file);
image.compress(Bitmap.CompressFormat.PNG, 90, stream);
stream.flush();
stream.close();
uri = FileProvider.getUriForFile(this, "com.example.recyclerview_codelabs.fileprovider", file);
} catch (IOException e) {
e.printStackTrace();
}
return uri;
}
private int getItem(int i) {
return viewPager.getCurrentItem() + i;
}
}
在你的activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/main_image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
tools:ignore="MissingConstraints" />
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:context="com.example.recyclerview_codelabs.ZoomableImageView" />
<ImageView
android:id="@+id/btnShare"
android:layout_width="0dp"
android:adjustViewBounds="true"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:background="@drawable/circle_button"
android:contentDescription="Share"
android:foreground="?android:attr/selectableItemBackground"
android:scaleType="centerCrop"
android:src="@drawable/ic_share_black_24dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/btnPrevious"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintHorizontal_chainStyle="spread"
app:layout_constraintStart_toStartOf="parent" />
<ImageView
android:id="@+id/btnPrevious"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:background="@drawable/circle_button"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/btnNext"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/btnShare"
app:srcCompat="@android:drawable/ic_media_previous" />
<ImageView
android:id="@+id/btnNext"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:layout_marginEnd="10dp"
android:background="@drawable/circle_button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/btnDownload"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/btnPrevious"
app:srcCompat="@android:drawable/ic_media_next" />
<ImageView
android:id="@+id/btnDownload"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:adjustViewBounds="true"
android:background="@drawable/circle_button"
android:scaleType="fitXY"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/btnNext"
app:srcCompat="@android:drawable/stat_sys_download" />
<ProgressBar
android:id="@+id/progress"
android:visibility="invisible"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_width="wrap_content"
tools:visibility="visible"
android:layout_height="wrap_content" />
</android.support.constraint.ConstraintLayout>
file_paths.xml 在 res>xml 文件夹中:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<cache-path name="shared_images" path="images/"/>
</paths>
</resources>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.recyclerview_codelabs">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:ignore="GoogleAppIndexingWarning">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.recyclerview_codelabs.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
</application>
</manifest>