这是我在尝试加载保存到我的数据库中的uri时遇到的错误:
E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: [B@d169ac: open failed: ENOENT (No such file or directory)
I/System.out: resolveUri failed on bad bitmap uri: [B@d169ac
这是我正在使用的代码:
public class CreateOrEditActivity extends ActionBarActivity implements View.OnClickListener {
private ExampleDBHelper dbHelper;
EditText jobEditText;
ImageView custSignature;
Cursor rs;
Button saveButton;
LinearLayout buttonLayout;
Button editButton, deleteButton;
File file;
String sigfile;
Dialog dialog;
LinearLayout mContent;
View view;
signature mSignature;
Bitmap bitmap;
Button btn_get_sign, mClear, mGetSign, mCancel;
String StoredPath;
String pic_name;
String DIRECTORY;
byte[] custsign;
int personID;
@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() + "/DigitSign/";
pic_name = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
StoredPath = DIRECTORY + pic_name + ".png";
btn_get_sign = (Button) findViewById(R.id.SignatureButton);
// Method to create Directory, if the Directory doesn't exists
file = new File(DIRECTORY);
if (!file.exists()) {
file.mkdir();
}
// Dialog Function
dialog = new Dialog(CreateOrEditActivity.this);
// Removing the features of Normal Dialogs
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.signature);
dialog.setCancelable(true);
btn_get_sign.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Function call for Digital Signature
dialog_action();
}
});
custSignature = (ImageView) findViewById(R.id.custimagesig);
jobEditText = (EditText) findViewById(R.id.editCustComment);
SimpleDateFormat calld = new SimpleDateFormat( "yyMMddHHmm" );
jobEditText.setText( calld.format( new Date() ));
saveButton = (Button) findViewById(R.id.saveButton);
saveButton.setOnClickListener(this);
buttonLayout = (LinearLayout) findViewById(R.id.buttonLayout);
editButton = (Button) findViewById(R.id.editButton);
editButton.setOnClickListener(this);
deleteButton = (Button) findViewById(R.id.deleteButton);
deleteButton.setOnClickListener(this);
dbHelper = new ExampleDBHelper(this);
if (personID > 0) {
saveButton.setVisibility(View.GONE);
buttonLayout.setVisibility(View.VISIBLE);
rs = dbHelper.getPerson(personID);
rs.moveToFirst();
custsign = rs.getBlob(rs.getColumnIndex(ExampleDBHelper.PERSON_SIGNATURE));
if(custsign.length>0){
Bitmap bitmap = BitmapFactory.decodeByteArray(custsign, 0, custsign.length);
custSignature.setImageBitmap(bitmap);
}else{
Toast.makeText(this,"Nothing Loaded ",Toast.LENGTH_LONG).show();
}
int personAge = rs.getInt(rs.getColumnIndex(ExampleDBHelper.PERSON_JOB_NUMBER));
if (!rs.isClosed()) {
rs.close();
}
custSignature.setImageURI(parse(String.valueOf(custsign)));
custSignature.setFocusable(false);
custSignature.setClickable(false);
jobEditText.setText((CharSequence) (personAge + ""));
jobEditText.setFocusable(false);
jobEditText.setClickable(false);
}
}
@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);
custSignature.setEnabled(false);
custSignature.setFocusableInTouchMode(false);
custSignature.setClickable(false);
jobEditText.setEnabled(true);
jobEditText.setFocusableInTouchMode(true);
jobEditText.setClickable(true);
return;
case R.id.deleteButton:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(R.string.deletePerson)
.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dbHelper.deletePerson(personID);
Toast.makeText(getApplicationContext(), "Deleted Successfully", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(), FragmentJob.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
})
.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
AlertDialog d = builder.create();
d.setTitle("Delete Form?");
d.show();
return;
}
}
public void persistPerson() {
if (personID > 0) {
if (dbHelper.updatePerson(personID,
nameCustomer.getText().toString(),
techComment.getText().toString(),
StoredPath.getBytes().toString(),
Integer.parseInt(jobEditText.getText().toString()))) {
Toast.makeText(getApplicationContext(), "Form Update Successful", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(), FragmentJob.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
} else {
Toast.makeText(getApplicationContext(), "Form Update Failed", Toast.LENGTH_SHORT).show();
}
} else {
if (dbHelper.insertPerson(nameCustomer.getText().toString(),
techComment.getText().toString(),
StoredPath.getBytes().toString(),
Integer.parseInt(jobEditText.getText().toString()))) {
Toast.makeText(getApplicationContext(), "Form Inserted", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Could not Insert Form", Toast.LENGTH_SHORT).show();
}
Intent intent = new Intent(getApplicationContext(), FragmentJob.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
}
private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
};
//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
);
}
}
// Function for Digital Signature
public void dialog_action() {
mContent = (LinearLayout) dialog.findViewById(R.id.linearLayout);
mSignature = new signature(getApplicationContext(), null);
mSignature.setBackgroundColor(Color.WHITE);
// Dynamically generating Layout through java code
mContent.addView(mSignature, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
mClear = (Button) dialog.findViewById(R.id.clear);
mGetSign = (Button) dialog.findViewById(R.id.getsign);
mGetSign.setEnabled(false);
mCancel = (Button) dialog.findViewById(R.id.cancel);
view = mContent;
mClear.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.v("tag", "Panel Cleared");
mSignature.clear();
mGetSign.setEnabled(false);
}
});
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();
}
});
mCancel.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.v("tag", "Panel Cancelled");
dialog.dismiss();
// Calling the same class
recreate();
}
});
dialog.show();
}
public class signature extends View {
private static final float STROKE_WIDTH = 5f;
private static final float HALF_STROKE_WIDTH = STROKE_WIDTH / 2;
private Paint paint = new Paint();
private Path path = new Path();
private float lastTouchX;
private float lastTouchY;
private final RectF dirtyRect = new RectF();
public signature(Context context, AttributeSet attrs) {
super(context, attrs);
paint.setAntiAlias(true);
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeWidth(STROKE_WIDTH);
}
public void save(View v, String StoredPath) {
Log.v("tag", "Width: " + v.getWidth());
Log.v("tag", "Height: " + v.getHeight());
if (bitmap == null) {
bitmap = Bitmap.createBitmap(mContent.getWidth(), mContent.getHeight(), Bitmap.Config.RGB_565);
}
Canvas canvas = new Canvas(bitmap);
try {
// Output the file
FileOutputStream mFileOutStream = new FileOutputStream(StoredPath);
v.draw(canvas);
// Convert the output file to Image such as .png
bitmap.compress(Bitmap.CompressFormat.PNG, 90, mFileOutStream);
mFileOutStream.flush();
mFileOutStream.close();
} catch (Exception e) {
Log.v("log_tag", e.toString());
}
}
public void clear() {
path.reset();
invalidate();
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawPath(path, paint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float eventX = event.getX();
float eventY = event.getY();
mGetSign.setEnabled(true);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
path.moveTo(eventX, eventY);
lastTouchX = eventX;
lastTouchY = eventY;
return true;
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_UP:
resetDirtyRect(eventX, eventY);
int historySize = event.getHistorySize();
for (int i = 0; i < historySize; i++) {
float historicalX = event.getHistoricalX(i);
float historicalY = event.getHistoricalY(i);
expandDirtyRect(historicalX, historicalY);
path.lineTo(historicalX, historicalY);
}
path.lineTo(eventX, eventY);
break;
default:
debug("Ignored touch event: " + event.toString());
return false;
}
invalidate((int) (dirtyRect.left - HALF_STROKE_WIDTH),
(int) (dirtyRect.top - HALF_STROKE_WIDTH),
(int) (dirtyRect.right + HALF_STROKE_WIDTH),
(int) (dirtyRect.bottom + HALF_STROKE_WIDTH));
lastTouchX = eventX;
lastTouchY = eventY;
return true;
}
private void debug(String string) {
Log.v("log_tag", string);
}
private void expandDirtyRect(float historicalX, float historicalY) {
if (historicalX < dirtyRect.left) {
dirtyRect.left = historicalX;
} else if (historicalX > dirtyRect.right) {
dirtyRect.right = historicalX;
}
if (historicalY < dirtyRect.top) {
dirtyRect.top = historicalY;
} else if (historicalY > dirtyRect.bottom) {
dirtyRect.bottom = historicalY;
}
}
private void resetDirtyRect(float eventX, float eventY) {
dirtyRect.left = Math.min(lastTouchX, eventX);
dirtyRect.right = Math.max(lastTouchX, eventX);
dirtyRect.top = Math.min(lastTouchY, eventY);
dirtyRect.bottom = Math.max(lastTouchY, eventY);
}
}
}
我的数据库类:
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
import java.util.HashMap;
public class ExampleDBHelper extends SQLiteOpenHelper {
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 PERSON_SIGNATURE = "signature";
public static final String PERSON_JOB_NUMBER = "jobnumber";
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, " +
PERSON_SIGNATURE + " BLOB, " +
PERSON_JOB_NUMBER + " 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 signature,
int jobnumber) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(PERSON_SIGNATURE, signature);
contentValues.put(PERSON_JOB_NUMBER, 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 signature,
int jobnumber) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(PERSON_SIGNATURE, signature);
contentValues.put(PERSON_JOB_NUMBER, jobnumber);
db.update(PERSON_TABLE_NAME, contentValues, PERSON_COLUMN_ID + " = ? ", new String[]{Integer.toString(id)});
return true;
}
public Integer deletePerson(Integer id) {
SQLiteDatabase db = this.getWritableDatabase();
return db.delete(PERSON_TABLE_NAME,
PERSON_COLUMN_ID + " = ? ",
new String[]{Integer.toString(id)});
}
public Cursor getPerson(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery("SELECT * FROM " + PERSON_TABLE_NAME + " WHERE " +
PERSON_COLUMN_ID + "=?", new String[]{Integer.toString(id)});
return res;
}
public Cursor getAllPersons() {
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery("SELECT * FROM " + PERSON_TABLE_NAME, null);
return res;
}
}
我不知道我在这里做错了什么。我很确定Uri正在保存到数据库中。这是当用户点击按钮进行签名然后点击保存以将文件存储在上面指定的目录中时。我检查了我的权限,一切都没问题。我认为我在读取uri以获取图像并在imageview中显示时会出错。
我在网上搜索并尝试了各种人的建议,但可以找到解决方案。
有人可以帮忙吗?
由于