【问题标题】:How to share an image on social sites from a folder如何从文件夹在社交网站上共享图像
【发布时间】:2017-07-06 05:13:29
【问题描述】:

我将一些图像保存在内部存储器的文件夹中,并在单击按钮时显示这些所有保存的图像。现在我想在社交网站上分享当前打开的图片,如 facebook、gmail 等。我可以分享文字但不能分享图片。

保存图片的代码是...

 RelativeLayout content = (RelativeLayout) findViewById(R.id.relative);
        content.setDrawingCacheEnabled(true);
        Bitmap bitmap = content.getDrawingCache();

        File myDir=new File("/sdcard/MyCollection");
        myDir.mkdirs();
        Random generator = new Random();
        int n = 10000;
        n = generator.nextInt(n);
        String fname = "Image-"+ n +".jpg";
        File file = new File (myDir, fname);
        if (file.exists ()) file.delete ();
        FileOutputStream outStream;
        try {
            outStream = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
            outStream.flush();
            outStream.close();
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }

图片访问代码是.......

ImageButtonsharingButton = new ImageButton(this); sharedButton.setLayoutParams(new ViewGroup.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT)); sharedButton.setImageResource(R.drawable.alert);

    getFromfolder();
    String[] projection = {MediaStore.Images.Thumbnails._ID};

    cursor = managedQuery( MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
            projection,
            null,
            null,
            MediaStore.Images.Thumbnails.IMAGE_ID);
    columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
    GridView sdcardImages = (GridView) findViewById(R.id.gridview);
    sdcardImages.setAdapter(new ImageAdapter());
    sdcardImages.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView parent, View v, int position, long id) {
            String[] projection = {MediaStore.Images.Media.DATA};
            cursor = managedQuery( MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                    projection,
                    null,
                    null,
                    null);
            columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToPosition(position);
            String imagePath = cursor.getString(columnIndex);
        }
    });

}

public void getFromfolder()
{
    File file= new File(android.os.Environment.getExternalStorageDirectory(),"MyCollection");

    if (file.isDirectory())
    {
        listFile = file.listFiles();
        for (int i = 0; i < listFile.length; i++)
        {
            f.add(listFile[i].getAbsolutePath());
        }
    }
}

public class ImageAdapter extends BaseAdapter {
    private LayoutInflater mInflater;

    public ImageAdapter() {
        mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    public int getCount() {
        return f.size();
    }
    public Object getItem(int position) {
        return position;
    }
    public long getItemId(int position) {
        return position;
    }
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(
                    R.layout.gelleryitem, null);
            holder.imageview = (ImageView) convertView.findViewById(R.id.thumbImage);
            convertView.setTag(holder);
        }
        else {
            holder = (ViewHolder) convertView.getTag();
        }
        Bitmap myBitmap = BitmapFactory.decodeFile(f.get(position));
        holder.imageview.setImageBitmap(myBitmap);
        return convertView;
    }
}
class ViewHolder {
    ImageView imageview;
}

【问题讨论】:

    标签: android


    【解决方案1】:

    首先,您必须从文件路径中获取图像作为位图。然后您可以通过意图将图像传递给其他应用程序。如果您想要一个字符串与图像一起传递,您可以将它作为EXTRA_TEXT 传递给意图。在权限中添加 WRITE_EXTERNAL_STORAGE。试试这个:

    File imgFile = new  File(imagePath);
    if(imgFile.exists()){
          Bitmap icon = BitmapFactory.decodeFile(imagePath);
          Intent share = new Intent(Intent.ACTION_SEND);
          share.setType("image/jpeg");
    
          ContentValues values = new ContentValues();
          values.put(Images.Media.TITLE, "title");
          values.put(Images.Media.MIME_TYPE, "image/jpeg");
          Uri uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI,
            values);
    
    
          OutputStream outstream;
          try {
             outstream = getContentResolver().openOutputStream(uri);
             icon.compress(Bitmap.CompressFormat.JPEG, 100, outstream);
          outstream.close();
          } catch (Exception e) {
             System.err.println(e.toString());
          }
    
          //text
          share.putExtra(Intent.EXTRA_TEXT, "Text that has to be shared");
          //image
          share.putExtra(Intent.EXTRA_STREAM, uri);
          startActivity(Intent.createChooser(share, "Share Image"));
    }
    

    【讨论】:

    【解决方案2】:

    要将图像添加为电子邮件附件,请查看此处的 Intent.EXTRA_STREAM 部分:https://developer.android.com/guide/components/intents-common.html#Email

    Facebook 看这里:https://developers.facebook.com/docs/sharing/android

    【讨论】:

    • 但我想与所有社交网站分享
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多