1

My application has a RealtiveLayout() with an ImageView() and two 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" />

How do I save the ImageView() with the two textviews to the SD card of a device on a single Button click?

It Assistors
  • 998
  • 2
  • 14
  • 29
  • might help: http://stackoverflow.com/questions/10362259/save-imageview-to-android-emulator-gallery – Riad Dec 25 '14 at 08:07

2 Answers2

2

For the image: first, you need to add a permission:

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

then, get the bitmap:

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

create your file:

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

write the bitmap:

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

For the text, you need just to write the text to the file.

Miki Franko
  • 687
  • 3
  • 7
1

for the text:

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();
        }
    }
Miki Franko
  • 687
  • 3
  • 7