当前位置: 动力学知识库 > 问答 > 编程问答 >

android - How to Save and Share an Image of Viewpager?

问题描述:

I want to save and share Image of Viewpager

I've done these till now, but yet I can't share / save images

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 file

<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>

It would be great if anybody could help me

Thank you!

网友答案:

Regarding how to share this link will you and regarding saving an image on to sdcard this link will help you in solving your problem.

Edit:

Write your sharing and saving code in pager fragment( In the fragment Itself). Let say you have written methods saveImage() and shareImage() in your fragment

Now get your current fragment in activity as

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

and now call your methods from fragment as

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

Hope you understand now

分享给朋友:
您可能感兴趣的文章:
随机阅读: