0

所以,我有一个如下图所示的视图。另外,我将添加一个保存按钮。您还可以查看我的 XML 文件。我正在使用此代码通过按下一个和上一个按钮从可绘制文件夹加载图像:

public void onClick(View v) {
        switch (v.getId()){

        case R.id.bNext:
            a++;
            setImage(a);
            break;
        case R.id.bPrevious:
            if(a>0)
            {
                a--;
                setImage(a);  
                break;
            }
        }
    }
    private void setImage(int a){
        if (a == 0)
        {
        slika.setImageResource(R.drawable.l2);
        a = 1;
        }
        else if (a == 1)
        {
        slika.setImageResource(R.drawable.l3);
        a = 2;
        }
        else if (a == 2)
        {
        slika.setImageResource(R.drawable.l4);
        a = 3;
        }
        else if (a == 3)
        {
        slika.setImageResource(R.drawable.l5);
        a = 4;
        }
        else if (a == 4)
        {
        slika.setImageResource(R.drawable.l6);
        a = 5;
        }
.
.
.
.
.
        else if (a == 55)
        {
        slika.setImageResource(R.drawable.l57);
        a = 56;
        }
        }

我需要保存当前在屏幕上的图像。我在 stackoverflow 上找到了一些答案,但这些都是针对具有已知名称的特定图像的。我不知道用户想要保存什么图像。

在此处输入图像描述

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:gravity="center">

                <ImageView
            android:id="@+id/imageView1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_centerHorizontal="true"/>

        <Button
            android:id="@+id/bMenu"
            android:layout_width="80dp"
            android:layout_height="35dp"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="120dp"
            android:background="@drawable/buttons"
            android:gravity="center"
            android:padding="0dp"
            android:text="Menu"
            android:textColor="#ffffff"
            android:textSize="23dp" />

        <Button
            android:id="@+id/bNext"
            android:layout_width="80dp"
            android:layout_height="35dp"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="220dp"
            android:background="@drawable/buttons"
            android:padding="0dp"
            android:text="Next"
            android:textSize="23dp"
            android:textColor="#ffffff" />

        <Button
            android:id="@+id/bPrevious"
            android:layout_width="80dp"
            android:layout_height="35dp"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="20dp"
            android:background="@drawable/buttons"
            android:gravity="center"
            android:padding="0dp"
            android:text="Previous"
            android:textColor="#ffffff"
            android:textSize="23dp" />

    </RelativeLayout>
4

1 回答 1

3
String folder = "/sdcard/Pictures/MyAppFolder";

void save(){  
    Imageview  view = (ImageView)findViewById(R.id.cachesView);
    view.buildDrawingCache(); 
    Bitmap yourBitmap = view.getDrawingCache();  
    final File myDir = new File(folder);
    myDir.mkdirs();
    final Random generator = new Random();
    int n = 10000;
    n = generator.nextInt(n);
    final String fname = "StyleMe-" + n + ".png";
    File file = new File(myDir, fname);
    if (file.exists()) {
        FileOutputStream out = new FileOutputStream(file);
        yourBitmap.compress(CompressFormat.JPEG, 100, out);
        out.flush();
        out.close();
        sendBroadcast(
            new Intent(Intent.ACTION_MEDIA_MOUNTED,
            Uri.parse("file://"+ Environment.getExternalStorageDirectory()))
        ); // this will refresh the gallery app.
        Toast.makeText(getApplication(), "Image Saved", Toast.LENGTH_SHORT).show();
    }
}

请参见

在您的课程中添加此方法,然后单击按钮放置

save();
于 2013-07-16T16:11:34.257 回答