保存位图不适用于dialogfragment

时间:2015-08-10 08:07:59

标签: android android-fragments bitmap

当我将片段中的工作代码复制到对话片段时突然无效。

on regular fragment:将位图保存在SD卡上。

on dialogfragment:不在sd卡上保存位图。

对话框片段:

    package com.example.smite.floater;

    import android.app.AlertDialog;
    import android.app.Dialog;
    import android.app.DialogFragment;
    import android.content.Context;
    import android.content.DialogInterface;
    import android.graphics.Bitmap;
    import android.graphics.Bitmap.CompressFormat;
    import android.graphics.drawable.BitmapDrawable;
    import android.os.Bundle;
    import android.os.Environment;
    import android.text.Layout;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.widget.EditText;
    import android.widget.ImageView;
    import android.widget.LinearLayout;
    import android.widget.TextView;

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;

    /**
     * Created by smite on 09/08/2015.
     */
    public class DialogEditText extends DialogFragment {

        TextView textView;

        ImageView img;

        Bitmap bitmap;

        BitmapDrawable drawable;

        EditText editText;

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {

            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

            LayoutInflater inflater = getActivity().getLayoutInflater();

            builder.setView(inflater.inflate(R.layout.dialogedittext, null));
            builder.setTitle("Pick a name");

            View creatorLayout = inflater.inflate(R.layout.creator, null);

            textView = (TextView)creatorLayout.findViewById(R.id.textView);
            img = (ImageView)creatorLayout.findViewById(R.id.img);
            editText =(EditText)getActivity().findViewById(R.id.edittext);

            builder.setPositiveButton("save", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                    textView.buildDrawingCache();
                    Log.d("d", "buildDrawingCache");
                    img.setImageBitmap(textView.getDrawingCache());
                    Log.d("d", "buildDrawingCache2");
                    drawable = (BitmapDrawable) img.getDrawable();
                    bitmap = drawable.getBitmap();
                    File sdCardDirectory = Environment.getExternalStorageDirectory();
                    File image = new File(sdCardDirectory, "edittext.png");
                    Log.d("d", "created image");

                    boolean success = false;

                    FileOutputStream outputStream;

                    try {
                        outputStream = new FileOutputStream(image);
                        Log.d("d", "created image");
 //line : 79            bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
                        outputStream.flush();
                        outputStream.close();
                    }catch (FileNotFoundException ex1) {
                        ex1.printStackTrace();
                    }
                    catch (IOException e) {
                        e.printStackTrace();
                    }

                }
            });
            builder.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    DialogEditText.this.getDialog().cancel();
                }
            });

        return builder.show();

    }
}

logcat错误:

08-10 04:06:56.566  16710-16710/com.example.smite.floater E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.smite.floater, PID: 16710
java.lang.NullPointerException
        at com.example.smite.floater.DialogEditText$1.onClick(DialogEditText.java:79)
        at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5001)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)

craetor xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/siganture_creator"
        android:hint="@string/signature..."
        android:imeOptions="actionDone|flagNoExtractUi"
        android:inputType="text"
        android:layout_marginTop="30dp"
        />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:gravity="center"
        android:layout_gravity="center">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/signature..."
                android:id="@+id/textView"
                android:textSize="50dp"
                />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/img"
                android:visibility="gone"
                />

    </LinearLayout>


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="save"
        android:id="@+id/save"
        android:layout_marginTop="10dp"
        />

</LinearLayout>

感谢帮助人(:

1 个答案:

答案 0 :(得分:1)

要从视图中获取位图,应绘制视图。在您的情况下,您正在夸大R.layout.creato布局,获取它的实例并且您没有使用setView()addView()方法来绘制该布局。换句话说,您只创建了creator布局的不同实例,但它还没有显示为什么位图为空。

尝试使用getActivity()方法来获取而不是creatorLayout变量。它应该对你有所帮助。它会在活动范围内找到视图。

查看以下编辑过的代码:

<强> 被修改

@覆盖             public Dialog onCreateDialog(Bundle savedInstanceState){

            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

            builder.setView(inflater.inflate(R.layout.dialogedittext, null));
            builder.setTitle("Pick a name");

            textView = (TextView)getActivity().findViewById(R.id.textView);
            img = (ImageView)getActivity().findViewById(R.id.img);
            editText =(EditText)getActivity().findViewById(R.id.edittext);

            builder.setPositiveButton("save", new DialogInterface.OnClickListener() {
                @Override 
                public void onClick(DialogInterface dialog, int which) {

                    textView.buildDrawingCache();
                    Log.d("d", "buildDrawingCache");
                    img.setImageBitmap(textView.getDrawingCache());
                    Log.d("d", "buildDrawingCache2");
                    drawable = (BitmapDrawable) img.getDrawable();
                    bitmap = drawable.getBitmap();
                    File sdCardDirectory = Environment.getExternalStorageDirectory();
                    File image = new File(sdCardDirectory, "edittext.png");
                    Log.d("d", "created image");

                    boolean success = false;

                    FileOutputStream outputStream;

                    try { 
                        outputStream = new FileOutputStream(image);
                        Log.d("d", "created image");
 //line : 79            bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream); 
                        outputStream.flush();
                        outputStream.close();
                    }catch (FileNotFoundException ex1) {
                        ex1.printStackTrace();
                    } 
                    catch (IOException e) {
                        e.printStackTrace();
                    } 

                } 
            }); 
            builder.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
                @Override 
                public void onClick(DialogInterface dialog, int which) {
                    DialogEditText.this.getDialog().cancel();
                } 
            }); 

        return builder.show();

    }