gpt4 book ai didi

java - 将图像保存到 sqlite 然后应用程序崩溃

转载 作者:行者123 更新时间:2023-12-01 12:03:10 24 4
gpt4 key购买 nike

我是 Android 编程的初学者。我有一个将图像作为字节数组保存到 sqlite 数据库的应用程序。在我的 android studio 中没有检测到错误,但是当我执行插入(通过按钮)时,应用程序崩溃了。我从很多人那里得到了这些方法。

图像来自相机并在 ImageView 中显示。

我使用 Utility 来处理图像,使用 DataHandler 来处理数据库。

知道我的代码有什么问题吗?请描述一下,因为我只是初学者:(

任何帮助都将是对我的巨大插入。

这是我的代码

MyActivity.java

public class MyActivity extends Activity implements OnClickListener {

Button kirim;
EditText address, zipcode, otherID, stories, yearBuilt, screener, totalArea, buildingName, use;
DataHandler handler;

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

bn = (Button) findViewById(R.id.bn);
IMG = (ImageView) findViewById(R.id.img);
bn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if(i.resolveActivity(getPackageManager())!=null){

startActivityForResult(i, REQUEST_CODE);
}
}
});

kirim = (Button) findViewById(R.id.kirim);
address = (EditText) findViewById(R.id.editAddress);
zipcode = (EditText) findViewById(R.id.editZipCode);
otherID = (EditText) findViewById(R.id.editOtherIdentifier);
stories = (EditText) findViewById(R.id.editStories);
yearBuilt = (EditText) findViewById(R.id.editYearBuilt);
screener = (EditText) findViewById(R.id.editScreenerName);
totalArea = (EditText) findViewById(R.id.editTotalFloorArea);
buildingName = (EditText) findViewById(R.id.editBuildingName);
use = (EditText) findViewById(R.id.editUsage);


kirim.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
String getAddress = address.getText().toString();
String getZipcode = zipcode.getText().toString();
String getOtherID = otherID.getText().toString();
String getStories = stories.getText().toString();
String getYear = yearBuilt.getText().toString();
String getScreener = screener.getText().toString();
String getTotalarea = totalArea.getText().toString();
String getBuildingname = buildingName.getText().toString();
String getUse = use.getText().toString();
byte[] getImage = Utility.getBytes(BitmapFactory.decodeResource(getResources(), R.id.img));
handler = new DataHandler(getBaseContext());
handler.open();
long id = handler.insertData(getAddress, getZipcode, getOtherID, getStories, getYear, getScreener, getTotalarea, getBuildingname, getUse, getImage);
Toast.makeText(getBaseContext(), "Data Inserted", Toast.LENGTH_LONG).show();

}
});

Utility.java

public class Utility {
// convert from bitmap to byte array
public static byte[] getBytes(Bitmap bitmap) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 0, stream);
return stream.toByteArray();
}

// convert from byte array to bitmap
public static Bitmap getPhoto(byte[] image) {
return BitmapFactory.decodeByteArray(image, 0, image.length);
}
}

DataHandler.java

public class DataHandler {
public static final String ADDRESS = "address";
public static final String ZIPCODE = "zipcode";
public static final String OTHERID = "otherID";
public static final String STORIES = "stories";
public static final String YEARBUILT = "yearBuilt";
public static final String SCREENER = "screener";
public static final String TOTALAREA = "totalArea";
public static final String BUILDINGNAME = "buildingName";
public static final String USAGE = "use";
public static final String IMAGE = "image";
public static final String TABLE_NAME = "mytable";
public static final String DATA_BASE_NAME = "mydb";
public static final int DATABASE_VERSION = 1;

DatabaseHelper dbhelper;
Context ctx;
SQLiteDatabase db;
public DataHandler(Context ctx){
this.ctx = ctx;
dbhelper = new DatabaseHelper(ctx);

}


private static class DatabaseHelper extends SQLiteOpenHelper{

public DatabaseHelper(Context ctx){
super(ctx,DATA_BASE_NAME, null, DATABASE_VERSION);

}


@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {

sqLiteDatabase.execSQL("CREATE TABLE " + TABLE_NAME + " (" + ADDRESS + " TEXT, " +
ZIPCODE + " TEXT NOT NULL, " +
OTHERID + " TEXT NOT NULL, " +
STORIES + " TEXT NOT NULL, " +
YEARBUILT + " TEXT, " +
IMAGE + " BLOB, " +
SCREENER + " TEXT, " +
TOTALAREA + " TEXT, " +
BUILDINGNAME + " TEXT, " +
USAGE + " TEXT, " +
IMAGE + " BLOB);"
);




}




@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i2) {

sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(sqLiteDatabase);

}
}
public DataHandler open(){
dbhelper = new DatabaseHelper(ctx);
db = dbhelper.getWritableDatabase();
return this;
}

public void close(){
dbhelper.close();
}

public long insertData(String address, String zipcode, String otherID, String stories, String yearBuilt, String screener, String totalArea, String buildingName, String use, byte[] image){

ContentValues content = new ContentValues();
content.put(ADDRESS, address);
content.put(ZIPCODE, zipcode);
content.put(OTHERID, otherID);
content.put(STORIES, stories);
content.put(YEARBUILT, yearBuilt);
content.put(SCREENER, screener);
content.put(TOTALAREA, totalArea);
content.put(BUILDINGNAME, buildingName);
content.put(USAGE, use);
content.put(IMAGE, image);
return db.insert(TABLE_NAME, null, content);
}}

这是 logcat 显示错误

8289-8289/com.example.brian.asprika E/OpenGLRenderer﹕ SFEffectCache:clear(), mSize = 0 01-09 14:03:33.733 8289-8289/com.example.brian.asprika W/IInputConnectionWrapper﹕ showStatusIcon on inactive InputConnection 01-09 14:03:43.193 8289-8289/com.example.brian.asprika I/HWUI﹕ EGLImpl-HWUI Protected EGL context created 01-09 14:03:46.593 8289-8289/com.example.brian.asprika D/AndroidRuntime﹕ Shutting down VM 01-09 14:03:46.593 8289-8289/com.example.brian.asprika W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x417a3c08) 01-09 14:03:46.603 8289-8289/com.example.brian.asprika E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.example.brian.asprika, PID: 8289 java.lang.NullPointerException at com.example.brian.asprika.Utility.getBytes(Utility.java:15) at com.example.brian.asprika.MyActivity$2.onClick(MyActivity.java:89) at android.view.View.performClick(View.java:4637) at android.view.View$PerformClick.run(View.java:19422) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5586) 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:1268) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084) at dalvik.system.NativeStart.main(Native Method)

最佳答案

byte[] getImage = Utility.getBytes(BitmapFactory.decodeResource(getResources(), R.id.img));

R.id.img 不能是图像可绘制标识符,并且 decodeResource() 此处返回 null。

对资源中的图像使用 R.drawable 标识符。

关于java - 将图像保存到 sqlite 然后应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27855374/

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