I tried to use FreeImage library to load PNG as a texture (from memory). That's the fragment of code:
FIMEMORY *fiStream = FreeImage_OpenMemory(streamData, size);
FREE_IMAGE_FORMAT fileFormat = FreeImage_GetFileTypeFromMemory(fiStream, 0);
FIBITMAP *image = FreeImage_LoadFromMemory(fileFormat, fiStream, 0);
int bitsPerPixel = FreeImage_GetBPP(image);
width = (int)FreeImage_GetWidth(image);
height = (int)FreeImage_GetHeight(image);
I'm using FILE with fopen to open file and then read stream to streamData object. File and stream is read correctly.
The result is: fileFormat = -1 and image is NULL.
I also tried to use FreeImage to load PNG file directly from disk using FreeImage_Load, but the result is the same - it returns NULL.
Has anybody faced similar problem? Can you suggest an alternative to FreeImage that can read data from memory?
try this code to load your image from memory:
File file = new File("/sdcard/Images/image1.jpg");
if(file.exists()){
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
ImageView image = (ImageView) findViewById(R.id.imageview);
image.setImageBitmap(bitmap);
}
Related
I am trying to export TeeChart as Image on Xamarin Android without showing the chart. It creates the file on external storage. However, the file is broken and cannot be loaded.
Do you know if it is possible? If it possible, can you give me a sample code doing that?
You can use this to get the view's bitmap and storage it:
Steema.TeeChart.TChart tChart1= new Steema.TeeChart.TChart(this);
Steema.TeeChart.Styles.Bar bar1 = new Steema.TeeChart.Styles.Bar();
tChart1.Series.Add(bar1);
bar1.Add(3, "Pears", System.Drawing.Color.Red);
bar1.Add(4, "Apples", System.Drawing.Color.Blue);
bar1.Add(2, "Oranges", System.Drawing.Color.Green);
Steema.TeeChart.Themes.BlackIsBackTheme theme = new Steema.TeeChart.Themes.BlackIsBackTheme(tChart1.Chart);
theme.Apply();
// here you can get the view' bitmap
tChart1.DrawingCacheEnabled = true;
tChart1.BuildDrawingCache();
Bitmap viewBitmap = tChart1.DrawingCache;
//storage the bitmap.
FileStream stream = File.OpenWrite(FilesDir.AbsolutePath + "111111.jpg");
viewBitmap.Compress(Bitmap.CompressFormat.Jpeg, 90, stream);
Here is the setDrawingCacheEnabled method, which allow you get view's bitmap.
I have an image saved in my Pictures folder, how to display it in a imageview?
like:
imageview.setimage("//Pictures//cat.jpg)
I know it's not a correct code, but I want to achieve something like this, hope someone can help, thanks!
you first generate a bitmap from file path and then put that bitmap to imageview
File image = new File(filePath);
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeFile(image.getAbsolutePath(),bmOptions);
bitmap = Bitmap.createScaledBitmap(bitmap,parent.getWidth(),parent.getHeight(),true);
imageView.setImageBitmap(bitmap);
Edit: Also add this permission in your manifest
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
You can set image like this from the sd card get path and create file variable and decode file using BitmapFactory set imageview image
String path = Environment.getExternalStorageState()+"/Pictures//cat.jpg";
File f = new File(path);
imageview.setImageBitmap(new BitmapFactory.decodeFile(f.getAbsolutePath()));
First you passing wrong file path.
for Correct File path do like this
Environment.getExternalStoragePublicDirectory (Environment.DIRECTORY_PICTURES).getAbsolutePath());
then create your URL like.
String filePath = Environment.getExternalStoragePublicDirectory (Environment.DIRECTORY_PICTURES).getAbsolutePath()) + "/cat.jpg";
Then use like this.
File image = new File(filePath);
imageView.setImageBitmap(new BitmapFactory.decodeFile(image.getAbsolutePath()));
Got it working with combination of the 2 posted codes, thanks for everyone, here's the working code:
Environment.getExternalStoragePublicDirectory (Environment.DIRECTORY_PICTURES).getAbsolutePath();
String filePath = Environment.getExternalStoragePublicDirectory (Environment.DIRECTORY_PICTURES) + "/picFolder/1.jpg";
File image = new File(filePath);
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeFile(image.getAbsolutePath(),bmOptions);
image1.setImageBitmap(bitmap);
Here i have stored an image by converting as string using 'base64 format' on my server,and i could able to display it in the same way on image view .But now i want download it to my sd card .I can see that some people downloading using 'URL'. But in my case there is no URL.I just converted the image into string and stored and able to display it on imageview by reconverting it.
this is the way i reconverted the string into image
//getting string from server using json parsor
String image=json_data.getString("img");
txt.setText(u);
//converting string to image
byte[] imageAsBytes = Base64.decode(p.getBytes(), 0);
im = (ImageView)this.findViewById(R.id.imageView1);
im.setImageBitmap(
BitmapFactory.decodeByteArray(imageAsBytes, 0,imageAsBytes.length));
Use Image Loader libraries instead of yours.
https://github.com/nostra13/Android-Universal-Image-Loader
https://github.com/bumptech/glide
http://square.github.io/picasso/
THIS SNIPPET SHOULD HELP !
// give any name to file xxx.jpg
File filePath = new File(Environment.getExternalStorageDirectory()+"/name.jpg");
FileOutputStream os = new FileOutputStream(filePath, true);
EDIT
Bitmap x = BitmapFactory.decodeByteArray(imageasbytes , 0 , inageasbytes.length());
x.compress(
Bitmap.CompressFormat.JPEG, 85, os);
os.flush();
os.close();
Also , add the following permission .
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
Also if you want to know how to recover it..
String imgFile = Environment.getExternalStorageDirectory() + "/name.jpg";
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile);
Imageview.setimagebitmap(mybitmap);
I am creating one wallpaper application therefore i put some image in asset folder. I need to show this image one by one on button click and store it in sd card.
What i did:
I use ImageView and WebView to show image. First, when i use WebView, i stuck on setting image size because it showing to small and i need to show those image as per device window size.
I use following code but didn't help to adjust image on screen
myWebView.loadUrl("file:///android_asset/image.html");
WebSettings settings = myWebView.getSettings();
settings.setUseWideViewPort(true);
settings.setLoadWithOverviewMode(true);
I also set <src img="someimage.jpg" width=""100%"> but it didn't help me.
Then i use ImageView to show image and able to show image at least in some proper size using following code.
InputStream ims = getAssets().open("31072011234.jpg");
// load image as Drawable
Drawable d = Drawable.createFromStream(ims, null);
// set image to ImageView
imageView.setImageDrawable(d);
My Question is
Which is the good way to show image on screen imageview or webview?. how to take all picture in array when i don't know name of this images and store it in SD card
Give me some hint or reference.
Thanks in advance.
You don't need to put your images in assets folder, you can use res/drawable to store your images and access it as resource.
Using below code you can access images from drawable without need to know the names of image files.
Class resources = R.drawable.class;
Field[] fields = resources.getFields();
String[] imageName = new String[fields.length];
int index = 0;
for( Field field : fields )
{
imageName[index] = field.getName();
index++;
}
int result = getResources().getIdentifier(imageName[10], "drawable", "com.example.name");
and using below code you can save your images to SD card.
File file = new File(extStorageDirectory, "filename.PNG");
outStream = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();
The best way to show an image would be ImageView (That's why it's called an Image View), I recommend that you add the image in res/drawable folder and show the image using:
imageView.setImageResource(R.id.some_image);
The resource can be saved to sdcard using:
Bitmap bm = BitmapFactory.decodeResource( getResources(), R.id.some_image);
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
File file = new File(extStorageDirectory, "someimage.PNG");
outStream = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();
There's a 2MB limit on the server where I need to upload an image.
I'm using this method to downsample a Bitmap Strange out of memory issue while loading an image to a Bitmap object
within this method
public InputStream getPhotoStream(int imageSizeBytes) throws IOException {
int targetLength = 1500;
ByteArrayOutputStream photoStream;
byte[] photo;
Bitmap pic;
final int MAX_QUALITY = 100;
int actualSize = -1;
do {
photo = null;
pic = null;
photoStream = null;
//this calls the downsampling method
pic = getPhoto(targetLength);
photoStream = new ByteArrayOutputStream();
pic.compress(CompressFormat.JPEG, MAX_QUALITY, photoStream);
photo = photoStream.toByteArray();
actualSize = photo.length;
targetLength /= 2;
} while (actualSize > imageSizeBytes);
return new ByteArrayInputStream(photo);
}
This throws OutOfMemoryError on the second iteration. How could I downsample the image below a certain size limit?
I think the problem is happening because you are compressing the image to a in memory representation, you need to free up that memory before you try to compress again.
You need call close() in the photoStream before trying again in order to free up resources.
Also toByteArray() makes a copy of the stream in memory that you have to free up later, why don't you just use the photoStream.size() to check for the file size?
I can post some code if you need.
Instead of this:
pic = null;
Do this:
if (pic!=null)
pic.recycle();
pic = null
If you simply set the bitmap object to null the memory it occupied isn't released immediately. In the second case you are explicitly telling the OS you are done with the bitmap and its okay to release its memory.
Also consider using a compression quality of 90 instead of 100, I believe that will reduce the resulting file size quite a bit.