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 8830509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:06:32+00:00 2026-06-14T08:06:32+00:00

Im showing some images from internet to my listview using an image dowloader.The images

  • 0

Im showing some images from internet to my listview using an image dowloader.The images are stored in sd card using hashcode.How can i share those images from via email.In the default Gmail client shows i have attached the images.But in actual main i could not see any attachment.Only email body is availble . Here is the code im using

Activity

public class MainActivity extends Activity {
    public static final String arrGroupelements[] = { "http://api.androidhive.info/music/images/adele.png",
        "http://api.androidhive.info/music/images/eminem.png", "http://api.androidhive.info/music/images/mj.png"

};
    ListView lv;
    Button btn;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Intializeviews();
        ImgAdapter adapter = new ImgAdapter(MainActivity.this, arrGroupelements);
        lv.setAdapter(adapter);

        setListenersForViews();
    }

    private void setListenersForViews() {
    btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            if(arrGroupelements.length>0)
            {

                Intent mailIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);

                mailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,new String[]{""});
                mailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Hows the pic");
                ArrayList<Uri> uris = new ArrayList<Uri>();

                String mailText =  "";


            for(int i=0;i<arrGroupelements.length;i++)
            {

                String ImagePath = arrGroupelements[i].toString();

                StringBuilder builder = new StringBuilder();

                String sampleFile= Download(ImagePath);

                mailText = mailText+"i shared "+ImagePath;  
                try
                {
                     ContentValues values = new ContentValues(7);
                     values.put(Images.Media.TITLE, sampleFile);
                     values.put(Images.Media.DISPLAY_NAME, ImagePath);
                     values.put(Images.Media.DATE_TAKEN, new Date().getTime());
                     values.put(Images.Media.MIME_TYPE, "image/jpeg");
                     values.put(Images.ImageColumns.BUCKET_ID, sampleFile.hashCode());
                     values.put(Images.ImageColumns.BUCKET_DISPLAY_NAME, sampleFile);
                     values.put("_data", sampleFile);
                     ContentResolver contentResolver = getApplicationContext().getContentResolver();
                     Uri uri = contentResolver.insert(Images.Media.EXTERNAL_CONTENT_URI, values);
                     uris.add(uri);
                }
                catch (Exception e) 
                {

                }

            }
            Bundle bundle = new Bundle();
            mailIntent.putExtra(Intent.EXTRA_TEXT, mailText);

            mailIntent.putParcelableArrayListExtra(android.content.Intent.EXTRA_STREAM, uris);
            mailIntent.setType("multipart/mixed");

                startActivityForResult(Intent.createChooser(mailIntent, "Choose client"),500);
            }

        }

        private String Download(String url) {

            String filename = String.valueOf(url.hashCode());

            File f = new File(android.os.Environment.getExternalStorageDirectory().getAbsolutePath() +File.separator +"data/test/images/", filename+".png");
            if(f.isFile())
            {
            return android.os.Environment.getExternalStorageDirectory().getAbsolutePath() +File.separator +"data/test/images/"+File.separator+ filename+".png";
            }
            return "";

        }


    });


    }

    private void Intializeviews() {
         lv=(ListView)findViewById(R.id.lv);
         btn=(Button)findViewById(R.id.btn);
    }


}

ImgAdapter

public class ImgAdapter extends ArrayAdapter<String> {

    private final Activity context;
    private final String[] names;
    ImageDownloader downloader;

    public ImgAdapter(Activity context, String[] names) {

        super(context, R.layout.lvrow, names);
        this.context = context;
        this.names = names;
        Log.e("size of names is ", String.valueOf(names.length));
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = context.getLayoutInflater();
        View rowView = inflater.inflate(R.layout.lvrow, null, true);
        ImageView img = (ImageView) rowView.findViewById(R.id.img);

        String url = names[position].toString();

        downloader = new ImageDownloader();

        downloader.download(url, img);

        return rowView;

    }

}

ImageDownloader

public class ImageDownloader {

    Map<String,Bitmap> imageCache;

    public ImageDownloader(){
        imageCache = new HashMap<String, Bitmap>();

    }

