image view Shared preferences - android

I'm newbie in android. My question is how to set shared preferences in image view. I want to shared the image to another activity. Please help me because I'm stocked on it.. Please help me the explain me clearly and codes. Thank you.

The "standard" way to share data across Activities is usign the putExtraXXX methods on the intent class. You can put the image path in your intent:
Intent intent = new Intent(this,MyClassA.class);
intent.putExtra(MyClassA.IMAGE_EXTRA, imagePath);
startActivity(intent);
And you retrieve it and open it in your next Activity:
String filePath = getIntent().getStringExtra(MyClassA.IMAGE_EXTRA);
Here is an implementation of a function that opens and decodes the image and return a Bitmap object, notice that this function requires the image to be located in the assets folder:
private Bitmap getImageFromAssets(String assetsPath,int reqWidth, int reqHeight) {
AssetManager assetManager = getAssets();
InputStream istr;
Bitmap bitmap = null;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
try {
istr = assetManager.open(assetsPath);
bitmap = BitmapFactory.decodeStream(istr, null, options);
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
options.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeStream(istr, null, options);
} catch (IOException e) {
return null;
}
return bitmap;
}

Related

Generate image byte from image Path

I have an android application i'm developing but i have come to face the problem of converting image path into bytes.I have a C library for matching images and i want to pass image from sqlite into library so that the image can be matched with other image, Here is the code block that i receive an image path from sqlite:
private byte matdata[] = new byte[512];
for (Multimedia multdata : multimedialist) {
String log = "Id: " + multdata.getID() + " ,FID: "
+ multdata.getFID() + " ,PATH: " + multdata.getPath();
String imagePath = multdata.getPath();
clibrary.GetTemplateByGen(matdata, matsize);
mret = clibrary.MatchTemplate(refdata, refsize[0], matdata, matsize[0]);
Log.i("MATCH" ,""+mret);
Log.i("PATH" ,""+multdata.getPath());
}
As per code block above ,i need to to convert the value for String imagePath to new byte[512] so that i can pass it on clibrary.GetTemplateByGen(matdata, matsize); as clibrary.GetTemplateByGen("BYTES PASSED AFTER IMAGEPATH CONVERTED TO BYTES[512]", matsize);
I have tried to convert path into bitmap and then into byte as below:
Bitmap bitmap = decodeImg(imagePath, 150, 150);
final int lnth=bitmap.getByteCount();
ByteBuffer dst= ByteBuffer.allocate(lnth);
bitmap.copyPixelsToBuffer( dst);
byte[] bytearray=dst.array();
}public static Bitmap decodeImg(String path, int reqWidth, int reqHeight) {
File file = new File(path);
if (file.exists()) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);'
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(path, options);
} else
return null;
}`
but still the library can not perform matching for the byte array formed.
Can anyone assist on that as i have spent a lot of time solving the issue ,Thanks in advance

BitmapFactory.decodeStream returning null with bmOptions

Trying to get image from gallery, my photo uri : content://com.android.providers.media.documents/document/image%3A15672
when i don't use bmOptions(BitmapFactory.decodeStream(inStream)) i get bitmap image succesfully, but when i add bmOptionsBitmapFactory.decodeStream(inStream,null,bmOptions)) i get null bitmap, unable to figure out what am doing wrong.
private void setPic(Uri photoUri) {
InputStream inStream = null;
try {
inStream = getContentResolver().openInputStream(photoUri);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
Log.i("response", "INPUT STREAM| Bitmap : "+ BitmapFactory.decodeStream(inStream,null,bmOptions));
}
That is expected behavior if you use bmOptions.inJustDecodeBounds = true;, which can be translated to human language as don't load the bitmap, just resolve it's size and some other metadata. It is usually used to know Bitmap size before loading it to memory to prevent OOM exceptions, and load bitmap pre-down-scaled.

Android - SkImageDecoder::Factory only when i read image with extension .png

In my Android app i have to read a lot of images, for this reason i have implemented image caching. This is the method through which i decode my images(that were stored in the assets/ folder):
private Bitmap getBitmapFromAsset(String strName)
{
AssetManager assetManager = context.getAssets();
InputStream istr = null;
try {
istr = assetManager.open(strName);
} catch (IOException e) {
e.printStackTrace();
}
return decodeFileFromAssets(istr);
}
private Bitmap decodeFileFromAssets(InputStream stream ){
//decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(stream,null,o);
final int REQUIRED_WIDTH=1280;
final int REQUIRED_HIGHT=720;
int scale=1;
while(o.outWidth/scale/2>=REQUIRED_WIDTH && o.outHeight/scale/2>=REQUIRED_HIGHT)
scale*=2;
//decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize=scale;
return BitmapFactory.decodeStream(stream, null, o2);
}
I have notice that when i read .jpg images its all ok, but when i read .png images i came across to this annoying error:
D/skia﹕ --- SkImageDecoder::Factory returned null
how can i resolve this issue without converting all the .png images into .jpg images??
EDIT
This issue is presenting only when i have to show a .png images from the assets folder and not for all other format. Why?
EDIT 2
I have edited my code in order to explain better the function of my methods...

Java.IO.File to System.IO.Stream in C#?

I am trying to move a part of code written in Java (Android) to C# (Mono for android) and I am stuck at finding a way to do this. The part of the code in Java is as follows:
private Bitmap decodeFile(File f){
try {
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f), null, o);
...
} catch(FileNotFoundException ex){
}
Precisely, converting from Java.IO.File to System.IO.Stream as required by the first parameter of DecodeStream is my problem. How should this statement be rewritten?
I usually use the static System.File methods to obtain the corresponding FileStream:
var stream = File.OpenRead("PathToFile")
In your case, you should get rid of the "File" class that you have in java: File is a static class in .NET. Can you pass the path directly (as a String) to your decodeFile function?
private Bitmap decodeFile(string f){
try {
var o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
using (var stream = File.OpenRead(f)) {
BitmapFactory.decodeStream(stream, null, o);
...
}
} catch(FileNotFoundException ex){
}

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

Categories

Resources