I have an app where the user draws a picture and returns to my ImageView in my app. The image is displayed and working. I then have a save button to save the entire form (with the Image), but when I click save and reopen the app the text is displayed but not the image.
Here is the code to enter the text in the database:
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));
}
});
This will show the text entered and image when the user selects get "drawing" button and drew a picture. After this step when hitting the "save" button the text is saved and the image not.
The error is:
E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: [B@cdfe4dd/storage/emulated/0/MYForm/20170615_114018.png: open failed: ENOENT (No such file or directory)
For my 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;
}
.....
I hope i made sense here :-)
Could some help me?
答案 0 :(得分:0)
Add this permission in Android Manifest.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
答案 1 :(得分: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