0

我正在开发一个带有照片的联系人应用程序。我从 SQLite 存储和检索数据,它在我的列表视图上运行良好。

问题是当我单击一个列表项时,它应该显示图像视图和其他一些细节。但图像未显示在活动上

主要活动的列表视图

在此处的空白处应该是联系人的图像

用户数据库助手.java

package com.example.arif.contacts;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;

public class UserDBHelper extends SQLiteOpenHelper {

    String TAG = "DEBUG";

    private static final String DATABASE_NAME = "USERINFO.DB";
    private static final String TABLE_NAME = "ContactTable";

    private static final String TABLE_COL_NAME = "NAME";
    private static final String TABLE_COL_MOB = "MOB";
    private static final String TABLE_COL_EMAIL = "EMAIL";
    private static final String TABLE_COL_IMG = "EMAIL";
    private static final String TABLE_COL_ID = "ID";
    private static final int DATABASE_VERSION = 1;
    private static final String CREATE_QUERY =
            "CREATE TABLE "+TABLE_NAME+"("+TABLE_COL_ID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+TABLE_COL_NAME+" TEXT, "+TABLE_COL_MOB+" TEXT, "+TABLE_COL_EMAIL+" TEXT, "+TABLE_COL_IMG+" blob)" ;


    public UserDBHelper(Context context)
    {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        Log.e("DataBase", "Database created / Opened");
    }


    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_QUERY);
        Log.e("DataBase", "Table Created");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int i, int i1) {

    }

    public boolean AddInfo(String Name, String Mob , String Email, byte[] img )
    {

        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues CV = new ContentValues();
        CV.put("NAME", Name);
        CV.put("MOB", Mob);
        CV.put("EMAIL", Email );
        CV.put("NewImage", img);

        long result = db.insert(TABLE_NAME, null,CV);
        Log.e("DataBase", "Add Info Bug");
        if(result == -1)
        {
            return false;
        }
        else
        {
            return true;
        }

    }

    public Cursor getInformation()
    {
        Cursor data;
        SQLiteDatabase db = this.getWritableDatabase();
        String query = "Select * from " + TABLE_NAME;
        data = db.rawQuery(query,null);

        return data;

    }

    public String fetch_Name(int i)
    {
        String Str = "";

        SQLiteDatabase db = this.getReadableDatabase();
        String query = "Select "+TABLE_COL_NAME+" FROM " + TABLE_NAME + " WHERE " +TABLE_COL_ID+" = "+i;

        Cursor data = db.rawQuery(query,null);

        if(data.moveToFirst())
        {
            Str = data.getString(data.getColumnIndex(TABLE_COL_NAME+""));
        }


        return Str;
    }

    public String fetch_MOB(int i)
    {
        String Str = "";

        SQLiteDatabase db = this.getReadableDatabase();
        String query = "Select "+TABLE_COL_MOB+" FROM " + TABLE_NAME + " WHERE " +TABLE_COL_ID+" = "+i;

        Cursor data = db.rawQuery(query,null);

        if(data.moveToFirst())
        {
            Str = data.getString(data.getColumnIndex(TABLE_COL_MOB+""));
        }


        return Str;
    }

    public String fetch_Email(int i)
    {
        String Str = "";

        SQLiteDatabase db = this.getReadableDatabase();
        String query = "Select "+TABLE_COL_EMAIL+" FROM " + TABLE_NAME + " WHERE " +TABLE_COL_ID+" = "+i;

        Cursor data = db.rawQuery(query,null);

        if(data.moveToFirst())
        {
            Str = data.getString(data.getColumnIndex(TABLE_COL_EMAIL+""));
        }


        return Str;
    }


    public Bitmap fetch_Img(int i) {  /// THIS FUNCTION IS NOT WORKING



        byte[] ImgByte;
        SQLiteDatabase db = this.getWritableDatabase();

        String query = "Select "+TABLE_COL_IMG+" FROM " + TABLE_NAME + " WHERE " +TABLE_COL_ID+" = "+i;

        Cursor data = db.rawQuery(query,null);
        if(data.moveToFirst())
        {
            ImgByte =  data.getBlob(data.getColumnIndex(TABLE_COL_IMG+""));
            Bitmap bitMAP =  BitmapFactory.decodeByteArray(ImgByte, 0, ImgByte.length); // Here it's Always Returning Null
            if(bitMAP == null)
            {
                Log.e( TAG, "Bitmap is null :/ ");
            }
            return bitMAP;
        }
        return null;

    }

}

详细信息查看活动

package com.example.arif.contacts;

import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class DetailsOfContacts extends AppCompatActivity {

    int Pos;
    TextView Name_View, Mob_View, Email_View;
    ImageView Image_View;
    UserDBHelper userDBHelper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_details_of_contacts);

        //---- ---- Intend Bundle ---- ----
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        Pos = -1;
        if(bundle != null)
        {
            Pos = bundle.getInt("Position");
        }
        Toast.makeText(getApplicationContext(), "Position "+ Pos,Toast.LENGTH_SHORT).show();


        // ---- ---- Find View ---- ----
        Name_View = (TextView) findViewById(R.id.DetName);
        Mob_View = (TextView) findViewById(R.id.DetMob);
        Email_View = (TextView) findViewById(R.id.DetEmail);
        Image_View = (ImageView) findViewById(R.id.DetImg);

       /// --- --- DataBase -----
        UserDBHelper userDBHelper = new UserDBHelper(getApplicationContext());

        Name_View.setText(userDBHelper.fetch_Name(Pos+1)); // Working Fine
        Mob_View.setText(userDBHelper.fetch_MOB(Pos+1));// Working Fine
        Email_View.setText(userDBHelper.fetch_Email(Pos+1));// Working Fine
        Image_View.setImageBitmap(userDBHelper.fetch_Img(Pos+1)); // it's Not showing The image in activity

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.edit_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id ;
        id = item.getItemId();
        if(id == R.id.EditButt)
        {
            Toast.makeText(getApplicationContext(),"Edit Button Clicked", Toast.LENGTH_SHORT).show();
            Intent I = new Intent(DetailsOfContacts.this, EditActivity.class);
            startActivity(I);
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

09-18 09:35:56.866 26144-26144/com.example.arif.contacts E/DEBUG: Bitmap is null :/

4

1 回答 1

0

图像和电子邮件的列名相同

private static final String TABLE_COL_EMAIL = "EMAIL";
private static final String TABLE_COL_IMG = "EMAIL";

更改图像的列名。

于 2017-09-18T14:12:15.043 回答