使用textView将drawable保存到Gallery中

时间:2014-01-08 13:40:40

标签: android android-imageview android-gallery android-drawable

我有一个名为tmp1的drawable,它在imageView上设置。

final ImageView blankcard= (ImageView) findViewById(R.id.blankcard); 

blankcard.setBackgroundResource(R.drawable.tmp1);

我将一些从editText中获取的文本设置为tmp1到Intent:

stv1.setText(Name+" ");
stv2.setText(Company);
stv3.setText("  "+Phone);
stv4.setText(Mobile+" ");
stv5.setText("  "+Address);
stv6.setText("  "+Email);
stv7.setText(Website+"  ");
stv8.setText(Title+" ");

crdlogoframe.setImageBitmap(BitmapFactory.decodeFile(pre_img_path1));
crdqrframe.setImageBitmap(BitmapFactory.decodeFile(pre_img_path2));

为了将.tmp1保存到我的图库中,我将其转换为Bitmap并保存目录。然后我有:

Bitmap icon = BitmapFactory.decodeResource(context.getResources(),R.drawable.tmp1);
try {
FileOutputStream out = new FileOutputStream(Environment.getExternalStorageDirectory()+ "/Android/data/MapCards/templates.jpg/");
icon.compress(Bitmap.CompressFormat.PNG, 90, out);
out.close();
} catch (Exception e) {
e.printStackTrace();
}

Toast.makeText(getApplicationContext(), "Saved Directory!", Toast.LENGTH_SHORT).show();

这有助于我只将drawable保存到文件夹中。有没有办法用文本保存它,也可能是图像。感谢。

这是布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/mainpagebck"
android:orientation="vertical"
android:padding="25dp" >
<RelativeLayout
    android:layout_width="264dp"
    android:layout_height="200dp"
    android:layout_marginTop="15dp" >

    <ImageView
        android:id="@+id/blankcard"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:adjustViewBounds="true"
        android:scaleType="fitStart" />



    <TextView
        android:id="@+id/crdCompany"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="Company"
        android:textColor="#20526d"
        android:textSize="15sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/crdWebsite"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="16dp"
        android:text="website"
        android:textColor="#20526d"
        android:textSize="9sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/crdAddress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/crdPhone"
        android:layout_alignParentLeft="true"
        android:text="address"
        android:textColor="#20526d"
        android:textSize="13sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/crdEmail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/crdWebsite"
        android:layout_alignBottom="@+id/crdWebsite"
        android:layout_alignParentRight="true"
        android:text="email"
        android:textColor="#20526d"
        android:textSize="9sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/crdPhone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/crdWebsite"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="17dp"
        android:text="phone"
        android:textColor="#20526d"
        android:textSize="13sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/crdName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/crdCompany"
        android:text="name"
        android:textColor="#20526d"
        android:textSize="13sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/crdTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/crdName"
        android:layout_marginTop="12dp"
        android:text="title"
        android:textColor="#20526d"
        android:textSize="13sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/crdMobile"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/crdTitle"
        android:layout_marginTop="5dp"
        android:text="mobile"
        android:textColor="#20526d"
        android:textSize="13sp"
        android:textStyle="bold" />

<FrameLayout
        android:id="@+id/frameLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="25dp" >

<ImageView
        android:id="@+id/crdlogoframe"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_alignParentLeft="true"               
        android:src="@drawable/border"
        android:visibility="visible"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter" />
    </FrameLayout>

    <ImageView
        android:id="@+id/crdqrframe"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_alignTop="@+id/frameLayout1"
        android:layout_toRightOf="@+id/frameLayout1"
        android:src="@drawable/border"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter" />

</RelativeLayout>

我如何嵌套画布:

public class Template extends Activity {

FrameLayout frame;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.template);

frame  = (FrameLayout)findViewById(R.id.frame); 
context=this;

final ImageView blankcard= (ImageView) findViewById(R.id.blankcard);

btn1.setOnLongClickListener(new OnLongClickListener() {

        @Override
        public boolean onLongClick(View arg0) {

             frame.setDrawingCacheEnabled(true);
             frame.buildDrawingCache(true);
             Bitmap bmpText = Bitmap.createBitmap(frame.getDrawingCache());
             Canvas canvas = new Canvas(bmpText);
             Paint p = new Paint();
             canvas.drawText("Hi I am Here", 25, 25, p);
             frame.draw(canvas);
             canvas.save();
             frame.setDrawingCacheEnabled(false);

             try {
                   FileOutputStream out = new FileOutputStream(Environment.getExternalStorageDirectory()+ "/Android/data/MapCards/templates.jpg/");
                   bmpText.compress(Bitmap.CompressFormat.PNG, 90, out);
                   out.close();
                } catch (Exception e) {
                   e.printStackTrace();
                }


            Toast.makeText(getApplicationContext(), "Saved Directory!", Toast.LENGTH_SHORT).show();
            return false;
        }
});

具有FrameLayout的新活动:

public class SaveCard extends Activity{

FrameLayout frame;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.savecard);

    frame  = (FrameLayout)findViewById(R.id.frame);

}}

新框架布局:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/frame"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.8" >

<TextView
    android:id="@+id/selected"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text= "Hi I am Here"
     />

<ImageView
    android:id="@+id/image_view"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:adjustViewBounds="true"
    android:scaleType="matrix" />
</FrameLayout>

上次错误:不幸的是,MapCard已经停止:当我长按按钮时。

logcat的:

01-09 09:45:39.915: E/AndroidRuntime(4852): FATAL EXCEPTION: main
01-09 09:45:39.915: E/AndroidRuntime(4852): java.lang.NullPointerException
01-09 09:45:39.915:   E/AndroidRuntime(4852):atcom.example.mapcard.Template$9.onLongClick(Template.java:436)
01-09 09:45:39.915: E/AndroidRuntime(4852):     at android.view.View.performLongClick(View.java:3627)
01-09 09:45:39.915: E/AndroidRuntime(4852):     at android.widget.TextView.performLongClick(TextView.java:9532)
01-09 09:45:39.915: E/AndroidRuntime(4852):     at android.view.View$CheckForLongPress.run(View.java:14241)
01-09 09:45:39.915: E/AndroidRuntime(4852):     at android.os.Handler.handleCallback(Handler.java:605)
01-09 09:45:39.915: E/AndroidRuntime(4852):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-09 09:45:39.915: E/AndroidRuntime(4852):     at android.os.Looper.loop(Looper.java:137)
01-09 09:45:39.915: E/AndroidRuntime(4852):     at android.app.ActivityThread.main(ActivityThread.java:4507)
01-09 09:45:39.915: E/AndroidRuntime(4852):     at java.lang.reflect.Method.invokeNative(Native Method)
01-09 09:45:39.915: E/AndroidRuntime(4852):     at java.lang.reflect.Method.invoke(Method.java:511)
01-09 09:45:39.915: E/AndroidRuntime(4852):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
01-09 09:45:39.915: E/AndroidRuntime(4852):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
01-09 09:45:39.915: E/AndroidRuntime(4852):     at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案