【发布时间】:2015-11-21 10:49:33
【问题描述】:
我正在尝试在离线模式下显示用户个人资料详细信息。姓名、图片、地址、邮箱等基本信息。
我已经阅读了blob,并将图像以字节数组的形式保存在blob中。
现在在检索时,我已将字节数组转换为位图,它始终为空。 :(
代码 sn-p:
点击按钮时从图库中选择图片:
photo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, RESULT_LOAD_IMG);
}
});
获取图像并将其转换为字符串和字节数组:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
try
{
if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK && null != data)
{
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imgPath = cursor.getString(columnIndex);
cursor.close();
String fileNameSegments[] = imgPath.split("/");
fileName = fileNameSegments[fileNameSegments.length - 1];
BitmapFactory.Options options = null;
options = new BitmapFactory.Options();
options.inSampleSize = 3;
bitmap = BitmapFactory.decodeFile(imgPath,options);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage), null, o);
final int REQUIRED_SIZE = 140;
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp / 2 < REQUIRED_SIZE
|| height_tmp / 2 < REQUIRED_SIZE) {
break;
}
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage), null, o2);
images.setImageBitmap(BitmapFactory.decodeFile(imgPath));
images.setVisibility(View.VISIBLE);
;
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte_arr = stream.toByteArray();
encodedString = Base64.encodeToString(byte_arr, Base64.DEFAULT);
}
else
{
Toast.makeText(this, "You haven't picked Image",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG).show();
}
}
现在将基本细节和编码字符串发送到服务器以保存在数据库中。 成功后,我会在 SQLite 中保存相同的详细信息。
@Override
protected void onPostExecute(Void aVoid)
{
dialog1.dismiss();
if(code==200)
{
Intent intent = new Intent(getApplicationContext(), SuccessfulLogin.class);
startActivity(intent);
finish();
DBhelper dbHelper = new DBhelper(Edit.this);
Employee employee_One = new Employee(byte_arr, name.getText().toString(), mobile.getText().toString(),address.getText().toString(),birth.getText().toString(),UserID);
// Employee employee_One = new Employee(byte_arr, name.getText().toString(), mobile.getText().toString(),address.getText().toString(),city.getText().toString(),birth.getText().toString(),UserID);
dbHelper.open();
dbHelper.insertEmpDetails(employee_One);
dbHelper.close();
dbHelper.open();
employee_One = dbHelper.retriveEmpDetails();
dbHelper.close();
/*
Log.e("Edit Profile Details", "Address --" + employee_One.getAddress() + "\n name " + employee_One.getName()
+ "\n Image " + Arrays.toString(employee_One.getBitmap()) + " \n mobile--" + employee_One.getPhoneNumber()
+ "\n DOB " + (employee_One.getBirth()) + " \n email--" + employee_One.getEmail());
Log.e("Profile Values", "Address --" + address.getText().toString() + "\n name " +
name.getText().toString() + "\n Image " + Arrays.toString(byte_arr) + " \n mobile--" +mobile.getText().toString());
*/
Log.e("byte_arr", "" + Arrays.toString(byte_arr));
Toast.makeText(getBaseContext(), "Details updated successfully..", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getBaseContext(), "Sorry, Try Again",Toast.LENGTH_LONG).show();
}
super.onPostExecute(aVoid);
}
我在这里使用相同的字节数组。
员工构造函数:
public Employee(byte[] b, String n, String k, String a, String bt, String e)
{
bmp = b;
Log.e("\nEmployee bmp", "" + Arrays.toString(this.bmp));
name = n;
_phone_number = k;
_address = a;
_birth = bt;
_email = e;
}
在离线模式下检索数据:
if(isNetworkAvailable())
{
new DisplayDetails().execute();
}
else
{
DBhelper dbHelper = new DBhelper(ViewProfile.this);
dbHelper.open();
employee_One = dbHelper.retriveEmpDetails();
dbHelper.close();
Log.e("Profile Details", "Address --" + employee_One.getAddress() + "\n name " +
employee_One.getName() + "\n Image " + Arrays.toString(employee_One.getBitmap()) + " \n mobile--" + employee_One.getPhoneNumber());
name.setText(employee_One.getName());
address.setText(employee_One.getAddress());
mobile.setText(employee_One.getPhoneNumber());
birth.setText(employee_One.getBirth());
email.setText(employee_One.getEmail());
byte[] outImage=employee_One.getBitmap();
ByteArrayInputStream imageStream = new ByteArrayInputStream(outImage);
Bitmap theImage = BitmapFactory.decodeStream(imageStream);
Image.setImageBitmap(theImage);
/*
ByteArrayInputStream imageStream = new ByteArrayInputStream(outImage);
Bitmap theImage = BitmapFactory.decodeStream(imageStream);
Image.setImageBitmap(theImage);
************
ByteArrayInputStream imageStream = new ByteArrayInputStream(outImage);
Bitmap theImage = BitmapFactory.decodeStream(imageStream);
Log.e("\nbitmap", "" + theImage +"\n outImage--" + Arrays.toString(outImage));
****************
ByteArrayInputStream arrayInputStream = new ByteArrayInputStream(outImage);
Bitmap bitmap = BitmapFactory.decodeStream(arrayInputStream);
******************
int intByteCount = outImage.length;
int[] intColors = new int[intByteCount / 3];
int intWidth = 2;
int intHeight = 2;
final int intAlpha = 255;
if ((intByteCount / 3) != (intWidth * intHeight)) {
throw new ArrayStoreException();
}
for (int intIndex = 0; intIndex < intByteCount - 2; intIndex = intIndex + 3) {
intColors[intIndex / 3] = (intAlpha << 24) | (outImage[intIndex] << 16) | (outImage[intIndex + 1] << 8) | outImage[intIndex + 2];
}
Bitmap bmpImage = Bitmap.createBitmap(intColors, intWidth, intHeight, Bitmap.Config.ARGB_8888);
*/
}
我已经尝试了所有注释的代码。但是失败
每次我在位图中得到 null 并且图像没有分配给ImageView。
同时我无法在我的表中添加超过 6 列。如果我尝试添加第 7 列,则会出现各种错误。
我不明白哪里出错了..
任何形式的帮助都将不胜感激。
谢谢。
日志
Image array: [91, 45, 49, 44, 32, 45, 52, 48, 44, 32, 45, 49, 44, 32, 45, 51, 50, 44, 32, 48, 44, 32, 49, 54, 44, 32, 55, 52, 44, 32, 55, 48, 44, 32, 55, 51, 44, 32, 55, 48, 44, 32, 48, 44, 32, 49, 44, 32, 49, 44, 32, 48, 44, 32, 48, 44, 32, 49, 44, 32, 48, 44, 32, 49, 44, 32, 48, 44, 32, 48, 44, 32, 45, 49, 44, 32, 45, 51, 55, 44, 32, 48, 44, 32, 54, 55, 44, 32, 48, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 45, 49, 44, 32, 45, 51, 55, 44, 32, 48, 44, 32, 54, 55, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 45, 49, 44, 32, 45, 54, 52, 44, 32, 48, 44, 32, 49, 55, 44, 32, 56, 44, 32, 48, 44, 32, 45, 49, 48, 54, 44, 32, 48, 44, 32, 45, 53, 54, 44, 32, 51, 44, 32, 49, 44, 32, 51, 52, 44, 32, 48, 44, 32, 50, 44, 32, 49, 55, 44, 32, 49, 44, 32, 51, 44, 32, 49, 55, 44, 32, 49, 44, 32, 45, 49, 44, 32, 45, 54, 48, 44, 32, 48, 44, 32, 51, 49, 44, 32, 48, 44, 32, 48, 44, 32, 49, 44, 32, 53, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 49, 44, 32, 48, 44, 32, 48, 44, 32, 48, 44, 32, 48, 44, 32, 48, 44, 32, 48, 44, 32, 48, 44, 32, 48, 44, 32, 49, 44, 32, 50, 44, 32, 51, 44, 32, 52, 44, 32, 53, 44, 32, 54, 44, 32, 55, 44, 32, 56, 44, 32, 57, 44, 32, 49, 48, 44, 32, 49, 49, 44, 32, 45, 49, 44, 32, 45, 54, 48, 44, 32, 48, 44, 32, 45, 55, 53, 44, 32, 49, 54, 44, 32, 48, 44, 32, 50, 44, 32, 49, 44, 32, 51, 44, 32, 51, 44, 32, 50, 44, 32, 52, 44, 32, 51, 44, 32, 53, 44, 32, 53, 44, 32, 52, 44, 32, 52, 44, 32, 48, 44, 32, 48, 44, 32, 49, 44, 32, 49, 50, 53, 44, 32, 49, 44, 32, 50, 44, 32, 51, 44, 32, 48, 44, 32, 52, 44, 32, 49, 55, 44, 32, 53, 44, 32, 49, 56, 44, 32, 51, 51, 44, 32, 52, 57, 44, 32, 54, 53, 44, 32, 54, 44, 32, 49, 57, 44, 32, 56, 49, 44, 32, 57, 55, 44, 32, 55, 44, 32, 51, 52, 44, 32, 49, 49, 51, 44, 32, 50, 48, 44, 32, 53, 48, 44, 32, 45, 49, 50, 55, 44, 32, 45, 49, 49, 49, 44, 32, 45, 57, 53, 44, 32, 56, 44, 32, 51, 53, 44, 32, 54, 54, 44, 32, 45, 55, 57, 44, 32, 45, 54, 51, 44, 32, 50, 49, 44, 32, 56, 50, 44, 32, 45, 52, 55, 44, 32, 45, 49, 54, 44, 32, 51, 54, 44, 32, 53, 49, 44, 32, 57, 56, 44, 32, 49, 49, 52, 44, 32, 45, 49, 50, 54, 44, 32, 57, 44, 32, 49, 48, 44, 32, 50, 50, 44, 32, 50, 51, 44, 32, 50, 52, 44, 32, 50, 53, 44, 32, 50, 54, 44, 32, 51, 55, 44, 32, 51, 56, 44, 32, 51, 57, 44, 32, 52, 48, 44, 32, 52, 49, 44, 32, 52, 50, 44, 32, 53, 50, 44, 32, 53, 51, 44, 32, 53, 52, 44, 32, 53, 53, 44, 32, 53, 54, 44, 32, 53, 55, 44, 32, 53, 56, 44, 32, 54, 55, 44, 32, 54, 56, 44, 32, 54, 57, 44, 32, 55, 48, 44, 32, 55, 49, 44, 32, 55, 50, 44, 32, 55, 51, 44, 32, 55, 52, 44, 32, 56, 51, 44
E/bitmap﹕ null
【问题讨论】:
-
这是因为在Db中保存任何类型的数据都有1MB的限制..而不是image..您可以将image保存在sdcard中..然后将image的路径保存在db中。