本文介绍了使用Android的内容提供商从资产文件夹发送图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好〜我想通过一个普通的意图送从资产文件夹中的图像在应用程序中(这样用户可以将图像发送到接受这样的意图,包括短信,Facebook的任何其他应用程序,和Twitter)

Hello~ I'm trying to send an image from the assets folder in an application via a general intent (so the user can send an image to any other application which accepts such an intent, including SMS, Facebook, and Twitter).

东张西望了一会儿之后,我发现这个StackOverflow问题。它似乎工作得非常好,并遵循了一下意见之后,该方法似乎并不成为德precated。我的问题,但问题在于,我无法得到它的工作。什么发生的是,我成功地选择意向选择器对话的应用程序,但一旦应用程序时,我得到一个敬酒,说:无法找到照片。

After looking around for a while, I found this StackOverflow question. It seemed to work quite well, and after following the comments for a bit, the method doesn't seem to be deprecated. My issue, however, is that I can't get it to work. What occurs is that I successfully choose an application from the intent chooser dialogue, but once the application, I get a toast that says "Can't find photo."

下面是我目前的code ...

Here is my current code...

AndroidManifest.xml中:

AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mdstudios.diabeticons" >

<provider
    android:name="com.mdstudios.diabeticons.Utils.AssetsProvider"
    android:authorities="com.mdstudios.diabeticons"
    android:grantUriPermissions="true"
    android:exported="true" />

<application
...

AssetsProvider.java:

AssetsProvider.java:

package com.mdstudios.diabeticons.Utils;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.CancellationSignal;
import android.util.Log;

import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * Created by jawad on 06/04/15.
 */
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;
  }

  @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;
  }
}

SendActivity.java:

SendActivity.java:

// Sends an intent to share the image that was passed to this Activity
private void sendImage() {
    Log.d(LOGTAG, mFilePath);
    Uri theUri = Uri.parse("content://com.mdstudios.diabeticons/" + mFilePath);
    // Tried file path with subfolder and non-subfolder images

    Intent theIntent = new Intent(Intent.ACTION_SEND);
    theIntent.setType("image/*");
    theIntent.putExtra(Intent.EXTRA_STREAM,theUri);
    theIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Subject for message");
    theIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Body for message");
    startActivity(theIntent);
}

我不知道我在做什么错。我尝试发送的图像是在子文件夹(即文件夹/ image1.png)。不过,我使用所有子文件夹以外的图像试过(即简单的image1.png复制后的图像的文件路径是任何资产的子文件夹外)。此外,在原来如此问题的意见,似乎我不得不使用不同的方式来获得URI中的文件路径。无论是原来的,也不是新的方法(在AssetsProvider类的评论中指出的)工作。

I'm not sure what I'm doing wrong. The images I'm trying to send are in subfolders (ie, "folder/image1.png"). However, I've tried using an image outside of any subfolders (ie, simply "image1.png" for the file path after copying the image to be outside of any asset subfolders). Furthermore, in the comments in the original SO question, it seemed I had to use a different way to get the file path from the Uri. Neither the original nor the new method (pointed out in the comments in the AssetsProvider class) worked.

感谢您的任何和所有帮助!

Thank you for any and all help!

编辑:
看着周围一些后,我发现这个问题的一个类似的帖子在这里,没有回复。然后我看了看我的更深入logcat中,发现了一个错误:

After looking around some more, I found a similar posting of the issue here with no replies. I then looked at my logcat more in depth, and found an error:

04-06 14:30:54.773  18883-19532/? E/Babel﹕ java.io.FileNotFoundException: No content provider: content://com.mdstudios.diabeticons/Banana.png
        at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1060)
        at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:914)
        at android.content.ContentResolver.openInputStream(ContentResolver.java:639)
        at ajz.a(SourceFile:5280)
        at ajz.doInBackgroundTimed(SourceFile:5066)
        at dsn.doInBackground(SourceFile:65)
        at android.os.AsyncTask$2.call(AsyncTask.java:288)
        at java.util.concurrent.FutureTask.run(FutureTask.java:237)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
        at java.lang.Thread.run(Thread.java:841)

我仍然不知道如何解决这个问题(虽然我仍然继续鼓捣出来),所以任何帮助仍然是AP preciated!谢谢!

I'm still not sure how to fix this (although I'm still continuing to tinker with it), so any help would still be appreciated! Thanks!

推荐答案

感谢CommonsWare,我在这里认识的问题。问题出在哪里提供商应该是专门manifest-中声明,该应用程序标记之间。

Thanks to CommonsWare, I realized the issue here. The problem is where the provider is supposed to be declared within the manifest- specifically, between the application tags.

因此​​,清单应该是这样的......

So, the manifest should look like this...

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mdstudios.diabeticons" >

    <application>
        <provider
            android:name="com.mdstudios.diabeticons.Utils.AssetsProvider"
            android:authorities="com.mdstudios.diabeticons"
            android:grantUriPermissions="true"
            android:exported="true" />
    </application>

</manifest>

请注意,在code我张贴的问题中,提供者是明显的标记之间尚未应用标签外(类似于在权限声明)。相反,供应商声明必须是应用程序标签之间,就像在那里活动的声明。

Notice that in the code I posted within the question, the provider was between the manifest tags yet outside the application tags (similar to where permissions are declared). Instead, the provider declaration must be between the application tags, just like where Activities are declared.

这篇关于使用Android的内容提供商从资产文件夹发送图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-03 07:23
查看更多