i have an image uri using this uri i insert image in the sqlite DataBase but the problem is how can i retrieve image .
this is code:
if(data!=null){
Uri uri = data.getData();
try {
InputStream inputStream = getContentResolver().openInputStream(uri);
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
Bitmap bmp = BitmapFactory.decodeStream(bufferedInputStream);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, bos);
byte[] bArray = bos.toByteArray();
DataBase dataBase=new DataBase(getBaseContext());
dataBase.insertImage(bArray);
Toast.makeText(getBaseContext(),"single item ",Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
i want to retrive image??
android docs :
abstract byte[] getBlob(int columnIndex) Returns the value of the
requested column as a byte array.
Example:
Cursor resultSet = mydatbase.rawQuery("Select * from TutorialsPoint",null);
resultSet.moveToFirst();
byte[] raw_image = resultSet.getBlob(1)
Related
I am converting Video Uri to bytearray but I am getting FileNotFoundException
I have written this code for converting Video Uri to bytearray
video = data.getData();
byte[] buf = new byte[1024];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
FileInputStream fis;
try {
fis = new FileInputStream(new File(video.getPath()));
int n;
while (-1 != (n = fis.read(buf)))
baos.write(buf, 0, n);
fis.close();
baos.close();
} catch (Exception e) {
e.printStackTrace();
}
bbytes= baos.toByteArray();
Try open stream with ContentResolver:
fis = context.getContentResolver().openInputStream(video);
I save the image uri by using below code
public void insert (Uri image) throws SQLiteException {
SQLiteDatabase database = mdb.getWritableDatabase();
ContentValues cv = new ContentValues();
try {
InputStream iStream = getContentResolver().openInputStream(image);
byte[] inputData = com.example.tony.monthlyexpenses.Utils.getBytes(iStream);
cv.put(MyDatabaseHelper.KEY_IMAGE,inputData);
}catch(IOException ioe)
{
Log.e("A", "<saveImageInDB> Error : " + ioe.getLocalizedMessage());
}
database.insert(MyDatabaseHelper.TABLE_EXPENSES, null, cv);
database.close();
}
Utils
public static Bitmap getImage(byte[] image) {
return BitmapFactory.decodeByteArray(image, 0, image.length);
}
public static byte[] getBytes(InputStream inputStream) throws IOException {
ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int len = 0;
while ((len = inputStream.read(buffer)) != -1) {
byteBuffer.write(buffer, 0, len);
}
return byteBuffer.toByteArray();
}
Now I want to retrieve the image and display as setImage Uri instead of setImage 'Bitmap'.
byte[] bb = cursor.getBlob(cursor.getColumnIndex("ImageReceipt"));
imageView.setImageBitmap(Utils.getImage(bb));
It depends on the contents of the array of bytes.
Let assume that your array of bytes are URI characters, you can use the following code:
byte [] buf = <your byte array>;
String s = new String(buf, "UTF-8");
Uri uri = Uri.parse(s);
But In your case it's not possible to convert image byte array to image Uri, U can save your image Uri instead of storing bitmap. so that u can read it as an image Uri and reduce memory space too.
I was looking at the Parse Android docs and saw that to save photos and videos, you have to initialize a new ParseFile with a name and a byte[] of data and save it.
What's the easiest way to convert an image Uri and video Uri to a byte array?
Here are my attempted solutions:
mPhoto = new ParseFile("img", convertImageToBytes(Uri.parse(mPhotoUri)));
mVideo = new ParseFile ("vid", convertVideoToBytes(Uri.parse(mVideoUri)));
private byte[] convertImageToBytes(Uri uri){
byte[] data = null;
try {
ContentResolver cr = getBaseContext().getContentResolver();
InputStream inputStream = cr.openInputStream(uri);
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
data = baos.toByteArray();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return data;
}
private byte[] convertVideoToBytes(Uri uri){
byte[] videoBytes = null;
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
FileInputStream fis = new FileInputStream(new File(getRealPathFromURI(this, uri)));
byte[] buf = new byte[1024];
int n;
while (-1 != (n = fis.read(buf)))
baos.write(buf, 0, n);
videoBytes = baos.toByteArray();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return videoBytes;
}
private String getRealPathFromURI(Context context, Uri contentUri) {
Cursor cursor = null;
try {
String[] proj = { MediaStore.Video.Media.DATA };
cursor = context.getContentResolver().query(contentUri, proj, null,
null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} finally {
if (cursor != null) {
cursor.close();
}
}
}
The convertImageToBytes and convertVideoToBytes methods work for now, but I'm just wondering If I'm doing doing this correctly.
From Uri to get byte[] I do the following things,
ByteArrayOutputStream baos = new ByteArrayOutputStream();
FileInputStream fis = new FileInputStream(new File(yourUri));
byte[] buf = new byte[1024];
int n;
while (-1 != (n = fis.read(buf)))
baos.write(buf, 0, n);
byte[] videoBytes = baos.toByteArray(); //this is the video in bytes.
I was trying to add the favicon of a webpage to a sqlite database, but I am getting a java.lang.NullPointerException when trying to save the favicon to a ByteArrayOutputStream via Bitmap.compress.
There is my code fragment:
Bitmap bitmap = view.getFavicon();
mySQLiteAdapter.openToWrite();
ByteArrayOutputStream byteArrayBitmapStream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0, byteArrayBitmapStream);
byte[] bArray = byteArrayBitmapStream.toByteArray();
String[] comments = new String[] {view.getUrl()};
String[] commentss = new String[] {view.getTitle()};
int nextInt = new Random().nextInt(1);
try{
mySQLiteAdapter.insert(bArray, commentss[nextInt], comments[nextInt]);
}catch (Exception e) {
e.printStackTrace();
}
mySQLiteAdapter.close();
Why am I getting this error?
I want to save images from a particular URL into the sqlite db and
then display it from the database..Can anyone tell me as how i could
do that.
Thanks in advance.
Regards,
Raghav Rajagopalan
try this code.
URL url = new URL("your image url");
URLConnection ucon = url.openConnection();
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is,128);
ByteArrayBuffer barb= new ByteArrayBuffer(128);
//read the bytes one by one and append it into the ByteArrayBuffer barb
int current = 0;
while ((current = bis.read()) != -1) {
barb.append((byte) current);
}
ContentValues filedata= new ContentValues();
//TABLE_FIELD = a field in the database table of type text
filedata.put(TABLE_FIELD,barb.toByteArray());
//TABLE_NAME = a table in the database with a field TABLE_FIELD
db.insert(TABLE_NAME, null, filedata);
//Retriving Data from DB:
//select the data
Cursor cursor = db.query(TABLE_STATIONLIST, new String[] {TABLE_FIELD},null, null, null, null, null);
//get it as a ByteArray
//reading as Binary large object(BLOB)
byte[] imageByteArray=cursor.getBlob(1);
//the cursor is not needed anymore so release it
cursor.close();
Bitmap image=getbyteAsBitmap(imageByteArray);
//use this method to convert byte[] to bitmap
private Bitmap getbyteAsBitmap(byte[] bytedata){
if(bytedata!=null){
ByteArrayInputStream imageStream = new ByteArrayInputStream(bytedata);
Bitmap theImage = BitmapFactory.decodeStream(imageStream);
//theImage=MediaProcesser.getRoundedCornerBitmap(theImage, 5);
return theImage;
}else{
return null;
}
}