android 共享图像目标-不支持图像格式

tyky79it  于 2022-11-27  发布在  Android
关注(0)|答案(3)|浏览(196)

我在Activity中显示图像,并希望通过Intent共享该图像。出现错误:不支持图像格式,无论是jpg还是png。
也许我的代码有错误:

public class StartActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);

Intent myIntent = getIntent();
int image = myIntent.getExtras().getInt("image");

ImageView imagefull = (ImageView)findViewById(R.id.imageView1);
imagefull.setImageResource(image);

Button share = (Button) findViewById (R.id.buttonshare);
share.setOnClickListener(new OnClickListener() 

{
    @Override
    public void onClick(View arg0) {

    Intent share = new Intent(Intent.ACTION_SEND);

    share.setType("image/*");

    Uri uri = Uri.fromFile(new File(getFilesDir(), "facebook.png"));
    share.putExtra(Intent.EXTRA_STREAM, uri);
    startActivity(Intent.createChooser(share, "Share Image!"));

    } });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
66bbxpm5

66bbxpm51#

对于PNG,请尝试share.setType("image/png");;对于JPEG,请尝试share.setType("image/jpg");

2admgd59

2admgd592#

试试这个:-

shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
shareIntent.setType("image/*");

//set your message
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, msgText); 
String imagePath = Environment.getExternalStorageDirectory() + File.separator + "image_name.jpg";
File imageFileToShare = new File(imagePath);
Uri uri = Uri.fromFile(imageFileToShare);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
Uri uri = Uri.fromFile(imageFileToShare);
sharingIntent.putExtra(Intent.EXTRA_STREAM, uri);
f8rj6qna

f8rj6qna3#

我尝试过几种方法,但是没有一种方法对我有效,而且操作的部分也不清楚,所以这里是我用来共享图像或视频类型内容的方法,以防有数据的绝对路径。这也适用于png和jpg。
在androidmanifest.xml中添加以下行:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<application
//Other codes
<provider
        android:name="androidx.core.content.FileProvider"
        android:authorities="com.example.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths" />
</provider>
//Other codes
</application>

在资源目录res中,创建一个名为xml的新文件夹。将一个新文件放入其中,该文件的名称与您在manifest.xml元数据部分使用的名称相同,在本例中为provider_paths.xml
android:资源="@xml/提供者路径”
将以下内容放入其中:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path
        name="external_files"
        path="." />
    <root-path
        name="external_files"
        path="/storage/"/>
</paths>

在您希望使用share函数的活动中,放置以下代码,其中path是包含内容绝对路径的字符串变量,而“com.example.fileprovider”是基于上面创建的新xml文件的其中一行的Fileprovider的author值,如下所示:
文件提供者”

File file = new File(path);
//Checking if the file exists
if(file.exists()) {
    //Creating the Uri for the file using the provider we just created
    Uri contentUri = 
    FileProvider.getUriForFile(Gallery.this,"com.example.fileprovider", file);
    //Creating the share intent
    Intent S = new Intent(Intent.ACTION_SEND);
    //Allowing different types to be shared
    S.setType("*/*");                                        
    S.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    //Providing the uri to the intent
    S.putExtra(Intent.EXTRA_STREAM, contentUri);
    //Starting the intent, can choose from apps which are listening for the 
    //content type
    startActivity(Intent.createChooser(S, "Share content using"));
}
else{                                  
    Toast.makeText(getApplicationContext(),path + " does not 
    exist",Toast.LENGTH_SHORT).show();
}

通过这个可以很容易地共享设备上的内容和路径。权限和资源值在manifest.xml中是至关重要的。当然可以更改它们,但要确保每次都要修改它们。

资源

Android Share Intent Image Sharing not working Except WhatsApp
https://www.geeksforgeeks.org/how-to-share-image-from-url-with-intent-in-android/
https://developer.android.com/training/sharing/send

相关问题