How to display bitmap from internal storage? - android

I saved my bitmap images in my internal storage but i can't redisplay it. I've been researching for a long time but i've not find yet.
public static void saveImages(Activity activity) throws IOException
{
for (int i=0; i<categories.getItems().length; i++) {
OutputStream os2 = activity.openFileOutput(categories.getItems()[i].getName(),
Context.MODE_WORLD_READABLE);
OutputStreamWriter osw2 = new OutputStreamWriter(os2);
Bitmap bmp = ((BitmapDrawable)categories.getItems()[i].getCategoryImage()).getBitmap();
bmp.compress(Bitmap.CompressFormat.PNG, 90, os2);
osw2.close();
}
}
This code works succesfully to save images. I will redisplay that images from files.
Thank you

Try this code: uses openFileInput to fetch the streams you saved and then decodes them:
for (int i=0; i<categories.getItems().length; i++) {
InputStream is = activity.openFileInput(categories.getItems()[i].getName());
Bitmap b = BitmapFactory.decodeStream(is);
// do whatever you need with b
}

Try this
File f=new File(yourdir, imagename);
Bitmap b = BitmapFactory.decodeStream(new FileInputStream(f));

decode bitmap, and then make a new imageView then add the bitmap to the imageView.

Related

bitmap.compress from Uri resulting in OutOfMemoryError