    //download function
    public void download(String url, ImageView imageView) {
         if (cancelPotentialDownload(url, imageView)&&url!=null) {

             //Caching code right here
             String filename = String.valueOf(url.hashCode());
             File f = new File(getCacheDirectory(imageView.getContext()), filename);

              // Is the bitmap in our memory cache?
             Bitmap bitmap = null;

              bitmap = (Bitmap)imageCache.get(f.getPath());
                BitmapFactory.Options bfOptions=new BitmapFactory.Options();
                bfOptions.inDither=false;                     //Disable Dithering mode
                bfOptions.inPurgeable=true;                   //Tell to gc that whether it needs free memory, the Bitmap can be cleared
                bfOptions.inInputShareable=true;              //Which kind of reference will be used to recover the Bitmap data after being clear, when it will be used in the future
                bfOptions.inTempStorage=new byte[32 * 1024]; 
                FileInputStream fs=null;

              if(bitmap == null){

                  //bitmap = BitmapFactory.decodeFile(f.getPath(),options);
                  try {
                      fs = new FileInputStream(f);
                        if(fs!=null) bitmap=BitmapFactory.decodeFileDescriptor(fs.getFD(), null, bfOptions);
                    } catch (IOException e) {
                        //TODO do something intelligent
                        e.printStackTrace();
                    } finally{ 
                        if(fs!=null) {
                            try {
                                fs.close();
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }
                    }

                  if(bitmap != null){
                      imageCache.put(f.getPath(), bitmap);
                  }

              }
              //No? download it
              if(bitmap == null){
                  BitmapDownloaderTask task = new BitmapDownloaderTask(imageView);
                  DownloadedDrawable downloadedDrawable = new DownloadedDrawable(task);
                  imageView.setImageDrawable(downloadedDrawable);
                  task.execute(url);
              }else{
                  //Yes? set the image
                  imageView.setImageBitmap(bitmap);
              }
         }
    }

    //cancel a download (internal only)
    private static boolean cancelPotentialDownload(String url, ImageView imageView) {
        BitmapDownloaderTask bitmapDownloaderTask = getBitmapDownloaderTask(imageView);

        if (bitmapDownloaderTask != null) {
            String bitmapUrl = bitmapDownloaderTask.url;
            if ((bitmapUrl == null) || (!bitmapUrl.equals(url))) {
                bitmapDownloaderTask.cancel(true);
            } else {
                // The same URL is already being downloaded.
                return false;
            }
        }
        return true;
    }

    //gets an existing download if one exists for the imageview
    private static BitmapDownloaderTask getBitmapDownloaderTask(ImageView imageView) {
        if (imageView != null) {
            Drawable drawable = imageView.getDrawable();
            if (drawable instanceof DownloadedDrawable) {
                DownloadedDrawable downloadedDrawable = (DownloadedDrawable)drawable;
                return downloadedDrawable.getBitmapDownloaderTask();
            }
        }
        return null;
    }

    //our caching functions
    // Find the dir to save cached images
    public static File getCacheDirectory(Context context){
        String sdState = android.os.Environment.getExternalStorageState();
        File cacheDir;

        if (sdState.equals(android.os.Environment.MEDIA_MOUNTED)) {
            File sdDir = android.os.Environment.getExternalStorageDirectory();  

            //TODO : Change your diretcory here
            cacheDir = new File(sdDir,"data/test/images");
        }
        else
            cacheDir = context.getCacheDir();

        if(!cacheDir.exists())
            cacheDir.mkdirs();
            return cacheDir;
    }

    private void writeFile(Bitmap bmp, File f) {
          FileOutputStream out = null;

          try {
            out = new FileOutputStream(f);
            bmp.compress(Bitmap.CompressFormat.PNG, 80, out);
          } catch (Exception e) {
            e.printStackTrace();
          }
          finally { 
            try { if (out != null ) out.close(); }
            catch(Exception ex) {} 
          }
    }
    ///////////////////////

    //download asynctask
    public class BitmapDownloaderTask extends AsyncTask<String, Void, Bitmap> {
        private String url;
        private final WeakReference<ImageView> imageViewReference;

        public BitmapDownloaderTask(ImageView imageView) {
            imageViewReference = new WeakReference<ImageView>(imageView);
        }

        @Override
        // Actual download method, run in the task thread
        protected Bitmap doInBackground(String... params) {
             // params comes from the execute() call: params[0] is the url.
             url = (String)params[0];
             return downloadBitmap(params[0]);
        }

        @Override
        // Once the image is downloaded, associates it to the imageView
        protected void onPostExecute(Bitmap bitmap) {
            if (isCancelled()) {
                bitmap = null;
            }

            if (imageViewReference != null) {
                ImageView imageView = imageViewReference.get();
                BitmapDownloaderTask bitmapDownloaderTask = getBitmapDownloaderTask(imageView);
                // Change bitmap only if this process is still associated with it
                if (this == bitmapDownloaderTask) {
                    imageView.setImageBitmap(bitmap);

                    //cache the image


                    String filename = String.valueOf(url.hashCode());
                    File f = new File(getCacheDirectory(imageView.getContext()), filename);

                    imageCache.put(f.getPath(), bitmap);

                    writeFile(bitmap, f);
                }
            }
        }


    }

    static class DownloadedDrawable extends ColorDrawable {
        private final WeakReference<BitmapDownloaderTask> bitmapDownloaderTaskReference;

        public DownloadedDrawable(BitmapDownloaderTask bitmapDownloaderTask) {
            super(Color.BLACK);
            bitmapDownloaderTaskReference =
                new WeakReference<BitmapDownloaderTask>(bitmapDownloaderTask);
        }

        public BitmapDownloaderTask getBitmapDownloaderTask() {
            return bitmapDownloaderTaskReference.get();
        }
    }

    //the actual download code
    static Bitmap downloadBitmap(String url) {
        HttpParams params = new BasicHttpParams();
        params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
        HttpClient client = new DefaultHttpClient(params);
        final HttpGet getRequest = new HttpGet(url);

        try {
            HttpResponse response = client.execute(getRequest);
            final int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode != HttpStatus.SC_OK) { 
                Log.w("ImageDownloader", "Error " + statusCode + " while retrieving bitmap from " + url); 
                return null;
            }

            final HttpEntity entity = response.getEntity();
            if (entity != null) {
                InputStream inputStream = null;
                try {
                    inputStream = entity.getContent(); 
                    final Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                    return bitmap;
                } finally {
                    if (inputStream != null) {
                        inputStream.close();  
                    }
                    entity.consumeContent();
                }
            }
        } catch (Exception e) {
            // Could provide a more explicit error message for IOException or IllegalStateException
            getRequest.abort();
            Log.w("ImageDownloader", "Error while retrieving bitmap from " + url + e.toString());
        } finally {
            if (client != null) {
                //client.close();
            }
        }
        return null;
    }
}

