Pregunta

I want to share image from my listview when I click on item. How I can get image path when I click on list item? I use this listener to handle click:

mListView.setOnItemClickListener(new ListView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> a, View v, int i, long l) {
                // handle click here
                Intent sharingIntent = new Intent(Intent.ACTION_SEND);
                sharingIntent.setType("text/plain");
                sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, a.toString());
                sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject");
                startActivity(Intent.createChooser(sharingIntent, "Share using"));

            }
        });

And my image bmp is created here:

url = new URL(imgUrl);

        // Creating an http connection to communicate with url
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

        // Connecting to url                
        urlConnection.connect();

        // Reading data from url 
        iStream = urlConnection.getInputStream();

        // Getting Caching directory 
        File cacheDirectory = getBaseContext().getCacheDir();

        // Temporary file to store the downloaded image 
        File tmpFile = new File(cacheDirectory.getPath() + "/wpta_"+position+".JPEG");              
      // tmpFile.getParentFile().mkdirs();
        // The FileOutputStream to the temporary file
        FileOutputStream fOutStream = new FileOutputStream(tmpFile);

        // Creating a bitmap from the downloaded inputstream
        Bitmap b = BitmapFactory.decodeStream(iStream);             

        // Writing the bitmap to the temporary file as png file
        b.compress(Bitmap.CompressFormat.JPEG,100, fOutStream);             

        // Flush the FileOutputStream
        fOutStream.flush();

        //Close the FileOutputStream
        fOutStream.close();             

        // Create a hashmap object to store image path and its position in the listview
        HashMap<String, Object> hmBitmap = new HashMap<String, Object>();

        // Storing the path to the temporary image file
        hmBitmap.put("demoPlace",tmpFile.getPath());

        // Storing the position of the image in the listview
        hmBitmap.put("position",position);              

        // Returning the HashMap object containing the image path and position
        return hmBitmap;       
¿Fue útil?

Solución

try this,

mListView.setOnItemClickListener(new ListView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> a, View v, int i, long l) {
                @SuppressWarnings("unchecked")
                HashMap<String, String> data = yourArrayListofhashmap.get(i);

                // handle click here
                Intent sharingIntent = new Intent(Intent.ACTION_SEND);
                sharingIntent.setType("text/plain");
                sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, data.get(yourstring));
                sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject");
                startActivity(Intent.createChooser(sharingIntent, "Share using"));

            }
        });
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top