【问题标题】:Problems sharing an image from assets in Android从 Android 中的资产共享图像的问题
【发布时间】:2015-07-30 17:16:06
【问题描述】:

有一些关于如何从 Android 的资产文件夹发送和共享图像(或文件)的文档和支持。

基本上,我得到了这些链接的支持:

  1. android share images from assets folder
  2. http://www.nowherenearithaca.com/2012/03/too-easy-using-contentprovider-to-send.html

还有很多类似的。

但无论如何,我使用的代码是:

内容提供者

public class AssetsProvider extends ContentProvider {
private static final String LOGTAG = "MD/AssetsProvider";

@Override
public AssetFileDescriptor openAssetFile( Uri uri, String mode ) throws FileNotFoundException
{
    Log.v(LOGTAG, "AssetsGetter: Open asset file");

    AssetManager am = getContext( ).getAssets( );

    String file_name = uri.getPath().substring(1, uri.getPath().length());
    //String file_name = uri.getLastPathSegment();
    // Neither of the two lines above work for me

    if( file_name == null )
        throw new FileNotFoundException( );

    AssetFileDescriptor afd = null;

    try
    {
        afd = am.openFd( file_name );
    }
    catch(IOException e)
    {
        e.printStackTrace( );
    }

    return afd;//super.openAssetFile(uri, mode);
}

@Override
public String getType( Uri p1 )
{
    // TODO: Implement this method
    return null;
}

@Override
public int delete( Uri p1, String p2, String[] p3 )
{
    // TODO: Implement this method
    return 0;
}

@Override
public Cursor query( Uri p1, String[] p2, String p3, String[] p4, String p5 )
{
    // TODO: Implement this method
    return null;
}

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public Cursor query( Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder, CancellationSignal cancellationSignal )
{
    // TODO: Implement this method
    return super.query( uri, projection, selection, selectionArgs, sortOrder, cancellationSignal );
}

@Override
public Uri insert( Uri p1, ContentValues p2 )
{
    // TODO: Implement this method
    return null;
}

@Override
public boolean onCreate( )
{
    // TODO: Implement this method
    return false;
}

@Override
public int update( Uri p1, ContentValues p2, String p3, String[] p4 )
{
    // TODO: Implement this method
    return 0;
}
}

我不太喜欢,因为它需要 API 16。

ANDROID MANIFEST(在<application> 标签内)

<provider
    android:name="package.name.utils.AssetsProvider"
    android:authorities="package.name"
    android:grantUriPermissions="true"
    android:exported="true" />

分享图片的代码

Uri theUri = Uri.parse("content://package.name/share_picture.png");
Intent theIntent = new Intent(Intent.ACTION_SEND);
theIntent.setType("image/*");
theIntent.putExtra(Intent.EXTRA_STREAM,theUri);
theIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"");
theIntent.putExtra(android.content.Intent.EXTRA_TEXT, "");
startActivity(theIntent);

我可以成功看到我的选择器。

我的主要问题是

我可以在某些应用程序中分享我的照片,但不能在其他应用程序中分享。例如: 我可以分享我的照片的应用程序:

  • Google 环聊

  • 推特

  • WhatsApp

  • Skype(卡在“发送”中)

  • Instagram

  • Dropbox

我无法分享照片的应用:

  • Gmail 收件箱

  • Gmail

  • 任何图像编辑应用

  • 消息

  • 松弛

  • 领英

  • 列表项

  • Hipchat

  • Messenger(由 Facebook 提供)

  • 脸书

我无法分享的应用有什么问题吗?还是我在代码上做错了什么?也许是setType 函数?

提前谢谢你。

问候。

拉斐尔。


从文件共享图像时也会出现问题,因此与资产无关的是问题:

今天早上我做了这个测试:

    Intent shareIntent = new Intent(Intent.ACTION_SEND);
    Uri phototUri = Uri.parse(folderToSaveFiles+relativeNameSharePicture);

    shareIntent.setData(phototUri);
    shareIntent.setType("image/png");
    shareIntent.putExtra(Intent.EXTRA_STREAM, phototUri);
    startActivity(Intent.createChooser(shareIntent, "Share Via"));

同样的事情发生了!!

【问题讨论】:

  • 尝试设置theIntent.setType("*/*");。无论如何,我会从路径转换为 ​​Android 友好的 Parcelable URIs
  • 不起作用,我用那个崩溃了每个外部应用程序:D
  • 我建议这样做是因为它对我有用。
  • 也许是文本或二进制文件?我很自豪我制作了崩溃 GMAIL 呵呵呵呵

标签: android android-sharing


【解决方案1】:

最简单的答案是使用my StreamProvider,它会为您处理所有这些。如果这不适用于您引用的某些应用程序,请提交带有症状的问题(例如,来自这些应用程序的堆栈跟踪)。但是,我没有将 AssetFileDescriptor 用于资产(适用于兼容设备),这在我的待办事项列表中进行更改。

下一个最简单的答案是将资产复制到内部存储和use FileProvider,以及my LegacyCompatCursorWrapper,如this sample app所示。

如果您想坚持使用您的代码:

第 1 步:实现一个真正的 getType() 方法,以返回 MIME 类型。

步骤#2:实现一个真正的query() 方法,一个处理OpenableColumns 的方法,理想情况下使用我的LegacyCompatCursorWrapper 或其他deals with poorly-written clients

第 3 步:将Add FLAG_GRANT_READ_URI_PERMISSION 更改为您的Intent 并将android:exported 更改为false

第 4 步:删除 EXTRA_TEXT,因为 ACTION_SEND 协议支持任一 EXTRA_TEXT EXTRA_STREAM,不能同时支持。

【讨论】:

  • 更新了问题。似乎也发生在一个简单的图像上。
【解决方案2】:

您是否尝试过在 getType() 方法中返回不同于 null 的内容? 某些应用程序可能会检查它,如果未识别,它们将不接受您发送的内容。 (你可以很容易地在那里记录一些东西,至少看看其他应用程序是否在调用它)

【讨论】:

    【解决方案3】:

    主要问题是 Uri 必须从 FileUri.fromFile(File file) 解析然后一切正常!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-09
      • 1970-01-01
      • 2014-06-15
      相关资源
      最近更新 更多