Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/190.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 一旦退出应用程序,生成的QR图像就会消失_Java_Android_Android Widget_Qr Code_Android Imageview - Fatal编程技术网

Java 一旦退出应用程序,生成的QR图像就会消失

Java 一旦退出应用程序,生成的QR图像就会消失,java,android,android-widget,qr-code,android-imageview,Java,Android,Android Widget,Qr Code,Android Imageview,最近,我正在玩二维码生成的游戏,在谷歌和stack overflow成员的帮助下,我成功地使用谷歌API生成了二维码 但是,当我在模拟器中单击“后退”按钮并再次单击应用程序时,二维码图像消失 如何使其保持永久性,直到我再次单击“生成”按钮以获得新的Qr图像 以下是我的问题答案提供商和那些在生成qr图像方面苦苦挣扎的人的代码 package com.test2; import java.io.IOException; import java.io.InputStream; import

最近,我正在玩二维码生成的游戏,在谷歌和stack overflow成员的帮助下,我成功地使用谷歌API生成了二维码

但是,当我在模拟器中单击“后退”按钮并再次单击应用程序时,二维码图像消失

如何使其保持永久性,直到我再次单击“生成”按钮以获得新的Qr图像

以下是我的问题答案提供商和那些在生成qr图像方面苦苦挣扎的人的代码

    package com.test2;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class Test2Activity extends Activity {


     ImageView QRCode;
     TextView MySite;
     EditText text,text1; 
     Button genarate;
     String textbox,textbox2;

       /** Called when the activity is first created. */
       @Override
       public void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.main);

           QRCode = (ImageView)findViewById(R.id.qrimage);
           MySite = (TextView)findViewById(R.id.mysite);

           genarate=(Button)findViewById(R.id.genarate);
           genarate.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                text=(EditText)findViewById(R.id.text);
                text1=(EditText)findViewById(R.id.text1);
                textbox=text.getText().toString();
                textbox2=text1.getText().toString();
                Bitmap bm = loadQRCode();
                    if(bm == null){
                    Toast.makeText(Test2Activity.this,
                      "Problem in loading QR Code1",
                      Toast.LENGTH_LONG).show();
                   }else{
                    QRCode.setImageBitmap(bm);
                   }
            }
                    Bitmap loadQRCode(){
                    Bitmap bmQR = null;
                    InputStream inputStream = null;

                    try {
                   inputStream = OpenHttpConnection("http://chart.apis.google.com/chart?chs=400x400&cht=qr&chl="+ textbox +"--->"+ textbox2);
                   bmQR = BitmapFactory.decodeStream(inputStream);
                   inputStream.close();
                  } catch (IOException e) {
                   // TODO Auto-generated catch block
                   e.printStackTrace();
                  }
                    return bmQR;
                   }

                   private InputStream OpenHttpConnection(String strURL) throws IOException{
                    InputStream is = null;
                    URL url = new URL(strURL);
                    URLConnection urlConnection = url.openConnection();

                    try{
                     HttpURLConnection httpConn = (HttpURLConnection)urlConnection;
                     httpConn.setRequestMethod("GET");
                     httpConn.connect();

                     if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
                      is = httpConn.getInputStream(); 
                     }
                     }catch (Exception ex){
                     }

                    return is; 
                 }  
        });
       }
    }
此活动的xml代码

    <?xml version="1.0" encoding="utf-8"?>
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/scrollView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   >
<TextView 
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="@string/hello"
   />

<EditText
    android:id="@+id/text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

<EditText
    android:id="@+id/text1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <requestFocus />
</EditText>

<Button
    android:id="@+id/genarate"
    android:layout_width="97dp"
    android:layout_height="wrap_content"
    android:text="genarate" />

<ImageView 
   android:id="@+id/qrimage"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:saveEnabled="true" android:visibility="visible"/>

<TextView 
   android:id="@+id/mysite"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   />
</LinearLayout>

    </ScrollView>

您可以将生成的图像另存为字符串

要对图像进行编码,请执行以下操作:
Base64.encode(image,Base64.DEFAULT)

要存储和检索可在活动中使用的SharedReference中的值,请执行以下操作:

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);   

//Save String
preferences.edit().putString("image64", imageData).commit();

//Get String
String imageBase64 = preferences.getString("image64", null);
                                                       ^----- Default value
if(imageBase64 == null)
    Log.d("LOG", "No image stored in the SharedPreferences");

//Create a bitmap from the base64 data
byte[] decodedString = Base64.decode(imageBase64, Base64.DEFAULT);
Bitmap bm = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);

friend是一个有着共同偏好的noob,如果你能用你建议的代码修改上面的java代码并将其发布到下面,这对mefriend将是一个很大的帮助。我用这种方式修改了代码,但在我退出并输入代码后,如果(bm==null){Toast.makeText(Test2Activity.this,“加载QR Code1的问题”,Toast.LENGTH_LONG.show();},代码就会消失else{QRCode.setImageBitmap(bm);String imageBase64=preferences.getString(“QRCode”,null);preferences.edit().putString(“QRCode”,imageBase64.commit();}你在
中检查的
bm
是什么?这是我们在共享首选项中保存的东西,所以我对代码有点混淆,我应该把这个项目工作区寄给你吗?这样你就可以纠正这个错误并给我发邮件了??