Cant get ImageView onItemClick from custom ListView Android [EDIT] - android

I've implemented a custom ListView, this is the structure of each row:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="#+id/imagenEvento"
android:layout_width="80dp"
android:layout_height="80dp"
android:scaleType="centerCrop"
android:adjustViewBounds="false"
android:paddingBottom="2dp"
android:contentDescription="#string/contenidoImagen"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:padding="10dp"
android:orientation="vertical" >
<TextView
android:id="#+id/titulo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="17sp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/sitio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/fecha"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
I've setted to my ListView and adapter and it works fine while accesing data, but when i try to convert an ImageView to a byteArray so i can send it to another Activitie, i can't get the ImageView
lv = (ListView) findViewById(R.id.lista);
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
//creo el nuevo objeto para poder llamarlo
Intent intent = new Intent(AllProductsActivity.this, PostActivity.class);
//Creo la informacion para pasar entre actividades
Bundle informacion= new Bundle();
informacion.putInt("id", eventos.get(position).getID());
Bitmap bmp = BitmapFactory.decodeResource(a.getResources(), R.id.imagenEvento);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
informacion.putByteArray("imagen", byteArray);
}
});
I'm getting my bmp NULL:
Bitmap bmp = BitmapFactory.decodeResource(a.getResources(), R.id.imagenEvento);
I guess i should indicate the position of the item due it's in a ListView but i don't know how to do it
[EDIT]
The problem is solved, i'm getting the image in the next Activity, but, i'm getting always the top image from the ListView of this Activity, how can i get the clicked Image?

Instead of:
Bitmap bmp = BitmapFactory.decodeResource(a.getResources(), R.id.imagenEvento);
to get your bitmap image. Try this:
ImageView image = (ImageView)v.findViewByID(R.id.iamgeenEvento);
Bitmap bmp = ((BitmapDrawable)image.getDrawable()).getBitmap();
Your original code was trying to decode the imageview itself as a bitmap and as it's not a bitmap would return a null value.

