I want to retrieve a list of images from SQLite.
This is my 'getImage()' method
public List<byte[]> getImage(int i) {
List<byte[]> list = new ArrayList<>();
String selectImage = "SELECT VocabImage FROM Vocab WHERE VocabTopic =" + i;
Cursor c = database.rawQuery(selectImage, null);
if(c.moveToFirst())
do{
list.add(c.getBlob(c.getColumnIndex("VocabImage")));
}while(c.moveToNext());
c.close();
return list;
}
This is my java class where I want to call my getImage() method. But there is an error saying that it cannot resolve symbol 'length' in (data.length) in the BitmapFactory line. Anyone has any idea how to solve this? Thank you.
Intent intent = getIntent();
int topicId = intent.getIntExtra("SelectedTopicId", 1);
databaseAccess.open();
List<byte[]>data = databaseAccess.getImage(topicId);
Bitmap image = BitmapFactory.decodeByteArray(data, 0, data.length);
imageView.setImageBitmap(image);
Bitmap image = BitmapFactory.decodeByteArray(data, 0, data.length);
change to
Bitmap image = BitmapFactory.decodeByteArray(data.get(position), 0, data.get(position).length); // pass position for which you want length.
you need to change like this: list.get(0).length for get image at index 0.
Related
I have image in my ListView. They are loaded as follows:
String iMages[] = {
"http://www.thebiblescholar.com/android_awesome.jpg",
"http://blogs-images.forbes.com/rogerkay/files/2011/07/Android1.jpg",
"http://cdn.slashgear.com/wp-content/uploads/2012/10/android-market-leader-smartphone.jpg",
"http://www.planmyworkshop.com/images/android.jpeg",
"http://www.androidguys.com/wp-content/uploads/2012/07/01-android2.jpg"
};
ArrayList<Bitmap> bitmap_array = new ArrayList<Bitmap>();
for (int i = 0; i < iMages.length; i++) {
Log.d("i-->" + i, "Url-->" + iMages[i]);
Bitmap bit = getBitmapFromURL(iMages[i]);
bitmap_array.add(bit);
}
How load them from res/drawable ? I tried different ways, but all the way past ...
Try something like this to decode a bitmap from your drawable folder:
Bitmap bitmap= BitmapFactory.decodeResource(context.getResources(),
R.drawable.ic_launcher);
I assume the images in your ListView are of type ImageView or subclasses (ImageButton, ZoomButton etc.).
If that is the case, just set the res image as background:
myImageView.setBackgroundResource(R.drawable.my_image);
Remember to do it only from UI thread.
You can also do like this
String imageFileName = "launcher"; // this is image file name
String PACKAGE_NAME = getApplicationContext().getPackageName();
int imgId = getResources().getIdentifier(PACKAGE_NAME+":drawable/"+imageFileName , null, null);
image_view.setImageBitmap(BitmapFactory.decodeResource(getResources(),imgId));
I am trying to take a screen shot of all the content of a scrollview(off page as well). The problem I am having is that when it get's to the line Bitmap b = u.getDrawingCache(); an exception is thrown "nullPointerException". I can't seems to figure out what is causing this. This is the code I have:
if(v.getId() == R.id.btnSnap)
{
try
{
View u = findViewById(R.id.svContent);
u.setDrawingCacheEnabled(true);
ScrollView z = (ScrollView) findViewById(R.id.svContent);
int totalHeight = z.getChildAt(0).getHeight();
int totalWidth = z.getChildAt(0).getWidth();
u.layout(0, 0, totalWidth, totalHeight);
u.buildDrawingCache(true);
Bitmap b = u.getDrawingCache();
//Just so that I can get a quick preview of
//what the picture looks like
ImageView img = (ImageView)findViewById(R.id.ivTest);
img.setImageBitmap(b);
//Save the picture
File fileName = new File(context.getExternalFilesDir(null)+"/ArcFlash/SafetyCheckList1.png");
FileOutputStream out = new FileOutputStream(fileName);
b.compress(Bitmap.CompressFormat.PNG, 90, out);
out.close();
}
catch(Exception ex)
{
handler.Log(ex.getMessage(), "onClick(SafetyCheckList.java)", context);
}
}
EDIT The problem seems to be at u.layout(0, 0, totalWidth, totalHeight);
I'm having problem converting byte array to bitmap. Now what I'm trying to achieve is I'm getting image as a byte array and trying to convert into bitmap so that I can display the image. but after running my below code in my bitmap output i'm getting Null value.
String t= "byte array of the image";
byte[] temp = t.getBytes() ;
Bitmap bmp = BitmapFactory.decodeByteArray(temp, 0, temp.length);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
System.out.println("bitmap output"+bmp);
I have googled a lot and found this code works for every1. can please someone tell me where I'm doing wrong.
Thanks in advance
In my case this is working
String result = "here imge;
if (result != "") {
byte[] bloc = Base64.decode(result);
BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize = 8;
Bitmap b = BitmapFactory.decodeByteArray(bloc, 0, bloc.length);
Try this way
String t= "byte array of the image";
byte[] temp = Base64.decode(t, Base64.NO_WRAP); //UPDATE HERE
Bitmap bmp = BitmapFactory.decodeByteArray(temp, 0, temp.length);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
System.out.println("bitmap output"+bmp);
I have a camera that I use to take pictures and my problem is on the onPictureTaken function:
public Camera.PictureCallback jpeghandler = new Camera.PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
Options options = new BitmapFactory.Options();
options.inScaled = false;
options.inDither = false;
options.inSampleSize = 2;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
photo = BitmapFactory.decodeByteArray(data, 0, data.length, options);
Matrix matrix = new Matrix();
matrix.postRotate(90);
Bitmap rotate_bitmap = Bitmap.createBitmap(photo , 0, 0, photo.getWidth(), photo.getHeight(), matrix, true);
photo.recycle();
photo = Bitmap.createBitmap(rotate_bitmap, 0, rotate_bitmap.getHeight()/2 - rotate_bitmap.getWidth()/2, rotate_bitmap.getWidth(), rotate_bitmap.getWidth());
Intent resultIntent = new Intent();
String image_url = MediaStore.Images.Media.insertImage(getContentResolver(), photo, "photo" , null);
resultIntent.putExtra("Photo_Taken", image_url);
setResult(Activity.RESULT_OK, resultIntent);
photo.recycle();
rotate_bitmap.recycle();
finish();
}
};
I have another activity that opens this one and then I take a picture, closing this one and passing the location of the image back to the first activity. The problem is that when I reopen this activity to take another picture it gives me an Out Of Memory error on this line:
Bitmap rotate_bitmap = Bitmap.createBitmap(photo , 0, 0, photo.getWidth(), photo.getHeight(), matrix, true);
What is wrong and what is happening? Cause the photo size is always under 25Mb and then in the second photo it appears this: Out of memory on a 13128976-byte allocation.
Thank you!
Your heap gets full [ (image data) + (other data) > (heap size) ]. I believe you have two instances of your photo stored on heap (when second activity is launched, data (including photo) from first activity is not yet cleared from memory).
If you're using Eclipse I strongly recommend installing Memory Analyzer (MAT)
Check my question for more info.
I have to convert a byte to Bitmap and i set it to imageview
i do have methode that convert Bitmap in ImageView into Byte and i insert it later,
public static byte[] ConvertDrawableToByteArray(Drawable drawableResource) {
Bitmap imageBitmap = ((BitmapDrawable) drawableResource).getBitmap();
ByteArrayOutputStream imageByteStream = new ByteArrayOutputStream();
imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, imageByteStream);
byte[] imageByteData = imageByteStream.toByteArray();
return imageByteData;
}
and when i want to retrieve image from database and show it in an ImageView i do
//--
byte[] image_b = c.getBlob(4);
Bitmap b = BitmapFactory.decodeByteArray(image_b, 0, image_b.length);
img.setImageBitmap(b);
but it return nothing
what is wrong, please help
Thanks a lot
I guess it's because the cursor didn't return anything. Maybe the Blob is too big to be queried in one time because android cursor limit is 1 mb per query please check again your stacktrace/logcat and if the blob size is the problem you can try to split the byte to smaller size and store it in your database
You can play around with the following and let us know if it works:
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = resolver.query(imageUri, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
return BitmapFactory.decodeFile(picturePath);
I dont understand where is the problèlme i do likee this :String t = image_b.toString(); System.out.println("what it give me" +t); and i find in the logcat this line : 03-19 17:01:28.167: I/System.out(1019): what it give me[B#41f1f5c0
is that mean it return something but dont showin or is the byte [B#41f1f5c0 is not correct