gpt4 book ai didi

java - 在应用程序上使用 Intent 共享图像时出现错误共享失败

转载 作者:行者123 更新时间:2023-12-02 02:17:56 25 4
gpt4 key购买 nike

我正在从可绘制文件夹加载单个 jpg 图像,在 list 中设置文件提供程序权限,但是当我在 Whatsapp 上共享图像时,出现共享失败的错误,请重试。当我共享文本时,它工作正常,但当我尝试共享图像时,它给出了我的错误。这是以下文件

Fullscreenadapter.java

package com.mobdev.birthdaycakesquotes;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.support.v4.app.ShareCompat;
import android.support.v4.content.FileProvider;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;



public class Fullscreenadapter extends PagerAdapter {
private Integer[] Image;
private int _resource;
private Activity _activity;
private LayoutInflater inflater;
private String images;
TextView t1,t2;
public Fullscreenadapter(Activity activity,
Integer[] image) {
this._activity = activity;
this.Image=image;
}

@Override
public int getCount() {
return this.Image.length;
}

@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((RelativeLayout) object);
}
@Override
public Object instantiateItem(ViewGroup container, final int position) {
final ImageView imgDisplay,shareimage;


inflater = (LayoutInflater) _activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View viewLayout = inflater.inflate(R.layout.fullscreenlayoutdesign,
container,
false);

imgDisplay = (ImageView) viewLayout.findViewById(R.id.show);
shareimage=(ImageView)viewLayout.findViewById(R.id.share);
t1=(TextView)viewLayout.findViewById(R.id.currentposition);
t2=(TextView)viewLayout.findViewById(R.id.totalimage);


imgDisplay.setImageResource(Image[position]);
t1.setText(String.valueOf(position));
t2.setText(String.valueOf(Image.length));
images=createImageOnSDCard(R.drawable.sw);

shareimage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

Uri path = FileProvider.getUriForFile(_activity,
"com.mobdev.birthdaycakesquotes",new File(images));


Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, "This is one image I'm
sharing.");
shareIntent.putExtra(Intent.EXTRA_STREAM, path);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
shareIntent.setType("image/*");
_activity.startActivity(Intent.createChooser(shareIntent,
"Share..."));


}
});
((ViewPager) container).addView(viewLayout);
return viewLayout;
}
private String createImageOnSDCard(int resID) {
Bitmap bitmap = BitmapFactory.decodeResource(Resources.getSystem(), resID);
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/" + resID + ".jpg";
File file = new File(path);
try {
OutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}

return file.getPath();
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((RelativeLayout) object);
}
}

AndroidManifest文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mobdev.birthdaycakesquotes">

<application
android:allowBackup="true"
android:hardwareAccelerated="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".HomePage"
android:label="@string/title_activity_home_page"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" />
<activity android:name=".Details_activity"></activity>

<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.mobdev.birthdaycakesquotes"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
</application>

file_paths.xml

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

最佳答案

private void shareImage(Uri imagePath) {
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
sharingIntent.setType("image/*");
sharingIntent.putExtra(Intent.EXTRA_STREAM, imagePath);
//sharingIntent.setPackage("com.whatsapp"); for whatsapp only
startActivity(Intent.createChooser(sharingIntent, "Share Image Using"));// for all generic options
}

最后在你的 list 中添加这个,一定要成功

        <activity
android:name=".YourActivity"
android:icon="@drawable/share_this"
android:label="@string/shared_activity" >
<intent-filter>
<action android:name="android.intent.action.SEND" /> <!-- Send
action required to display activity in share list -->
<category android:name="android.intent.category.DEFAULT" /> <!--
Make activity default to launch -->
<!-- Mime type i.e. what can be shared with this activity only image and text -->
<data android:mimeType="image/*" />
<data android:mimeType="text/*" />
</intent-filter>
</activity>

这会起作用

关于java - 在应用程序上使用 Intent 共享图像时出现错误共享失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49011212/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com