The ImageView that your using is just a reference so no need to convert it into an array just pass the reference directly as static Or make a static class that holds such references so that you can use it later, But also you can save the image of the imageview as bitmap and encode save that image as byte array and decode it when needed, and later use it imageView as imageView.setImageBitmap(bitmap);
imageView.buildDrawingCache();
Bitmap bmap = imageView.getDrawingCache();
public String BitmapToString(Bitmap bitmap){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG,100,baos);
byte[] b = baos.toByteArray();
String image = Base64.encodeToString(b, Base64.DEFAULT);
return image;
}
public Bitmap StringToBitmap(String image){
byte[] encodeByte = Base64.decode(image, Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(encodeByte, 0 , encodeByte.length);
return bitmap;
}

Related

Displaying bitmap image on Android (OpenCV)

I'm trying to read an image from external storage as an OpenCV Mat file, convert it to a bitmap image and then display the image (review image after picture is taken). After debugging, it looks like everything is working except the setImageBitmap part. Please let me know what I might be doing wrong. Thanks!
public boolean onTouch(View v, MotionEvent event) {
Mat img = Highgui.imread(Environment.getExternalStorageDirectory().getPath() + "/sample_picture_2015-06-09_13-24-53.jpeg");
displayBitmap(img);
return false;
}
public void displayBitmap(Mat img){
Bitmap bm = Bitmap.createBitmap(img.cols(), img.rows(),Bitmap.Config.ARGB_8888);
Utils.matToBitmap(img, bm);
// find the imageview and draw it!
ImageView iv = (ImageView) findViewById(R.id.imageView1);
iv.setImageBitmap(bm);
Log.i(TAG, "Here");
}
And here is my xml file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<org.opencv.samples.tutorial3.Tutorial3View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
android:id="#+id/tutorial3_activity_java_surface_view" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="2dp"/>
</LinearLayout>
Though it's too late, maybe will help others,
In your onTouch,
File imgFile = new File(Environment.getExternalStorageDirectory()+imgName);
if (imgFile.exists()) {
myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
Mat img = new Mat(myBitmap.getHeight(), myBitmap.getWidth(),
CvType.CV_8UC4);
displayBitmap(img);
Now it should work!

ImageView size is changed when changing the image

I have the problem that I have a layout with an ImageView. With AsyncTask I load an image from the web. It is a panorama picture. If I start up the Fragment, the image I load with src="#drawable/pano" is shown perfectly. It has full height and I can scroll through the panorama.
As soon as the new image is loaded into the ImageView the imageview rescales so that the height is:
Before the AsyncTask (Image is loaded from drawables...):
And this is after the AsyncTask loaded the image:
Here is my xml file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:overScrollMode="never"
android:scrollbars="none" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/img1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:src="#drawable/pano" />
</RelativeLayout>
</HorizontalScrollView>
AsyncTask Code:
public class DownloadPanorama extends
AsyncTask<ImageView, Void, Bitmap> {
ImageView imageView;
Bitmap bm;
public DownloadPanorama(ImageView mChart) {
imageView = mChart;
}
#Override
protected Bitmap doInBackground(ImageView... imageViews) {
return download_Image((String) imageView.getTag());
}
#Override
protected void onPostExecute(Bitmap result) {
imageView.setImageBitmap(result);
}
private Bitmap download_Image(String url) {
Bitmap bm = null;
try {
URL aURL = new URL(url);
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
bm = BitmapFactory.decodeStream(bis);
bis.close();
is.close();
return bm;
}
}
Also happens when not using AsyncTask and only using raw setImageBitmap... So I don't know if I have to change the scaleType in my xml file or if I need to set it again after loading a new image?
I would try changing:
android:layout_height="wrap_content"
to
android:layout_height="fill_parent"
in your ImageView.
If that doesn't work, you might be able to scale your bitmap:
myBitmap = Bitmap.createScaledBitmap(myBitmap, width, height,true);
EDIT
It sounds like from your comments and updated question that you know the size you would like the image to be (1200x400). You have two things you can do to get this size. The first, and recommended, is to replace the line
bm = BitmapFactory.decodeStream(bis);
with
BitmapFactory.Options options = new BitmapFactory.Options();
options.outWidth=1200;
options.outHeight=400;
bm = BitmapFactory.decodeStream(bis, new Rect(0,0,0,0), options);
The other option is to change your View bounds. In your comments above, it sounds like the bounds might change, so you would have to repeat this each time you loaded a new image of different size:
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) imageView.getLayoutParams();
params.width = 1200;
params.height = 400;
imageView.setLayoutParams(params);
You need to change
android:adjustViewBounds="true"
to
android:adjustViewBounds="false"

Display image as a byte array: doesn't work

My issue is: I have a byte array which must be display with ImageView. This is my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView imageView = (ImageView)findViewById(R.id.show_image);
byte[] arrayBytes = ...; // It's initialized
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSample = 4;
imageView.setImageBitmap(BitmapFactory.decodeByteArray(arrayBytes, 0, arrayBytes.length, options));
}
The code of activity_main.xml is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/show_image"
android:contentDescription="#string/Imagen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
But it doesn't work: no image has been displayed! What's I'm doing wrong?
To get byte array[], I read a double[][] array 2D from a file. To get byte value for each element of the matrix I used:
byte[] byteArray = new byte[SIZE];
int k = 0;
for(...i) {
for(...j) {
byteArray[k] = Double.valueOf(matrix[i][j]).byteValue();
k++;
}
}
Thanks in advance

Can I display a image in custom layout?

I know I can use the below code to display a image in system layout,
but I hope to display an image in my custom layout, how can I do that? Thanks!
public void onClick(View v) {
// TODO Auto-generated method stub
int id = v.getId();
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("file://" + arrPath[id]), "image/*");
startActivityForResult(intent,ForBrowse);
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/border_ui"
android:orientation="vertical"
android:paddingTop="3dip" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:id="#+id/btnClose"
style="#style/myTextAppearance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/myreturn" />
</LinearLayout>
If the above resolutions are not ok, then try this one, it loads the image from tha page you want:
String url = "file://" + arrPath[id]), "image/*";
Bitmap bmp = fromURL(url);
imgview.setImageBitmap(bmp);
and write this function:
public static Bitmap fromURL(String src) {
try {
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap mybitmap = BitmapFactory.decodeStream(input);
return bitmap;
} catch (Exception exception1) {
return null;
}
after setting the xml up as you have done,
simply add the following code to set image to your imageview
ImageView img = (ImageView)findViewById(R.id.Imageview1);
img.setImageResource(R.drawable.yourimage);
Intent intent = new Intent(this, YourImageViewerActivity.class);
Bundle bundle = new Bundle();
bundle.putString("data", yourImageUri);
intent.putExtras(bundle);
startActivity(intent);
onCreate() method:
Bundle bundle = getIntent().getExtras();
String yourImageUri= bundle.getString("data");
Bitmap bitmap = BitmapFactory.decodeFile(yourImageUri);
ImageView myImageView = (ImageView)findViewById(R.id.imageView1);
myImageView.setImageBitmap(bitmap);
If you are trying to add an image to you layout from the XML file then
<ImageView
android:id="#+id/imageView1"
android:src="#drawable/book" <- Change this to your drawable item
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />

android frame layout with horizontal scroll bar and ovelapped images

i want to create a Frame layout to place a number of images from database, which need to be a horizontal scrollable list. Image Views are creating dynamically based on database value. Images must be overlapped like the attachment . Here is my xml
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:layout_marginTop="20dp"
>
<FrameLayout
android:id="#+id/frmLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:scrollbars="horizontal"
android:layout_weight="1"
>
</FrameLayout>
</HorizontalScrollView>
and Here is java
public void getAllImages(){
cursorImages = dbAdapter.fetchImages();
if (cursorImages.moveToFirst()) {
BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize = 2;
do {
String filename = cursorImages.getString(cursorImages
.getColumnIndex("filename"));
try {
InputStream ims = getAssets().open("images/" + filename);
Bitmap bm = BitmapFactory.decodeStream(ims,null,options);
Matrix mat = new Matrix();
mat.postRotate(30);
Bitmap bMapRotate = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), mat, true);
ImageView im = new ImageView (this);
im.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
im.setImageBitmap(bMapRotate);
frmLayout.addView(im,new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
} catch (IOException ex) {
}
} while (cursorImages.moveToNext());
}
cursorImages.close();
}

Categories

Resources