使用textview()将imageview()保存到设备

时间:2014-12-25 07:59:28

标签: android android-layout textview imageview android-sdcard

我的应用程序有一个带有ImageView()的RealtiveLayout()和两个TextView()

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="300dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:src="@drawable/second_splash" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/imageView1"
    android:layout_centerHorizontal="true"
    android:gravity="center"
    android:text="Top Text"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textSize="35sp"
    android:textStyle="bold"
    android:shadowColor="@color/butbg"
    android:shadowRadius="20"
    />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/imageView1"
    android:layout_centerHorizontal="true"
    android:gravity="center"
    android:shadowColor="@color/butbg"
    android:shadowRadius="20"
    android:text="Down Text"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textSize="35sp"
    android:textStyle="bold" />

如何通过单击按钮将带有两个文本视图的ImageView()保存到设备的SD卡中?

2 个答案:

答案 0 :(得分:2)

对于图像: 首先,您需要添加权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

然后,获取位图:

 BitmapDrawable drawable = (BitmapDrawable) mImageView1.getDrawable();
 Bitmap bitmap = drawable.getBitmap();

创建你的文件:

 File sdCardDirectory = Environment.getExternalStorageDirectory();
 File image = new File(sdCardDirectory, "test.png");

写下位图:

boolean success = false;

// 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",
                Toast.LENGTH_LONG).show();
    } else {
        Toast.makeText(getApplicationContext(),
                "Error during image saving", Toast.LENGTH_LONG).show();
    }

对于文本,您只需将文本写入文件即可。

答案 1 :(得分:1)

的文字:

public void writefile()
{
    File externalStorageDir = Environment.getExternalStorageDirectory();
    File myFile = new File(externalStorageDir , "yourfilename.txt");

    if(myFile.exists())
    {
       try
       {

    FileOutputStream fostream = new FileOutputStream(myFile);
    OutputStreamWriter oswriter = new OutputStreamWriter(fostream); 
    BufferedWriter bwriter = new BufferedWriter(oswriter);   
    bwriter.write("Hi welcome ");
    bwriter.newLine();            
    bwriter.close(); 
    oswriter.close(); 
    fostream.close();
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
}
    else
    {
        try {
            myFile.createNewFile();
        }
        catch (IOException e) 
        {
            e.printStackTrace();
        }
    }