如何将图像共享到whatsapp时"The File formate is not supported"解决此类错误?



我正在实现一个应用程序,在点击"共享按钮"时将图像共享到whatsapp,根据这个项目,我创建了一个扩展Baseadapter的Imageadapter和一个共享图像的活动。所以,当我点击"共享按钮"时,它应该会生成"不支持文件格式"。

ImageAdapter.java

public class ImageAdapter extends BaseAdapter {
private Context mContext;
public int getCount() {
    return mThumbIds.length;
}
public Object getItem(int position) {
    return mThumbIds[position];
}
public long getItemId(int position) {
    return 0;
}
public ImageAdapter(Context c) {
    mContext = c;
}
public View getView(final int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if (convertView == null){
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(80, 80));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(15, 15, 15, 15);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext,FullScreenImage.class);
                intent.putExtra("imageID", mThumbIds[position]);
                mContext.startActivity(intent);
            }
        });

    }
    else{
        imageView = (ImageView) convertView;
    }
    imageView.setImageResource(mThumbIds[position]);
    return imageView;
}
private Integer[] mThumbIds = {
        R.mipmap.h1, R.mipmap.h2,
        R.mipmap.h3, R.mipmap.h4,
        R.mipmap.h5,R.mipmap.h6,
        R.mipmap.h7,R.mipmap.h8
        ,R.mipmap.h9,R.mipmap.h10
        ,R.mipmap.h11,R.mipmap.h12
        ,R.mipmap.h13,R.mipmap.h14
        ,R.mipmap.h15
};
}

FullScreenImage.java

public class FullScreenImage extends Activity {

ImageView imageView,imgBack;
Button setWallpaper,share;
Context activity;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_fullscreen_image);
    //Get image fro Image adapter
    Bundle bdl = getIntent().getExtras();
    final int imageRes = bdl.getInt("imageID");
    //Setting up imageview
    imageView = (ImageView)findViewById(R.id.imgDisplay);
    //Share image to whatsapp
    share = (Button)findViewById(R.id.btnShare);
    share.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Uri imageUri = Uri.parse("android.resource://com.v1.sensoft.halloween/mipmap"+imageRes);
            Intent intent = new Intent(Intent.ACTION_SEND);
            intent.setType("image/*");
            intent.putExtra(Intent.EXTRA_STREAM, imageUri);
            startActivity(Intent.createChooser(intent , "Share"));
        }
    });
    //Setting up wallpaper
    ImageView image = (ImageView) findViewById(R.id.imgDisplay);
    image.setImageResource(imageRes);
    setWallpaper = (Button)findViewById(R.id.setWall);
    setWallpaper.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            WallpaperManager myWallpaperManager
                    = WallpaperManager.getInstance(getApplicationContext());
            try {
                myWallpaperManager.setResource(imageRes);
                Context context = getApplicationContext();
                // Create layout inflator object to inflate toast.xml file
                LayoutInflater inflater = getLayoutInflater();
                // Call toast.xml file for toast layout
                View toastRoot = inflater.inflate(R.layout.layout_toast2, null);
                Toast toast = new Toast(context);
                // Set layout to toast
                toast.setView(toastRoot);
                toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL,
                        0,0 );
                toast.setDuration(Toast.LENGTH_LONG);
                toast.show();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    });

    //Header back button...for Mainactivity.java
    imgBack = (ImageView)findViewById(R.id.imgBack);
    imgBack.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intentLog9 = new Intent(getApplicationContext(), MainActivity.class);
            startActivity(intentLog9);
            finish();
        }
    });
}
}

如果有人知道问题出在哪里,请告诉我。提前感谢。。。

尝试将.jpg附加到图像文件名。fileName应该有一个有效的图像扩展名,如.jpg或.jpeg.

在您的onClick中,尝试实现以下代码:

Intent intent = new Intent(mContext,FullScreenImage.class);
                intent.putExtra("imageID", mThumbIds[position]+".jpg");
                mContext.startActivity(intent);
code `Intent sendIntent = new Intent();
    sendIntent.setAction(Intent.ACTION_SEND);
    List<String> images_url = shareContent.getImages_url();
    int type = shareContent.getType();
    ArrayList<Uri> uriList = new ArrayList<Uri>();
    for (int i = 0; i < images_url.size(); i++) {
        String localUrlFromUrl = ImageUtils.getInstance().getLocalUrlFromUrl(baseView, type, images_url.get(i));
        if (StringUtils.isNotBlank(localUrlFromUrl)) {
            uriList.add(Uri.parse(localUrlFromUrl));
        }
    }
    //照片需要转成Uri格式
    //单张照片
    //sendIntent.putExtra(Intent.EXTRA_STREAM,uriList.get(0));
    //多张图片
    sendIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uriList);
    sendIntent.setType("image/*");
    sendIntent.setPackage("com.whatsapp");
    try {
    baseView.getContext().startActivity(sendIntent);     
    } catch (Exception e) {
        e.printStackTrace();
        LogException.unloadException(e.getMessage());
        if (shareListener != null) {
            shareListener.shareFailed(FFApplication.Companion.getInstance().getString(R.string
                    .share_whatsapp_no_install));
        }
    }`

您必须将图片从url下载到sdcard;发送从SD卡到whatsapp 的路径

您可以使用以下代码片段仅将图像共享到whatsapp;

Uri imageUri = Uri.parse("android.resource://com.v1.sensoft.halloween/mipmap"+imageRes);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
//Target whatsapp:
shareIntent.setPackage("com.whatsapp");
//Add Image URI
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/jpeg");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(shareIntent);

我明白你的意思。在您的情况下,首先您必须在SD卡中Asynctask存储的帮助下从Uri下载图像,并使用以下代码发送到该图像

当你从SD卡中选择路径时,它会自动检测图像扩展本身,无需担心

try {
                    Intent sendIntent = new Intent();
                    sendIntent.setAction(Intent.ACTION_SEND);
                    sendIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                    sendIntent.putExtra(Intent.EXTRA_TEXT, "your title");
                    sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("your image path"));
                    sendIntent.setType("image/*");
                    sendIntent.setPackage("com.whatsapp");
                    startActivity(sendIntent);
                } catch (Exception e) {
                    e.printStackTrace();
                }

相关内容

最新更新