繁体   English   中英

如何将图像从可绘制资源保存到SD卡?

[英]How to save image from drawable resource to sd card?

所以,我有一个像下面的图片一样的视图。 另外,我将添加一个保存按钮。 您还可以看到我的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>
  void Save(){  
String folder = "/sdcard/Pictures/MyAppFolder";
       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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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