1

I'm an android beginner with little knowledge of coding. I've implemented a save button in my viewfippler gallery but I'm getting two errors on this line " Bitmap bitmap = getBitmapFromImageView(ImageView imageView);" in the saveimage() method. The compiler is saying there's a ")" expected and there's an illegal start of expression on the line specified above. The relevant code is below.

ViewFlipper.java

    public class ViewFlipperActivity extends Activity {
        @Override
        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        findViewById(R.id.btnSave).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                saveimage();
            }
        });

       ........

}
    public Bitmap getBitmapFromImageView(ImageView imageView) {
        int viewWidth = imageView.getWidth();
        int viewHeight = imageView.getHeight();
        Bitmap bitmap = Bitmap.createBitmap(viewWidth, viewHeight, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        imageView.layout(0, 0, viewWidth, viewHeight);
        imageView.draw(canvas);
        return bitmap;
    }


    public static void saveimage(){


        Bitmap bitmap = getBitmapFromImageView(ImageView imageView);

        File f =new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/HD GOSPEL LOCKSCREENS");
        if(!f.exists())
        {
            f.mkdirs();
        }
        f = new File(f.getAbsolutePath(),
                String.valueOf(System.currentTimeMillis()) +"hdgospelLockScreen.jpg");
        if(!f.exists())
        {
            try {
                f.createNewFile();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        try {
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(f));
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
   .......

}

user3135837
  • 47
  • 1
  • 8

1 Answers1

1

Your syntax is incorrect. Change this line: Bitmap bitmap = getBitmapFromImageView(ImageView imageView); to Bitmap bitmap = getBitmapFromImageView(imageView); where imageView is an ImageView object.

EDIT: Also, your static function saveImage should take in the ImageView object:

public static void saveImage(ImageView imageView) {
Mike Ortiz
  • 4,031
  • 4
  • 27
  • 54
  • You must create the ImageView first or pass it to the static function as I describe in my edit above. – Mike Ortiz Jan 04 '14 at 23:42
  • How would I pass it to the static function ? – user3135837 Jan 04 '14 at 23:49
  • 1
    Your method should not necessarily have to be static. – Nathan Walters Jan 04 '14 at 23:59
  • Ok I'll keep that in mind. I've put saveimage(ImageView imageView); in my save onClick listener but its still giving me errors – user3135837 Jan 05 '14 at 00:03
  • Where is your ImageView defined? Is it in your xml? You need to grab a reference to it with findViewById and then you can pass that ImageView into your saveImage function, which as @NathanWalters need not be a static function. – Mike Ortiz Jan 05 '14 at 00:11
  • Your syntax is still wrong. You need to pass an ImageView object to your method. – Nathan Walters Jan 05 '14 at 00:11
  • There are alot of imageviews within the viewflipper, how do I get the current imageview on display and pass that through? And yes the imageviews are in my main.xml – user3135837 Jan 05 '14 at 00:14
  • Well that's a rather more complex problem to solve. I don't have much experience with ViewFlippers, do you, @MikeOrtiz? – Nathan Walters Jan 05 '14 at 00:18
  • Each time the user swipes a new imageview is shown, the issue is how to grab the current image view object and passing it through to the saveimage() method – user3135837 Jan 05 '14 at 00:30
  • According to [this link](http://stackoverflow.com/questions/8265693/how-to-get-viewflippers-current-child-position), you can get the index of the view flipper using `flipper.getDisplayedChild();`. If you know the order and you know the index, you can call findViewById on the image resource to get an ImageView, which you then pass into saveImage(). – Mike Ortiz Jan 05 '14 at 00:33
  • It kind of makes sense but not entirely. That seems easier said than done. – user3135837 Jan 05 '14 at 00:47
  • You'll need to post more of your code to get a more concrete answer. I don't know what your ViewFlipper or xml code looks like. – Mike Ortiz Jan 05 '14 at 01:07
  • The main.xml full code is here http://pastebin.com/kXBPFtk6 and also the ViewFlipper.java full code is here http://pastebin.com/jBvQewn2 – user3135837 Jan 05 '14 at 01:11
  • 1
    Your code is utilizing some 3rd party libraries I haven't used before. My suggestion would be to maintain a constant list of your ImageViews. `List myList; myList.add(gift); myList.add(besober);` etc. Then, when you call `int index = flipper.getDisplayedChild();`, use that index to get your ImageView: `ImageView img = myList.get(index);`. Then, pass that img into your saveImage method. – Mike Ortiz Jan 05 '14 at 01:45
  • I've implemented what you've specified and the app is compiling but it crashes as soon as it gets pass the splash screen. Here is what i've implemented http://pastebin.com/WpcU2SvG – user3135837 Jan 05 '14 at 02:21
  • You need to initialize myList. Place `List myList = new ArrayList();` in your onCreate method before you add items to your list. In the future, you should always post the crash log, rather than just say that the app crashed. – Mike Ortiz Jan 05 '14 at 02:25
  • Thank you so much its working now. One last thing though, how can i get the saved images to appear in the users gallery as soon as they exit the app? – user3135837 Jan 05 '14 at 09:15