在大多数情况下,使用应用程序时,我们希望将图像从该应用程序共享到另一个应用程序。在使用许多社交媒体平台时,当我们希望将信息从一个应用程序共享到另一个应用程序时,我们发现此功能非常有用。在将数据发送到定义良好的任务流的一部分到另一个应用程序时,将使用Android Intent解析器。要使用Android意向解析器,请创建一个意向并添加其他功能。在这里,我们将了解如何做到这一点。
Note: If you want to share the Text of Your App with Another App in Android then please refer to this.
分步实施
步骤1:创建一个新项目
要在Android Studio中创建新项目,请参阅如何在Android Studio中创建/启动新项目。请注意,选择Java作为编程语言。
步骤2:使用AndroidManifest.xml文件
将以下权限添加到AndroidManifest.xml文件
在
XML
android:authorities="com.anni.shareimage.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
XML
XML
XML
Java
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.FileProvider;
import java.io.File;
import java.io.FileOutputStream;
public class MainActivity extends AppCompatActivity {
Button share;
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
share = findViewById(R.id.share);
imageView = findViewById(R.id.shareimage);
// initialising text field where we will enter data
share.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Now share image function will be called
// here we will be passing the text to share
// Getting drawable value from image
BitmapDrawable bitmapDrawable = (BitmapDrawable) imageView.getDrawable();
Bitmap bitmap = bitmapDrawable.getBitmap();
shareImageandText(bitmap);
}
});
}
private void shareImageandText(Bitmap bitmap) {
Uri uri = getmageToShare(bitmap);
Intent intent = new Intent(Intent.ACTION_SEND);
// putting uri of image to be shared
intent.putExtra(Intent.EXTRA_STREAM, uri);
// adding text to share
intent.putExtra(Intent.EXTRA_TEXT, "Sharing Image");
// Add subject Here
intent.putExtra(Intent.EXTRA_SUBJECT, "Subject Here");
// setting type to image
intent.setType("image/png");
// calling startactivity() to share
startActivity(Intent.createChooser(intent, "Share Via"));
}
// Retrieving the url to share
private Uri getmageToShare(Bitmap bitmap) {
File imagefolder = new File(getCacheDir(), "images");
Uri uri = null;
try {
imagefolder.mkdirs();
File file = new File(imagefolder, "shared_image.png");
FileOutputStream outputStream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 90, outputStream);
outputStream.flush();
outputStream.close();
uri = FileProvider.getUriForFile(this, "com.anni.shareimage.fileprovider", file);
} catch (Exception e) {
Toast.makeText(this, "" + e.getMessage(), Toast.LENGTH_LONG).show();
}
return uri;
}
}
以下是AndroidManifest.xml文件的完整代码
XML格式
步骤3:建立新的XML档案
创建一个 res中的XML目录,然后在其中创建一个path.xml文件。文件提供者只能为我们预先指定的目录中的文件生成内容URI。我们使用
XML格式
步骤4:使用activity_main.xml文件
转到activity_main.xml文件,并参考以下代码。以下是activity_main.xml文件的代码。
Reference article: How to Add Image to Drawable Folder in Android Studio?
XML格式
步骤5:使用MainActivity。 Java文件
转到MainActivity。 Java文件并参考以下代码。下面是MainActivity的代码。 Java文件。在代码内部添加了注释,以更详细地了解代码。
Java
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.FileProvider;
import java.io.File;
import java.io.FileOutputStream;
public class MainActivity extends AppCompatActivity {
Button share;
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
share = findViewById(R.id.share);
imageView = findViewById(R.id.shareimage);
// initialising text field where we will enter data
share.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Now share image function will be called
// here we will be passing the text to share
// Getting drawable value from image
BitmapDrawable bitmapDrawable = (BitmapDrawable) imageView.getDrawable();
Bitmap bitmap = bitmapDrawable.getBitmap();
shareImageandText(bitmap);
}
});
}
private void shareImageandText(Bitmap bitmap) {
Uri uri = getmageToShare(bitmap);
Intent intent = new Intent(Intent.ACTION_SEND);
// putting uri of image to be shared
intent.putExtra(Intent.EXTRA_STREAM, uri);
// adding text to share
intent.putExtra(Intent.EXTRA_TEXT, "Sharing Image");
// Add subject Here
intent.putExtra(Intent.EXTRA_SUBJECT, "Subject Here");
// setting type to image
intent.setType("image/png");
// calling startactivity() to share
startActivity(Intent.createChooser(intent, "Share Via"));
}
// Retrieving the url to share
private Uri getmageToShare(Bitmap bitmap) {
File imagefolder = new File(getCacheDir(), "images");
Uri uri = null;
try {
imagefolder.mkdirs();
File file = new File(imagefolder, "shared_image.png");
FileOutputStream outputStream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 90, outputStream);
outputStream.flush();
outputStream.close();
uri = FileProvider.getUriForFile(this, "com.anni.shareimage.fileprovider", file);
} catch (Exception e) {
Toast.makeText(this, "" + e.getMessage(), Toast.LENGTH_LONG).show();
}
return uri;
}
}
输出: