2017-06-15 52 views
0

我有一個應用程序,用戶在該應用程序中繪製圖片並返回到我的ImageView。圖像顯示並正常工作。然後我有一個保存按鈕來保存整個表單(使用圖像),但是當我點擊保存並重新打開應用程序時,文本會顯示,但不會顯示圖像。如何在ImageView中將圖像保存到數據庫並在活動開始時調用圖像

這裏是在數據庫中輸入文本代碼:

MainActivity.Class

EditText myName; 
ImageView myImage; 
EditText myJob; 
byte[] myProf; 
.... 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    personID = getIntent().getIntExtra(FragmentJob.KEY_EXTRA_CONTACT_ID, 0); 
    setContentView(R.layout.activity_edit); 
    verifyStoragePermissions(this); 

    DIRECTORY = Environment.getExternalStorageDirectory().getPath() + "/MYForm/"; 
    pic_name = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date()); 
    StoredPath = DIRECTORY + pic_name + ".png"; 

.... 

myName = (EditText) findViewById(R.id.editMyName); 
myImage = (ImageView) findViewById(R.id.myProfImage); 
myJob = (EditText) findViewById(R.id.myJobAgeWorked); 

.... 

dbHelper = new MyDBHelper(this); 

    if (personID > 0) { 
     saveButton.setVisibility(View.GONE); 
     buttonLayout.setVisibility(View.VISIBLE); 

     rs = dbHelper.getPerson(personID); 
     rs.moveToFirst(); 
     String myNameIs = rs.getString(rs.getColumnIndex(MyDBHelper.MY_NAME)); 
     myProf = rs.getBlob(rs.getColumnIndex(MyDBHelper.MY_PROFILE)); 

    ..... 

int myAge = rs.getInt(rs.getColumnIndex(MyDBHelper.MY_JOB)); 
     if (!rs.isClosed()) { 
      rs.close(); 
     } 

     myName.setText(cmyNameIs); 
     myName.setFocusable(false); 
     myName.setClickable(false); 

//I don't know if the below code works or not for "myImage" 

     myImage.setImageDrawable(Drawable.createFromPath(myProf + StoredPath)); 
     myImage.setFocusable(true); 
     myImage.setClickable(false); 

     myJob.setEnabled(true); 
     myJob.setFocusableInTouchMode(true); 
     myJob.setClickable(true); 

.... 

@Override 
public void onClick(View view) { 
    switch (view.getId()) { 
     case R.id.saveButton: 
      persistPerson(); 
      return; 
     case R.id.editButton: 
      saveButton.setVisibility(View.VISIBLE); 
      buttonLayout.setVisibility(View.GONE); 

      myName.setEnabled(true); 
      myName.setFocusableInTouchMode(true); 
      myName.setClickable(true); 

      myImage.setEnabled(false); 
      myImage.setFocusableInTouchMode(false); 
      myImage.setClickable(false); 

      myJob.setEnabled(true); 
      myJob.setFocusableInTouchMode(true); 
      myJob.setClickable(true); 

.... 

//This is where I think I am going wrong 

public void persistPerson() { 
    if (personID > 0) { 
     if (dbHelper.updatePerson(personID, 
       myName.getText().toString(), //WORKS 
       StoredPath.getBytes().toString(), //Not Sure that this is 
                correct 
       Integer.parseInt(myJob.getText().toString()))) { 

.... 

} else { 
     if (dbHelper.insertPerson(
       myName.getText().toString(), //WORKS 
       StoredPath.getBytes().toString(), //Not Sure that this is 
                correct 
       Integer.parseInt(myJob.getText().toString()))) { 

.... 

private static final int REQUEST_EXTERNAL_STORAGE = 1; 
private static String[] PERMISSIONS_STORAGE = { 
     Manifest.permission.READ_EXTERNAL_STORAGE, 
     Manifest.permission.WRITE_EXTERNAL_STORAGE 
}; 

***EDIT*** 
//persmission method. 
public static void verifyStoragePermissions(Activity activity) { 
    // Check if we have read or write permission 
    int writePermission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE); 
    int readPermission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.READ_EXTERNAL_STORAGE); 

    if (writePermission != PackageManager.PERMISSION_GRANTED || readPermission != PackageManager.PERMISSION_GRANTED) { 
     // We don't have permission so prompt the user 
     ActivityCompat.requestPermissions(
       activity, 
       PERMISSIONS_STORAGE, 
       REQUEST_EXTERNAL_STORAGE 
     ); 

    } 
} 

//THEN BELOW CODE IS FROM THE DRAWING PAD 

mGetSign.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 

      Log.v("tag", "Panel Saved"); 
      view.setDrawingCacheEnabled(true); 
      mSignature.save(view, StoredPath); 
      dialog.dismiss(); 
      Toast.makeText(getApplicationContext(), "Successfully Saved", Toast.LENGTH_SHORT).show(); 
      File f = new File(StoredPath); 
      ImageView mImgView1 = (ImageView)findViewById(R.id.editMyName); 
      Bitmap bmp = BitmapFactory.decodeFile(f.getAbsolutePath()); 
      mImgView1.setImageBitmap(bmp.createScaledBitmap(bmp, 620, 620, false)); 
     } 
    }); 

這將顯示輸入的文本和圖像,當用戶選擇獲得「繪圖」按鈕,然後畫了一個圖片。點擊「保存」按鈕後,此步驟將保存文本並且不會顯示圖像。

的錯誤是:

E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: [[email protected]/storage/emulated/0/MYForm/20170615_114018.png: open failed: ENOENT (No such file or directory) 

對於我MyDBHelper.class

public static final String DATABASE_NAME = "SQLiteExample.db"; 
private static final int DATABASE_VERSION = 2; 

public static final String PERSON_TABLE_NAME = "person"; 
public static final String PERSON_COLUMN_ID = "_id"; 
public static final String MY_NAME = "myfullname"; 
public static final String MY_PROFILE = "myprofileimage"; 

.... 

public ExampleDBHelper(Context context) { 
    super(context, DATABASE_NAME, null, DATABASE_VERSION); 
} 

@Override 
public void onCreate(SQLiteDatabase db) { 
    db.execSQL(
      "CREATE TABLE " + PERSON_TABLE_NAME + 
        "(" + PERSON_COLUMN_ID + " INTEGER PRIMARY KEY, " + 
        MY_NAME + " TEXT, " + 
        MY_PROFILE + " BLOB, " + 
        MY_JOB + " INTEGER)" 
    ); 
} 
@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    db.execSQL("DROP TABLE IF EXISTS " + PERSON_TABLE_NAME); 
    onCreate(db); 
} 

public boolean insertPerson(String mynameis, 
          String myprofimage, 
          int jobnumber) { 

    SQLiteDatabase db = this.getWritableDatabase(); 
    ContentValues contentValues = new ContentValues(); 

    contentValues.put(MY_NAME, mynameis); 
    contentValues.put(MY_PROFILE, myprofimage); 
    contentValues.put(MY_JOB, jobnumber); 

    db.insert(PERSON_TABLE_NAME, null, contentValues); 
    return true; 
} 

public int numberOfRows() { 
    SQLiteDatabase db = this.getReadableDatabase(); 
    int numRows = (int) DatabaseUtils.queryNumEntries(db, PERSON_TABLE_NAME); 
    return numRows; 
} 

public boolean updatePerson(Integer id, 
          String mynameis, 
          String smyprofimage, 
          int jobnumber) { 

    SQLiteDatabase db = this.getWritableDatabase(); 
    ContentValues contentValues = new ContentValues(); 
    contentValues.put(MY_NAME, customername); 
    contentValues.put(MY_PROFILE, signature); 
    contentValues.put(MY_JOB, jobnumber); 
    db.update(PERSON_TABLE_NAME, contentValues, PERSON_COLUMN_ID + " = ? ", new String[]{Integer.toString(id)}); 
    return true; 
} 

..... 

我希望我是有道理這裏:-)
一些能幫助我嗎?

回答

0

在Android Manifest中添加此權限。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
+0

我有我的清單文件,以及在我的MainActivity類別。我編輯了我的問題 – Allrounder

+0

我可以在手機上瀏覽到該位置,並且我可以看到文件 – Allrounder

0
you can save image as byte[] in database. take bitmap from your imageview and convert it to byte[] and then save in database. 

    ex : 

    yourimageView.buildDrawingCache(); 
    Bitmap bitmap = yourimageView.getDrawingCache(); 

    ByteArrayOutputStream stream=new ByteArrayOutputStream(); 
    byte[] imageData=stream.toByteArray(); 


now you can save imageData in database 
+0

謝謝,但我如何將它合併到我的代碼中?我剛剛嘗試,但不起作用。 – Allrounder

+0

點擊保存按鈕 –

相關問題