2

我有一个用于 imageView 位图图片的保存按钮。当我单击它时,我会得到 3 个相同图像的副本,但其中 2 个是 0 字节。这是图像:

在此处输入图像描述

这是我的代码:

public class Photo_gallery extends Activity implements OnClickListener{

    Button save;
    ImageView slika;


    final File myDir = new File("/sdcard/Pictures/sexyImages");
    boolean success = false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.photo_gallery);

        InitializeVars();

    }

    private void InitializeVars() {

        slika = (ImageView) findViewById(R.id.imageView1);
        save = (Button) findViewById(R.id.bSave);


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

            }
        });

    }

第二个问题....如何刷新图库应用程序?我知道我必须在某处实现此代码,但我不知道如何:

Environment.getExternalStorageDirectory();
4

0 回答 0