简体   繁体   中英

How to save images correctly to sd-card?

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();
        }

    }
   .......

}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM