lundi 9 novembre 2015

App crashed when captured image is retrieved

What's wrong with my code? I have a simple camera function in my app and it can use to choose image from gallery and take photo. Everything working fine just the captured image cannot be retrieved out.

Claims.java (insert image)

  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 (One of the navigation item )

  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)
        {
            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);

    }
}

        c.close();
    }
}

Image selected from gallery can be retrieved out to a in Receipt.java, but app crashed if the captured image trying to retrieved out and display on 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"));

(Receipt.java:32)

RetrieveImage(k);

WHY it works for choosing image from gallery, but not taking photo? Isn't because the image not successfully inserted ?

Does it has another way to check whether the captured images has stored? I using SQLite Debugger to see database in my real device. If the inserted image is get from gallery, I can see <BLOB> in image column. If is captured image, SQLite Debugger will crashed.

Aucun commentaire:

Enregistrer un commentaire