我的代码出了什么问题?我的应用程序中有一个简单的相机功能,它可用于choose image from gallery
和take photo
。只有捕获的图像才能正常工作。
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);
}
});
Receipt.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
上,则应用会崩溃。 / p>
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"));
(适用Receipt.java:32)
RetrieveImage(k);
为什么它适用于choosing image from gallery
,但不适用于taking photo
?是不是因为图像没有成功插入?
答案 0 :(得分:4)
你应该致电cursor.moveToFirst()
。首先移动光标位置并移除while loop
(我认为不需要)。
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);
}
}
答案 1 :(得分:0)
尝试更改您的代码
if(c!=null)
{
if (!c.isAfterLast())
while (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);
}
c.moveToNext();
}
c.close();