2012-03-13 48 views
0

最近我正在玩qr代碼生成,並經過谷歌和堆棧溢出成員的幫助後,我成功地能夠使用谷歌API生成它。生成的QR圖像消失一旦我退出應用程序

但是當我點擊模擬器中的「返回」按鈕,當我再次點擊應用程序時,qr代碼圖像消失?

如何讓它保持永久性,直到我再次點擊「生成」按鈕以獲得新的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> 

回答

2

您可以保存生成的圖像作爲Base64字符串在您的SharedPreferences

要對圖像進行編碼,請執行下列操作:Base64.encode(image, Base64.DEFAULT)

要存儲和檢索您的SharedPreferences值,您可以在活動中使用:

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); 
+0

朋友我與共享偏好一個小白,如果u可以在上面的代碼ü建議java代碼改變由以下張貼,這將是一個偉大的幫助對我來說 – 2012-03-13 12:05:49

+1

當然..我現在編輯 – SERPRO 2012-03-13 12:16:43

+0

在此先感謝朋友,我指望你:-) – 2012-03-13 12:46:24