本文介绍了如何从ViewPager保存图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有ViewPager的活动。我用它来刷卡的图像。我也有一个保存按钮,我需要保存当前图像到SD卡。但是,我得到一个错误。下面是我的XML文件:
<?XML版本=1.0编码=UTF-8&GT?;
<的RelativeLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools
机器人:ID =@ + ID / rl_view_pager
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:可点击=真
机器人:可聚焦=真
机器人:focusableInTouchMode =真正的>< android.support.v4.view.ViewPager
机器人:ID =@ + ID / view_pager
机器人:layout_width =match_parent
机器人:layout_height =match_parent/> <按钮
机器人:ID =@ + ID / BSAVE
机器人:layout_width =80dp
机器人:layout_height =40dp
机器人:layout_alignParentTop =真
机器人:layout_marginLeft =15dp
机器人:layout_marginTop =15dp
机器人:文本=保存
机器人:layout_alignParentLeft =真
机器人:TEXTSIZE =24sp
机器人:填充=0dp
机器人:背景=@绘制/按钮/> < / RelativeLayout的>
这是我的类:
公共类Photo_gallery扩展活动实现OnClickListener { 按钮来保存; 最终文件MYDIR =新的文件(Environment.getExternalStorageDirectory()getAbsolutePath()+/图片/图片/);
布尔成功= FALSE;
@覆盖
保护无效的onCreate(捆绑savedInstanceState){ super.onCreate(savedInstanceState); 的setContentView(R.layout.photo_gallery); 节省=(按钮)findViewById(R.id.bSave); 最后ViewPager viewPager =(ViewPager)findViewById(R.id.view_pager);
ImagePagerAdapter适配器=新ImagePagerAdapter();
viewPager.setAdapter(适配器); save.setOnClickListener(新OnClickListener(){ 公共无效的onClick(视图v){ 最后随机发生器=新的随机();
INT N = 10000;
N = generator.nextInt(N);
最后弦乐FNAME =StyleMe-+ N +巴纽; myDir.mkdirs(); 文件图像=新的文件(MYDIR,FNAME); BitmapDrawable绘制=(BitmapDrawable)viewPager.getBackground();
位图的位图= drawable.getBitmap(); // EN code中的文件作为一个PNG图像。
FileOutputStream中outStream;
尝试{ outStream =新的FileOutputStream(图片);
bitmap.com preSS(Bitmap.Com pressFormat.PNG,100,outStream);
/ * 100,以保持图像的质量完全* / outStream.flush();
outStream.close();
成功= TRUE;
}赶上(FileNotFoundException异常五){
e.printStackTrace();
}赶上(IOException异常五){
e.printStackTrace();
} 如果(成功){
Toast.makeText(getApplicationContext(),图像保存在/ SD卡/图片/ SexyImages成功,
Toast.LENGTH_LONG).show();
}其他{
Toast.makeText(getApplicationContext(),
图像时出错节约,Toast.LENGTH_LONG).show();
}
sendBroadcast(新意图(Intent.ACTION_MEDIA_MOUNTED,Uri.parse
(文件://
+ Environment.getExternalStorageDirectory()))); }
}); } 私有类ImagePagerAdapter扩展PagerAdapter {
私人诠释[] = mImages新INT [] {
R.drawable.p1,
R.drawable.p2,
R.drawable.p3,
。
。
。
。
R.drawable.p108
}; @覆盖
公众诠释的getCount(){
返回mImages.length;
} @覆盖
公共布尔isViewFromObject(查看视图,Object对象){
返回查看==((ImageView的)对象);
} @覆盖
公共对象instantiateItem(ViewGroup中的容器,INT位置){
上下文的背景下= Photo_gallery.this;
ImageView的ImageView的=新ImageView的(上下文);
INT填充= context.getResources()。getDimensionPixelSize(
R.dimen.padding_medium);
imageView.setPadding(填充,填充,填充,填充);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageView.setImageResource(mImages [位置]);
((ViewPager)容器).addView(ImageView的,0);
返回ImageView的;
} @覆盖
公共无效destroyItem(ViewGroup中的容器,INT位置,Object对象){
((ViewPager)容器).removeView((ImageView的)对象);
}
} 公共无效的onClick(查看为arg0){
// TODO自动生成方法存根 }}
我得到的错误在这条线(显示java.lang.NullPointerException):
位图位= drawable.getBitmap();
解决方案
你为什么不这样做这样的:
按钮保存Clicklistener:
INT CURRENTITEM = viewPager.getCurrentItem();
可绘制可绘制= getResource.getDrawable(mImages [CURRENTITEM]);
位图BM =((BitmapDrawable)绘制).getBitmap();
I have an activity with ViewPager. I'm using it to swipe images. I also have a save button, and I need to save the current image to SD card. But I get an error. Here's my XML file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rl_view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true" >
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<Button
android:id="@+id/bSave"
android:layout_width="80dp"
android:layout_height="40dp"
android:layout_alignParentTop="true"
android:layout_marginLeft="15dp"
android:layout_marginTop="15dp"
android:text="Save"
android:layout_alignParentLeft="true"
android:textSize="24sp"
android:padding="0dp"
android:background="@drawable/buttons" />
</RelativeLayout>
And here's my class:
public class Photo_gallery extends Activity implements OnClickListener{
Button save;
final File myDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Pictures/Images/");
boolean success = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.photo_gallery);
save = (Button) findViewById(R.id.bSave);
final ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
ImagePagerAdapter adapter = new ImagePagerAdapter();
viewPager.setAdapter(adapter);
save.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
final Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
final String fname = "StyleMe-" + n + ".png";
myDir.mkdirs();
File image = new File(myDir, fname);
BitmapDrawable drawable = (BitmapDrawable) viewPager.getBackground();
Bitmap bitmap = drawable.getBitmap();
// Encode the file as a PNG image.
FileOutputStream outStream;
try {
outStream = new FileOutputStream(image);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
/* 100 to keep full quality of the image */
outStream.flush();
outStream.close();
success = true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (success) {
Toast.makeText(getApplicationContext(), "Image saved with success at /sdcard/Pictures/SexyImages",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(),
"Error during image saving", Toast.LENGTH_LONG).show();
}
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse
("file://"
+ Environment.getExternalStorageDirectory())));
}
});
}
private class ImagePagerAdapter extends PagerAdapter {
private int[] mImages = new int[] {
R.drawable.p1,
R.drawable.p2,
R.drawable.p3,
.
.
.
.
R.drawable.p108
};
@Override
public int getCount() {
return mImages.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
Context context = Photo_gallery.this;
ImageView imageView = new ImageView(context);
int padding = context.getResources().getDimensionPixelSize(
R.dimen.padding_medium);
imageView.setPadding(padding, padding, padding, padding);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageView.setImageResource(mImages[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
}
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
}
I get error on this line (java.lang.NullPointerException):
Bitmap bitmap = drawable.getBitmap();
解决方案
why didn't you do like that : on button Save Clicklistener :
int currentItem =viewPager.getCurrentItem();
Drawable drawable = getResource.getDrawable(mImages[currentItem]);
Bitmap bm =((BitmapDrawable) drawable).getBitmap();
这篇关于如何从ViewPager保存图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!