1

我有一个 ViewPager 的活动。我正在使用它来滑动图像。我还有一个保存按钮,我需要将当前图像保存到 SD 卡。但我得到一个错误。这是我的 XML 文件:

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

这是我的课:

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

    }

}

我在这一行收到错误(java.lang.NullPointerException):

Bitmap bitmap = drawable.getBitmap();
4

1 回答 1

1

你为什么不这样做:在按钮 Save Clicklistener 上:

 int currentItem =viewPager.getCurrentItem();
  Drawable drawable = getResource.getDrawable(mImages[currentItem]);
  Bitmap bm =((BitmapDrawable) drawable).getBitmap();
于 2013-08-19T09:32:23.910 回答