gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 07:55:41 24 4
gpt4 key购买 nike

最近正在研究二维码生成,经过大量谷歌搜索和 Stack Overflow 成员的帮助,我使用谷歌 API 成功生成了它。

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

如何使其保持永久状态,直到我再次单击“生成”按钮以获得新的 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;
}
});
}
}

此 Activity 的 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 SharedPreferences 中的字符串.

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

要存储和检索可在 Activity 中使用的 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);

关于java - 一旦我退出应用程序,生成的 QR 图像就会消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9683518/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com