Reputation: 213
I am implementing one app to share images to whatsapp when clicking on "share button" and according to this project i created one Imageadapter that extend Baseadapter and one activity for sharing images.So,When i am clicking on "Share button" it should generate "The file formate is not supported".
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();
}
});
}
}
If anyone know what is the problem plz tell me. Thanks in advance...
Upvotes: 0
Views: 10992
Reputation: 11
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));
}
}`
you must download image fro url to the sdcard ;send the path from sdcard to whatsapp
Upvotes: 1
Reputation: 1001
Try to append .jpg to the image file name. The fileName should have a valid image name extension like .jpg or .jpeg.
In your onClick try implementing the below code:
Intent intent = new Intent(mContext,FullScreenImage.class);
intent.putExtra("imageID", mThumbIds[position]+".jpg");
mContext.startActivity(intent);
Upvotes: 3
Reputation: 729
I got your point. In your case first you have to download image from Uri with the help of Asynctask store inside in sdcard and send to that image using following code
When you pick path from sdcard it will automatically detect image extension itself no need to worry about that
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();
}
Upvotes: 0
Reputation: 334
You can use the below code snippet to share image to whatsapp only;
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);
Upvotes: 0