在我的Android应用程序中,我有活动,当用户点击按钮时,它通过使用意图打开相机,拍摄照片,在同一活动中将其显示为图像视图,之后还有另一个按钮,用于通过Whatsapp发送该照片。 **我想我没有正确保存文件。
请看看我正在保存文件并给文件命名的部分,在用whatsapp按钮发送之后,打开whatsapp但没有附加图像,whatsapp给出错误。**
这是我的活动代码。
public class Main2Activity extends AppCompatActivity {
ImageView iv;
File imagesFolder;
File image;
Uri uriSavedImage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
iv = (ImageView) findViewById(R.id.imageView3);
Button btnCapture = (Button) findViewById(R.id.button_camera);
Button send = (Button) findViewById(R.id.send_whatsapp);
//Set listener on Capture button
btnCapture.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent c = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); //Implicit Intent
startActivityForResult(c, 0);
imagesFolder = new File(Environment.getExternalStorageDirectory(), "MyImages");
imagesFolder.mkdirs(); // <----
image = new File(imagesFolder, "image_001.jpg");
String fileName = image.toString();
Log.e("log for file name", "hello");
uriSavedImage = Uri.fromFile(image);
c.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
}
});
}
//Override method onActivityResult used to retreive the image
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Bitmap m = (Bitmap) data.getExtras().get("data");
iv.setImageBitmap(m);
iv.setScaleType(ImageView.ScaleType.FIT_XY);
}
public void sendSelfie(View view) {
Intent whatsappIntent = new Intent(android.content.Intent.ACTION_SEND);
whatsappIntent.setType("image/jpg");
whatsappIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(image));
startActivity(Intent.createChooser(whatsappIntent, "Share image using"));
}
}
我的XML代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.android.happybirthdayprathmesh.Main2Activity">
<Button
android:id="@+id/button_camera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:onClick="clickSelfie"
android:text="Click here to take photo" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="50dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="60dp">
<ImageView
android:id="@+id/imageView3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true" />
</RelativeLayout>
<Button
android:id="@+id/send_whatsapp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:onClick="sendSelfie"
android:text="Send by Whatsapp" />
</RelativeLayout>
答案 0 :(得分:0)
我认为这个Link可以帮助您更好地理解如何使用Intent将图像发送到whatsupp。
我希望你做得好
答案 1 :(得分:0)
试试这个:
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/jpeg");
intent.setPackage(“com.whatsapp");
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+ imagePath));
答案 2 :(得分:0)
下面的代码将提供一种将图像和文本分享到应用程序和其他类似应用程序的通用方法。
public void shareShop(Activity activity, String imagePath, String url) {
Intent whatsappIntent = new Intent(android.content.Intent.ACTION_SEND);
whatsappIntent.putExtra(Intent.EXTRA_TEXT, url);
if (imagePath != null) {
whatsappIntent.setType("image/*");
whatsappIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + imagePath));
}//add image path
else {
whatsappIntent.setType("text/*");
}
activity.startActivity(Intent.createChooser(whatsappIntent, "Share image using"));
try {
activity.startActivity(whatsappIntent);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(activity, "Whatsapp have not been installed.", Toast.LENGTH_SHORT).show();
}
}
答案 3 :(得分:0)
String imagePath = "file://" + image.getAbsolutePath();
Uri.parse(imagePath)
答案 4 :(得分:0)
我用这个:
whatsapp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
File f=new File(muxedvdo);
Uri uri = Uri.parse("file://"+f.getAbsolutePath());
Intent share = new Intent(Intent.ACTION_SEND);
share.setPackage("com.whatsapp");
share.putExtra(Intent.EXTRA_STREAM, uri);
share.setType("image/jpeg");
share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(share, "Share imageFile"));
}
});