【问题标题】:android share image from url using array?android使用数组从url共享图像?
【发布时间】:2019-11-26 06:21:17
【问题描述】:

我是 android 新手,我想与我的图像存储阵列中的相应位置共享我的图像。我使用了一个 viewpage 采用者,所以当用户向左或向右滑动时,imageview 会发生变化。现在我想分享一下这是一组imageview代码。

  package anil.card.christmas.christmascard;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import java.io.File;
import java.io.FileOutputStream;

class ChristmasAdopter extends PagerAdapter {
    Context mContext;
    LayoutInflater mLayoutInflater;
    ImageView imageView;

    int imagepostion;
    private int[] mResources = {R.drawable.cs25, R.drawable.cs2,R.drawable.dx,R.drawable.prd, R.drawable.op,
            R.drawable.ud, R.drawable.ok,R.drawable.cs3,R.drawable.mux,R.drawable.ch, R.drawable.cs5,R.drawable.anil,
            R.drawable.cs6, R.drawable.cs7, R.drawable.pk, R.drawable.cs8, R.drawable.cs9,R.drawable.gk, R.drawable.cs10,
            R.drawable.cs11, R.drawable.cs13, R.drawable.cs14, R.drawable.cs15, R.drawable.cs16, R.drawable.cs17, R.drawable.cs19, R.drawable.cs21, R.drawable.cs23, R.drawable.cs1, R.drawable.cs27, R.drawable.cs29, R.drawable.cs31, R.drawable.cs33,
            R.drawable.cs35,R.drawable.newyear,R.drawable.ny,R.drawable.mn,R.drawable.hp,R.drawable.lifenew,

    R.drawable.cat,R.drawable.dog,R.drawable.flower,R.drawable.ws,R.drawable.chars,
    R.drawable.ship,R.drawable.bells,R.drawable.gift,R.drawable.dj};

    public ChristmasAdopter(Context context) {
        mContext = context;
        mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return mResources.length;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == ((LinearLayout) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View itemView = mLayoutInflater.inflate(R.layout.pager_item, container, false);

        imageView = (ImageView) itemView.findViewById(R.id.imageView);

      imagepostion=position;
    imageView.setImageResource(mResources[position]);

    container.addView(itemView);

        return itemView;
    }


    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((LinearLayout) object);
    }


// here is share content please tell me how i can share image which is show in imageView
    public void shareContent(){

        Toast.makeText(mContext, ""+imagepostion, Toast.LENGTH_SHORT).show();



    }

    private Bitmap getBitmapFromView(View view) {
        Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(returnedBitmap);
        Drawable bgDrawable =view.getBackground();
        if (bgDrawable!=null) {
            bgDrawable.draw(canvas);
        }   else{
            canvas.drawColor(Color.WHITE);
        }
        view.draw(canvas);
        return returnedBitmap;
    }

}



    }

【问题讨论】:

  • @PeterAlwin 请检查我的代码
  • 使用 recyclerview 怎么样?你能试试那个吗?
  • 当用户左右滑动时,它不是 recylerview 它的 pageAdopter 和单个图像的 viewpager,它会在 imageView 上显示图像,所以用户现在选择与朋友共享任何图像我只共享图像时遇到问题
  • 请明确分享图片的位置。如果你提到这一点,只有这样人们才能帮助你。有多种共享选项。您可以将图像从您的应用程序(即 facebook、whatsapp 等)分享到社交媒体。或者您可以通过 post apis.etc 与服务器共享它。还有很多其他选择。请澄清一下,你想做什么。

标签: java android android-intent


【解决方案1】:

在您的 ChristmasAdopter 类中,将 instantiateItem() 替换为,

@Override
public Object instantiateItem(ViewGroup container, int position) {
    View itemView = mLayoutInflater.inflate(R.layout.pager_item, container, false);

    imageView = (ImageView) itemView.findViewById(R.id.imageView);

  imagepostion=position;
  imageView.setImageResource(mResources[position]);

  imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
//save image to device
      saveImage();
//share image code
      shareImageFromDevice();
    }
    });
    container.addView(itemView);

    return itemView;
}

private void saveImage(){

Bitmap bitmap = BitmapFactory.decodeResource(getResources(),mResources[position]);

File image = new File(getExternalFilesDir(null), "test.png");
// After that, you just have to write the Bitmap thanks to its method compress such as:

boolean success = false;

// Encode the file as a PNG image.
FileOutputStream outStream;
try {

    outStream = new FileOutputStream(image);
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
    /* 100 to keep full quality of the image */

    outStream.flush();
    outStream.close();
    success = true;
} catch (IOException e) {
    e.printStackTrace();
}
//Finally, just deal with the boolean result if needed. Such as:

if (success) {
    Toast.makeText(getApplicationContext(), "Image saved with success",
            Toast.LENGTH_LONG).show();
} else {
    Toast.makeText(getApplicationContext(),
            "Error during image saving", Toast.LENGTH_LONG).show();
}
}
    private void shareImageFromDevice(){

    File file = new File(getExternalFilesDir(null), "test.png");

    Uri uri=Uri.fromFile(file);

    Intent share = new Intent(Intent.ACTION_SEND);
    share.setType("image/*");
    share.putExtra(Intent.EXTRA_STREAM, uri);
    startActivity(Intent.createChooser(share, "Share Image"));
}

【讨论】:

  • 我没有使用过 FileProvider。但我以不同的方式做到了。
  • 现在在应用程序中,当您向右或向左滑动时,图像会发生变化。当您触摸图像时,将出现一个选择器对话框屏幕,以便您可以共享图像。网上有很多可用的链接。如果您对其中一个不满意,您可以参考另一个。这项简单的研究应该由您自己完成。
  • 它说分享失败重试我在whatsapp上发送?
  • @joshua 自 Android API 29 以来几乎没有什么变化。这是一个更新的代码。您需要先将文件保存到设备然后共享它。我也测试过。这应该有效。
猜你喜欢
  • 2023-04-03
  • 1970-01-01
  • 1970-01-01
  • 2021-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-01
相关资源
最近更新 更多