I am trying to save a bitmap which user selects into my own App path.
Unfortunately, with very big images I get OutOfMemoryError error.
I am using the following code:
private String loadImage (Uri filePath) {
File fOut = new File(getFilesDir(),"own.jpg");
inStream = getContentResolver().openInputStream(filePath);
selectedImage = BitmapFactory.decodeStream(inStream);
selectedImage.compress(CompressFormat.JPEG, 100, new FileOutputStream(fOut));
}
Is there any way for me to save any image file of any size for an Uri to a file?
*I am not in a position to resize the image e.g. by using calculateInSampleSize method.
Is there any way for me to save any image file of any size for an Uri to a file?
Since it already is an image, just copy the bytes from the InputStream to the OutputStream:
private void copyInputStreamToFile( InputStream in, File file ) {
try {
FileOutputStream out = new FileOutputStream(file);
byte[] buf = new byte[8192];
int len;
while((len=in.read(buf))>0){
out.write(buf,0,len);
}
out.flush();
out.getFD().sync();
out.close();
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
(adapted from this SO answer)

VuDroid pdf viewer cannot find Bitmap to render or it doesnt render

I am trying to use the VuDroid PDF viewer and I need to take the rendered bitmap and store it as a byte[]. Then I need to convert it back into a Bitmap that can be displayed on a view using something like "canvas.drawBitmap(bitmap, 0, 0, paint);".
I have spent many hours trying to access the Bitmap and I might have done it already, but even if I get the byte[] to return something it still wont render as a Bitmap on the canvas.
Could someone please help me here, I must be missing something. Thank you so much.
I believe it is supposed to accessed via...
PDFPage.java .... public Bitmap renderBitmap(int width, int height, RectF pageSliceBounds)
-or-
through Page.java -or- DocumentView.java -or- DecodeService.java
Like I said I have tried all of these and have gotten results I just cannot see where I am going wrong since I cannot render it to see if the Bitmap was called correctly.
Thank you again :)
The doc says the method returns "null if the image could not be decode." You can try:
byte[] image = services.getImageBuffer(1024, 600);
InputStream is = new ByteArrayInputStream(image);
Bitmap bmp = BitmapFactory.decodeStream(is);
I think This will help you:-
Render a byte[] as Bitmap in Android
How does Bitmap.Save(Stream, ImageFormat) format the data?
Copy image with alpha channel to clipboard with custom background color?
if you want to get each pdf page as independent bitmap you should consider that
VuDroid render the pages,
PDFView only display them.
you should use VuDroid functions.
now you can use this example and create your own codes
Example code : for make bitmap from a specific PDF page
view = (ImageView)findViewById(R.id.imageView1);
pdf_conext = new PdfContext();
PdfDocument d = pdf_conext.openDocument(Environment.getExternalStorageDirectory() + "your PDF path");
PdfPage vuPage = d.getPage(1); // choose your page number
RectF rf = new RectF();
rf.bottom = rf.right = (float)1.0;
Bitmap bitmap = vuPage.renderBitmap(60, 60, rf); //define width and height of bitmap
view.setImageBitmap(bitmap);
for writing this bitmap on SDCARD :
try {
File mediaImage = new File(Environment.getExternalStorageDirectory().toString() + "your path for save thumbnail images ");
FileOutputStream out = new FileOutputStream(mediaImage);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
for retrieve saved image:
File file = new File(Environment.getExternalStorageDirectory().toString()+ "your path for save thumbnail images ");
String path = file.getAbsolutePath();
if (path != null){
view = Bitmap.createScaledBitmap(BitmapFactory.decodeFile(path), YOUR_X, YOUR_Y, false);
}
Try this code to check whether bitmap is properly generating or not
PdfContext pdf_conext = new PdfContext();
PdfDocument d = (PdfDocument) pdf_conext.openDocument(pdfPath);
PdfPage vuPage = (PdfPage) d.getPage(0);
RectF rf = new RectF();
Bitmap bitmap = vuPage.renderBitmap(1000,600, rf);
File dir1 = new File (root.getAbsolutePath() + "/IMAGES");
dir1.mkdirs();
String fname = "Image-"+ 2 +".jpg";
File file = new File (dir1, fname);
if (file.exists ())
file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}

Download and show the Thumbnail

I try to download a picture from URL to SD card/Download.
And I try to show its thumbnail in imageview.
Now I had below code:
try {
Download(URL); //download picture to SD card/Download
File myfile = new File(Environment.getExternalStorageDirectory() + "/Download/", filename);
Drawable photo = null;
photo = Drawable.createFromPath(myfile.getPath());
imageview.setBackgroundDrawable(photo);
}
It show the original picture.
But when the picture is large.
The memory error occurs.
So I want to show the smaller picture.
How should I do to generate the thumbnail and show it?
Or how to use the thumbnail generate by Android system?
Use Bitmap, Something like,
try
{
Download(URL); //download picture to SD card/Download
final int THUMBNAIL_SIZE = 64;
FileInputStream fis = new FileInputStream(Environment.getExternalStorageDirectory() + "/Download/", filename);
Bitmap imageBitmap = BitmapFactory.decodeStream(fis);
imageBitmap = Bitmap.createScaledBitmap(imageBitmap, THUMBNAIL_SIZE, THUMBNAIL_SIZE, false);
imageview.setImageBitmap(imageBitmap);
}
catch(Exception ex) {
}
From the Shown Code
Try this instead your last 2 lines
Bitmap photo = Bitmap.createScaledBitmap(BitmapFactory.decodeFile(myfile.getPath()),60,60,true);
imageview.setImageBitmap(photo);
And if you have made any objects for Bitmap/String/Stream in your Download() function free them calling System.gc();
And I hope this will work.

lazyload image in appwidget

Is there a way to lazyload an image from the internet into a remoteview.
remoteView.setImageViewBitmap(R.id.image, UrlUtils.loadBitmap(bitmapUrl));
I use this function but it is blocking my widget during a small time.
public static Bitmap loadBitmap(String url) {
Bitmap bitmap = null;
InputStream in = null;
BufferedOutputStream out = null;
try {
in = new BufferedInputStream(getPageInputStream(url));
final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
out = new BufferedOutputStream(dataStream);
Utils.copy(in, out);
out.flush();
final byte[] data = dataStream.toByteArray();
BitmapFactory.Options options = new BitmapFactory.Options();
// options.inSampleSize = 1;
bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,
options);
in.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
Thanks
Absolutely
Draw your widget the usual way, all the textual parts, etc. beside image
Create a service which will load image. Here's good tutorial that includes how to create and call service from the appwidget
After updating your widget call the service. Pass widget ID and image URL
Load image from cache or remotely in your service and update your widget again. Voila, you have it now
Try android-query lib for lazyload loading.
https://code.google.com/p/android-query/#Image_Loading

android save-Lazy List HashMap

I want to Serialize the Hash-Map of Default Lazy-list code and store it in a file on device..
Actually I want the images to be locally stored,,,,
As,my project contain a lot's of images to be used from server.... Please provide a way or code for modified lazy-list that stores the Hash-Map in a file.. As next time when app restart the Image-loader class must have that object so,,that image is not downloaded from server...
Make a variable named cacheDir and change getBitmap() method of ImageLoader class to this below one
private Bitmap getBitmap(String urlString)
{
String filename = String.valueOf(urlString.substring(urlString.lastIndexOf("/") + 1));
File f = new File(cacheDir, filename);
try
{
if(!f.exists())
{
Bitmap bitmap = null;
InputStream is = new URL(urlString).openStream();
OutputStream os = new FileOutputStream(f);
Globals.CopyStream(is, os);
os.close();
bitmap = decodeFile(f);
return bitmap;
}
else
{
Bitmap bitmap = decodeFile(f);
return bitmap;
}
}
catch (Exception ex)
{
ex.printStackTrace();
BitmapDrawable mDrawable = (BitmapDrawable) context.getResources().getDrawable(R.drawable.placeholder);
return mDrawable.getBitmap();
}
}

Categories

Resources