7

这是我的代码,我希望保存图像按钮只是将图像保存到画廊。

package com.nk_apps.hip.hop.lyric.wallpapers;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class Wallpapers extends Activity implements OnClickListener {
}
    ImageView display;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.wallpapers);

        display = (ImageView) findViewById(R.id.IVDisplay);
        ImageView image1 = (ImageView) findViewById(R.id.IVimage1);
        ImageView image2 = (ImageView) findViewById(R.id.IVimage2);
        ImageView image3 = (ImageView) findViewById(R.id.IVimage3);
        ImageView image4 = (ImageView) findViewById(R.id.IVimage4);
        ImageView image5 = (ImageView) findViewById(R.id.IVimage5);
        ImageView image6 = (ImageView) findViewById(R.id.IVimage6);
        ImageView image7 = (ImageView) findViewById(R.id.IVimage7);
        ImageView image8 = (ImageView) findViewById(R.id.IVimage8);
        ImageView image9 = (ImageView) findViewById(R.id.IVimage9);
        Button saveImage = (Button) findViewById(R.id.bSaveImage);

        image1.setOnClickListener(this);
        image2.setOnClickListener(this);
        image3.setOnClickListener(this);
        image4.setOnClickListener(this);
        image5.setOnClickListener(this);
        image6.setOnClickListener(this);
        image7.setOnClickListener(this);
        image8.setOnClickListener(this);
        image9.setOnClickListener(this);
        saveImage.setOnClickListener(this);
    }

    public void onClick(View v) {
        // TODO Auto-generated method stub

        switch (v.getId()) {

        case R.id.IVimage1:
            display.setImageResource(R.drawable.aotl);
            break;
        case R.id.IVimage2:
            display.setImageResource(R.drawable.ball_so_hard);
            break;
        case R.id.IVimage3:
            display.setImageResource(R.drawable.eye);
            break;
        case R.id.IVimage4:
            display.setImageResource(R.drawable.faded);
            break;
        case R.id.IVimage5:
            display.setImageResource(R.drawable.hiii_power);
            break;
        case R.id.IVimage6:
            display.setImageResource(R.drawable.i_invented_swag);
            break;
        case R.id.IVimage7:
            display.setImageResource(R.drawable.lifes_a_bitch);
            break;
        case R.id.IVimage8:
            display.setImageResource(R.drawable.mack_truck);
            break;
        case R.id.IVimage9:
            display.setImageResource(R.drawable.opposite);
            break;
        case R.id.bSaveImage:

            break;
        }

    } }

我想知道在最后一个案例下应该放什么。如果我不应该把它放在那里,我能被告知到底是什么吗?

感谢您的帮助,真的很难接受一切。

4

3 回答 3

14

在图库中保存任何图像可以通过以下方式完成:

MediaStore.Images.Media.insertImage(getContentResolver(), yourBitmap, yourTitle , yourDescription);
于 2012-08-26T22:52:01.363 回答
0

在 onClick 侦听器中,您可以使用它来将图像保存到图库,

FrameLayout mainLayout = (FrameLayout) findViewById(R.id.frame);
                    Random fCount = new Random();
                    int roll = fCount.nextInt(600) + 1;
                    File file = new File(Environment
                            .getExternalStorageDirectory()
                            + File.separator
                            + "/your_folder_name/" + String.valueOf(roll) + ".png");

                    Bitmap b = Bitmap.createBitmap(mainLayout.getWidth(),
                            mainLayout.getHeight(), Bitmap.Config.ARGB_8888);
                    Canvas c = new Canvas(b);
                    mainLayout.draw(c);
                    FileOutputStream fos = null;
                    try {
                        fos = new FileOutputStream(file);
                        if (fos != null) {
                            b.compress(Bitmap.CompressFormat.PNG, 90, fos);
                            fos.close();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
于 2012-08-26T22:51:35.363 回答
0

简单地说,您需要一个将图像传递给它的函数。查看完整代码如何以编程方式将图像保存到 android 中的画廊

private void saveImageToExternalStorage(Bitmap finalBitmap) {
        String root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString();
        File myDir = new File(root + "/saved_images_1");
        myDir.mkdirs();
        Random generator = new Random();
        int n = 10000;
        n = generator.nextInt(n);
        String fname = "Image-" + n + ".jpg";
        File file = new File(myDir, fname);
        if (file.exists())
            file.delete();
        try {
            FileOutputStream out = new FileOutputStream(file);
            finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
            out.flush();
            out.close();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
}

在您的 onClick 事件中调用您的函数并将您的图像作为位图传递给函数。

switch (v.getId()) {

        case R.id.IVimage1:
           saveImageToExternalStorage(img1);
       break;

       case R.id.IVimage2:
           saveImageToExternalStorage(img2);
       break;

       case R.id.IVimage3:
           saveImageToExternalStorage(img3);
       break;
}
于 2017-03-17T09:46:37.500 回答