gpt4 book ai didi

java - 捕获图像时应用程序崩溃

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

我的代码有什么问题吗?我的应用程序中有一个简单的相机功能,它可以用来从图库中选择图像拍照。一切正常,只是无法检索捕获的图像

Claims.java(插入图片)

  button.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {

if ((name != null && name.trim().length() > 0) && (result != null && result.trim().length() > 0)) {
// Toast.makeText(getActivity().getApplicationContext(), fk+"", Toast.LENGTH_LONG).show();
byte[] data=getBitmapAsByteArray(Global.img); // this is a function
if(data==null)
{
Toast.makeText(getActivity(), "null", Toast.LENGTH_LONG).show();
}
else
{


Toast.makeText(getActivity(), " not null", Toast.LENGTH_LONG).show();
SB.insertStaffBenefit(name, data, description, result, fk);
}

}

});
return claims;
}

ViewView.java(导航项之一)

  listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> listView, View view,
int position, long id) {
// Get the cursor, positioned to the corresponding row in the result set
Cursor cursor = (Cursor) listView.getItemAtPosition(position);

// Get the state's capital from this row in the database.
String ID =
cursor.getString(cursor.getColumnIndexOrThrow("_id"));

Intent intent = new Intent(ViewView.this.getActivity(), Receipt.class);
intent.putExtra("ID", ID);
startActivity(intent);
}
});

收据.java

public class Receipt extends AppCompatActivity {

private SQLiteDatabase database;
private MyDatabaseHelper dbHelper;
private Cursor c;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.receipt);
dbHelper = new MyDatabaseHelper(this);
final String k = getIntent().getExtras().getString("ID");
Toast.makeText(getApplicationContext(), k+"", Toast.LENGTH_LONG).show();
RetrieveImage(k);
}

public void RetrieveImage(String b)
{
ImageView a=(ImageView)findViewById(R.id.imageView5);
database = dbHelper.getWritableDatabase();
c = database.rawQuery("SELECT s.Image FROM Information i LEFT JOIN StaffBenefit s ON s.Twd_id=i._id WHERE i._id=? ", new String[]{String.valueOf(b)},null);
if(c!=null)
{
while (c.moveToNext()) {
byte[] img=c.getBlob(c.getColumnIndex("Image"));
if(img!=null)
{
Log.e("TAG", " Not null");
}
else
{
Log.e("TAG", " null");
}
ByteArrayInputStream imageStream = new ByteArrayInputStream(img);
Bitmap theImage= BitmapFactory.decodeStream(imageStream);
a.setImageBitmap(theImage);

}
}
c.close();
}
}

从图库中选择的图像可以检索到 Receipt.java 中的 a,但如果捕获的图像尝试检索并显示在 a 上,应用程序会崩溃.

Process: com.example.project.project, PID: 26303
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.project.project/com.example.project.project.Receipt}:
java.lang.IllegalStateException: Couldn't read row 0, col 0 from
CursorWindow. Make sure the Cursor is initialized correctly before
accessing data from it.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2413)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
at android.app.ActivityThread.access$900(ActivityThread.java:175)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5602)
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:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: Couldn't read row 0, col 0 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
at android.database.CursorWindow.nativeGetBlob(Native Method)
at android.database.CursorWindow.getBlob(CursorWindow.java:404)
at android.database.AbstractWindowedCursor.getBlob(AbstractWindowedCursor.java:45)
at com.example.project.project.Receipt.RetrieveImage(Receipt.java:43)
at com.example.project.project.Receipt.onCreate(Receipt.java:32)

(Receipt.java:43)

  byte[] img=c.getBlob(c.getColumnIndex("Image"));

(收据.java:32)

RetrieveImage(k);

为什么它适用于从图库中选择图像,但不适用于拍照?是不是因为图片没有插入成功?

最佳答案

您应该调用cursor.moveToFirst()。首先移动光标位置并删除 while 循环(我认为不需要)。

if(c.moveToFirst())
{
byte[] img=c.getBlob(c.getColumnIndex("Image"));
if(img!=null)
{
Log.e("TAG", " Not null");
}
else
{
Log.e("TAG", " null");
}
ByteArrayInputStream imageStream = new ByteArrayInputStream(img);
Bitmap theImage= BitmapFactory.decodeStream(imageStream);
a.setImageBitmap(theImage);

}
}

关于java - 捕获图像时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33607037/

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