如何保存和共享Viewpager的图像?

时间:2014-02-10 16:40:30

标签: android android-viewpager

我想保存并分享Viewpager的图片

到目前为止我已经完成了这些,但我无法分享/保存图像

ImageAdapter

public class ImageAdapter extends PagerAdapter {
Context context;
private int[] GalImages = new int[] {       
R.drawable.pic_1,
R.drawable.pic_2,
R.drawable.pic_3
.
.
.
};
ImageAdapter(Context context){
this.context=context;
}
@Override
public int getCount() {
return GalImages.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = new ImageView(context);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageView.setImageResource(GalImages[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
}

MainAcitivtiy Class

public class MainActivity extends Activity {


Bitmap bm;
String extStorageDirectory;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
ImageAdapter adapter = new ImageAdapter(this);
viewPager.setAdapter(adapter);




extStorageDirectory = Environment.getExternalStorageDirectory().toString();
}


public void share(View v){
    Intent share = new Intent(Intent.ACTION_SEND);
    share.setType("image/*");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();

bm.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    File f = new File(Environment.getExternalStorageDirectory() + File.separator+ "temporary_file.jpg");

    try {
        f.createNewFile();
        FileOutputStream fo = new FileOutputStream(f);
        fo.write(bytes.toByteArray());
    } catch (IOException e) {                       
            e.printStackTrace();
    }
    share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/temporary_file.jpg"));
    startActivity(Intent.createChooser(share, "Share Image"));


    }

public void save (View v){

      ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        FileOutputStream outp = null;
        File filename;
        try {
            String path = Environment.getExternalStorageDirectory().getAbsolutePath()+Nameoffolder;

            new File(path + "/folder/subfolder").mkdirs();
            filename = new File(path + "/folder/subfolder/image.jpg");

            FileOutputStream out = new FileOutputStream(filename);
            out.write(bytes.toByteArray());

            bm.compress(Bitmap.CompressFormat.JPEG, 90, out);
            out.flush();
            out.close();

            MediaStore.Images.Media.insertImage(getContentResolver(), filename.getAbsolutePath(), filename.getName(), filename.getName());

            Toast.makeText(getApplicationContext(), " Saved " , Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            e.printStackTrace();
        }

              }  


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

XML文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_alignParentTop="true" 
/>

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/view_pager"
android:layout_alignLeft="@+id/view_pager"
android:layout_marginBottom="29dp"
android:text="@string/save"
android:onClick="save" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_alignRight="@+id/view_pager"
android:text="@string/share"
android:onClick="share" />


</RelativeLayout>

如果有人能帮助我,那就太好了 谢谢!

1 个答案:

答案 0 :(得分:0)

关于如何分享this链接,您和将图像保存到SD卡this链接将有助于您解决问题。

编辑:

在寻呼机片段中编写共享和保存代码(在片段中自己)。假设您已经在片段中编写了saveImage()和shareImage()方法

现在将您当前的片段作为

进行活动

YourPagerFrament fr =(YourPagerFrament)yourPagerAdapter.instantiateItem(pager,pager.getCurrentItem());

现在从fragment调用你的方法

fr.saveImage(); fr.shareImage();

希望你现在明白