Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7731041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T06:22:49+00:00 2026-06-01T06:22:49+00:00

i got images from SD card folder and added them to LinearLayout dynamically which

  • 0

i got images from SD card folder and added them to LinearLayout dynamically which is inside a HorizontalScrollview.now when i click an image i want to show that particular image in another ImageView in the same activity as big image. how can i do that? i tried by getting getId(),getTag() but i am unable to do, please give me suggestions.
Note:I dont want use gallery widget because of center-locking feature.
My code :

    package com.pop.cam;

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;

    import android.app.Activity;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.os.Bundle;
    import android.view.View;
    import android.view.ViewGroup.LayoutParams;
    import android.widget.ImageView;
    import android.widget.LinearLayout;

    public class GalleryView extends Activity {

        ImageView iv;
        File[] sdDirFiles;
        LinearLayout linearLayout;
        int i;
        String path;

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
            setContentView(R.layout.pictures);
            linearLayout = (LinearLayout) findViewById(R.id.linearView);
            iv = (ImageView) findViewById(R.id.ImageView);

            // getting images from SD card folder
            File sdDir = new File("/sdcard/Pictures/"
                    + MyCameraAppActivity.DIR_NAME);
            sdDirFiles = sdDir.listFiles();
            for (i = 0; i < sdDirFiles.length; i++) {
                final ImageView imageView = new ImageView(this);
                imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                imageView.setLayoutParams(new LayoutParams(130, 100));
                Bitmap b = decodeFile(sdDirFiles[i].getAbsoluteFile());
                imageView.setImageBitmap(b);
                path = sdDirFiles[i].getAbsolutePath();
                imageView.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // Here i want to set clicked image as big.
                                        //I tried following but not workin
                        iv.setImageResource(v.getId());
                                        iv.setImageURI(Uri.parse(path));
Bitmap b = decodeFile(sdDirFiles[Integer.valueOf(v.getId())].getAbsoluteFile());
                    iv.setImageBitmap(b);

                    }
                });
                linearLayout.addView(imageView);
            }
        }

        // decodes image and scales it to reduce memory consumption
        private Bitmap decodeFile(File f) {
            try {
                // Decode image size
                BitmapFactory.Options o = new BitmapFactory.Options();
                o.inJustDecodeBounds = true;
                BitmapFactory.decodeStream(new FileInputStream(f), null, o);

                // The new size we want to scale to
                final int REQUIRED_SIZE = 70;

                // Find the correct scale value. It should be the power of 2.
                int scale = 1;
                while (o.outWidth / scale / 2 >= REQUIRED_SIZE
                        && o.outHeight / scale / 2 >= REQUIRED_SIZE)
                    scale *= 2;

                // Decode with inSampleSize
                BitmapFactory.Options o2 = new BitmapFactory.Options();
                o2.inSampleSize = scale;
                return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
            } catch (FileNotFoundException e) {
            }
            return null;
        }
    }

pictures.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <HorizontalScrollView
        android:id="@+id/horizontalScorllView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:id="@+id/linearView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
        </LinearLayout>
    </HorizontalScrollView>

    <ImageView
        android:id="@+id/ImageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:contentDescription="@string/app_name" >
    </ImageView>

</LinearLayout>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T06:22:50+00:00Added an answer on June 1, 2026 at 6:22 am

    Add below line before imageView.setOnClickListener(new View.OnClickListener() {

    imageView.setId(i);
    

    Use below lines in place of iv.setImageResource(v.getId());

    int id = v.getId();
    Bitmap b = decodeFile(sdDirFiles[id].getAbsoluteFile()); // this gives small image bcoz you implemented decodeFile like that.
    iv.setImageBitmap(b);
    

    or

    int id = v.getId();                 
    Uri uri = Uri.fromFile(sdDirFiles[id].getAbsoluteFile()); // it will produce original resolution or fit to screen
    iv.setImageURI(uri);
    

    I tested this code, its working fine.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a script that dynamically calls and displays images from a directory, what
I've got a folder of images which is reaching a critical mass after a
I'm trying to read images from the SD CARD and save them in my
How can I fetch images from a server? I've got this bit of code
I have got an ActiveX Control that gets an image from a fingerprint device
Im new to droid programming and i got a simple retrieve image from url
I've got raw data streams from image files, like: vector<char> rawData(fileSize); ifstream inFile(image.jpg); inFile.read(&rawData[0]);
I'm trying to get the pixel information from an image and have got to
I'm trying to retrieve images from a folder using glob() and i want it
I'm generating images from an original and storing them in a table. I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.