activity_main

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Email Share" />

    <ListView
        android:id="@+id/lv"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent" >
    </ListView>

</LinearLayout>

lvrow

<?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="wrap_content"
    android:orientation="vertical"
    android:background="@android:color/darker_gray"
     >
    <ImageView 
        android:id="@+id/img"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:background="@drawable/ic_launcher"
        />

</LinearLayout>

Manifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.multipleemailattachment"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_SETTINGS" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />


    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Am i missing anything?

  • 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-14T08:06:33+00:00Added an answer on June 14, 2026 at 8:06 am

    According to this thread : Android multiple email attachments using Intent, your intent mimetype is wrong. It should be :

     emailIntent.setType("text/plain");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some images stored in getExternalFilesDir() and i am trying to show those
When I'am selecting image from gallery and showing the image in ImageView some of
When I use images from the internet in a Listview , my Listview scrolls
ASP.NET Application I am having some confusion with showing images. IE is fully working
I am try to bind the dropdowmlist using jquery. But is showing some error.
I am having some trouble loading images from a file into an array. I
I creating some code for showing box with diffrent heights (height will be from
I am creating a collage combined with elements from other images. Here is some
I'm using fancybox to show some images in a gallery. I have a main
Hi everyone I have some values of intensities from images of yeast colony plates.

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.