检索图像时,应用程序崩溃了

时间:2015-11-10 10:07:51

标签: java android sqlite cursor

我已使用下面的代码插入捕获图像并获取var canvas; var circles = []; var circleRadius = 50; var score = 0; var xaxis = getRandom(848) var yaxis = getRandom(600) function wackableCircle(){ xaxis = getRandom(848) yaxis = getRandom(600) if(circles.length > 10){ alert("You lose!\nScore: " + score) } setTimeout(function() {circles.push(new wackableCircle()}, 1000 - (score / 5)); } function getRandom(max){ return Math.floor((Math.random() * max)); } function initialize(){ canvas = document.getElementById("canvas"); canvas.addEventListener("mousedown", onMouseDown) //add new circle every 1000ms setTimeout(function () {circles.push(new wackableCircle())}, 1000); setInterval(function () {updateCanvas()}, 15); //Update canvas every 15ms. } //Find difference between two numbers. function numberDifference (a, b){ return Math.abs(a - b); } function onMouseDown(event){ for(var i = 0; i < circles.length; i++){ var wackableCircle = circles[i]; if(numberDifference(event.clientX, xaxis) < circleRadius){ //The circle's X and the mouse's X are within 50. if(numberDifference(event.clientY, yaxis) < circleRadius){ //The circle's Y and the mouse's Y are also with in 50. circles.splice(i, 1); } } } alert("click at " + event.clientX + ", " + event.clientY); } function updateCanvas(){ //clear canvas var canvasContext = canvas.getContext("2d"); canvasContext.clearRect(0, 0, 848, 600); for (var i = 0; i < circles.length; i++){ wackableCircle = circles[i]; //Draw circle at wackableCircle's x and y. canvasContext.beginPath(); canvasContext.arc(xaxis, yaxis, circleRadius, 0 ,Math.PI * 2); canvasContext.fill(); } } ,表示数据不为空。

"not null"

但是,当我尝试检索捕获的图像时,应用程序崩溃了。

        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;
    }
public static byte[] getBitmapAsByteArray(Bitmap bitmap)
{
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 0, outputStream);
    return outputStream.toByteArray();
}

MyDatabaseHelper.java

 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();
     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(k)},null);
                if(c.moveToFirst())
                        {
                       byte[] img=c.getBlob(c.getColumnIndexOrThrow("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();
            }
        }
  

引起:java.lang.IllegalStateException:无法从CursorWindow读取第0行,第0列。确保Cursor已初始化   在从它访问数据之前正确。               在android.database.CursorWindow.nativeGetBlob(本机方法)               在android.database.CursorWindow.getBlob(CursorWindow.java:404)               在android.database.AbstractWindowedCursor.getBlob(AbstractWindowedCursor.java:45)               在com.example.project.project.Receipt.onCreate(Receipt.java:40)

(适用Receipt.java:40)

 public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table " + TABLE_STAFF_BENEFIT + "( "  + ID3 + " INTEGER PRIMARY KEY , Claim_Type TEXT, Image BLOB, Description TEXT , Amount TEXT, Twd_id INTEGER, FOREIGN KEY(Twd_id) REFERENCES "+TABLE_INFO+"(_id))");
}

Retrieve large blob from Android sqlite database中陈述的大blob引起的错误是什么?我该如何解决?感谢

1 个答案:

答案 0 :(得分:0)

我认为你自己找到了解决方案。

你知道onClick-Handler中的图像/图像有多大吗? 如果它们大于1Mb,则无法在一个光标中保存信息。请参阅您自己的解决方案链接。

但也许你可以先测试它,如果可能的话。 尝试插入/选择尺寸小于1Mb的图像,然后插入尺寸大于1Mb的图像。

如果第一次测试成功而第二次测试没有成功,则需要按照帖子中的说明进行操作:Retrieve large blob from Android sqlite database