android从资源文件夹共享图像

时间:2011-05-04 19:18:35

标签: android image share assets

我正在尝试从我的资源文件夹中共享图像。我的代码是:

Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpg");
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///assets/myImage.jpg"));
startActivity(Intent.createChooser(share, "Share This Image"));

但它不起作用。你有什么想法吗?

7 个答案:

答案 0 :(得分:15)

可以通过自定义ContentProvider

从资源文件夹共享文件(包括图像)

您需要扩展ContentProvider,在清单中注册并实施openAssetFile方法。然后,您可以通过Uri s

评估资产
    @Override
    public AssetFileDescriptor openAssetFile(Uri uri, String mode) throws FileNotFoundException {
        AssetManager am = getContext().getAssets();
        String file_name = uri.getLastPathSegment();

        if(file_name == null) 
            throw new FileNotFoundException();
        AssetFileDescriptor afd = null;
        try {
            afd = am.openFd(file_name);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return afd;
    }

答案 1 :(得分:11)

补充@intrepidis回答的内容:

您将需要覆盖方法,例如上面的示例类:

package com.android.example;

import android.content.ContentProvider;
import android.net.Uri;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import java.io.FileNotFoundException;
import android.content.ContentValues;
import android.database.Cursor;
import java.io.IOException;
import android.os.CancellationSignal;

public class AssetsProvider extends ContentProvider
{

        @Override
        public AssetFileDescriptor openAssetFile( Uri uri, String mode ) throws FileNotFoundException
        {
                Log.v( TAG, "AssetsGetter: Open asset file" );
                AssetManager am = getContext( ).getAssets( );
                String file_name = uri.getLastPathSegment( );
                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;
        }
}

我需要覆盖两次查询方法。 并在androidmanifest.xml中的标记上方添加以下行:

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

有了这个,所有的工作就像一个魅力:D

答案 2 :(得分:10)

这篇博客解释了一切:
http://nowherenearithaca.blogspot.co.uk/2012/03/too-easy-using-contentprovider-to-send.html

基本上,这是明显的:

<provider android:name="yourclass.that.extendsContentProvider"                android:authorities="com.yourdomain.whatever"/>

内容提供程序类具有:

@Override
public AssetFileDescriptor openAssetFile(Uri uri, String mode) throws FileNotFoundException {
    AssetManager am = getContext().getAssets();
    String file_name = uri.getLastPathSegment();
    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);
}

调用代码执行此操作:

Uri theUri = Uri.parse("content://com.yourdomain.whatever/someFileInAssetsFolder");
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);

答案 3 :(得分:3)

许多应用要求您提供图像的名称和大小。这是一个改进的代码(以Google的FileProvider代码为例):

public class AssetsProvider extends ContentProvider {

    private final static String LOG_TAG = AssetsProvider.class.getName();

    private static final String[] COLUMNS = {
            OpenableColumns.DISPLAY_NAME, OpenableColumns.SIZE };

    @Override
    public boolean onCreate() {
        return true;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        /**
         * Source: {@link FileProvider#query(Uri, String[], String, String[], String)} .
         */
        if (projection == null) {
            projection = COLUMNS;
        }

        final AssetManager am = getContext().getAssets();
        final String path = getRelativePath(uri);
        long fileSize = 0;
        try {
            final AssetFileDescriptor afd = am.openFd(path);
            fileSize = afd.getLength();
            afd.close();
        } catch(IOException e) {
            Log.e(LOG_TAG, "Can't open asset file", e);
        }

        final String[] cols = new String[projection.length];
        final Object[] values = new Object[projection.length];
        int i = 0;
        for (String col : projection) {
            if (OpenableColumns.DISPLAY_NAME.equals(col)) {
                cols[i] = OpenableColumns.DISPLAY_NAME;
                values[i++] = uri.getLastPathSegment();
            } else if (OpenableColumns.SIZE.equals(col)) {
                cols[i] = OpenableColumns.SIZE;
                values[i++] = fileSize;
            }
        }

        final MatrixCursor cursor = new MatrixCursor(cols, 1);
        cursor.addRow(values);
        return cursor;
    }

    @Override
    public String getType(Uri uri) {
        /**
         * Source: {@link FileProvider#getType(Uri)} .
         */
        final String file_name = uri.getLastPathSegment();
        final int lastDot = file_name.lastIndexOf('.');
        if (lastDot >= 0) {
            final String extension = file_name.substring(lastDot + 1);
            final String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
            if (mime != null) {
                return mime;
            }
        }

        return "application/octet-stream";
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        return null;
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        return 0;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        return 0;
    }

    @Override
    public AssetFileDescriptor openAssetFile(Uri uri, String mode) throws FileNotFoundException {
        final AssetManager am = getContext().getAssets();
        final String path = getRelativePath(uri);
        if(path == null) {
            throw new FileNotFoundException();
        }
        AssetFileDescriptor afd = null;
        try {
            afd = am.openFd(path);
        } catch(IOException e) {
            Log.e(LOG_TAG, "Can't open asset file", e);
        }
        return afd;
    }

    private String getRelativePath(Uri uri) {
        String path = uri.getPath();
        if (path.charAt(0) == '/') {
            path = path.substring(1);
        }
        return path;
    }
}

答案 4 :(得分:2)

AFAIK,无法与assets文件夹共享图像。但是可以从res文件夹共享资源。

答案 5 :(得分:2)

要从资源文件夹共享,我只能推荐cwac-provider库(StreamProvider)。

在避免开发自己的内容提供商时,它会为反复无常的旧版应用添加一些支持(请检查USE_LEGACY_CURSOR_WRAPPER)。

答案 6 :(得分:0)

因为这里没有其他答案对我有用(在2019年),所以我通过将资产复制到应用程序的内部文件目录然后共享此文件来进行解决。 就我而言,我需要从资产文件夹共享一个pdf文件。

在AndroidManifest.xml中添加一个文件提供程序(无需使用自定义文件):

RealThread

在res / xml /

中创建filepaths.xml文件
<provider
    android:name="androidx.core.content.FileProvider"
    android:authorities="${applicationId}.provider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/filepaths" />
</provider>

如果要管理应用目录中的其他文件,当然应该在这里使用子目录。

现在在您要触发共享意图的班级中。

1。在文件目录中创建一个空文件

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <files-path
        name="root"
        path="/" />
</paths>

2。将资产的内容复制到文件中

private fun createFileInFilesDir(filename: String): File {
    val file = File(filesDir.path + "/" + filename)
    if (file.exists()) {
        if (!file.delete()) {
            throw IOException()
        }
    }
    if (!file.createNewFile()) {
        throw IOException()
    }
    return file
}

3。为文件创建共享意向

private fun copyAssetToFile(assetName: String, file: File) {
    val buffer = ByteArray(1024)
    val inputStream = assets.open(assetName)
    val outputStream: OutputStream = FileOutputStream(file)
    while (inputStream.read(buffer) > 0) {
        outputStream.write(buffer)
    }
}

4。执行1-3并激发意图

private fun createIntentForFile(file: File, intentAction: String): Intent {
    val uri = FileProvider.getUriForFile(this, applicationContext.packageName + ".provider", file)
    val intent = Intent(intentAction)
    intent.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
    intent.setDataAndType(uri, "application/pdf")
    return intent
}

5。调用函数

private fun sharePdfAsset(assetName: String, intentAction: String) {
    try {
        val file = createFileInFilesDir(assetName)
        copyAssetToFile(assetName, file)
        val intent = createIntentForFile(file, intentAction)
        startActivity(Intent.createChooser(intent, null))
    } catch (e: IOException) {
        e.printStackTrace()
        AlertDialog.Builder(this)
            .setTitle(R.string.error)
            .setMessage(R.string.share_error)
            .show()
    }
}

如果要在共享后删除文件,则可以使用sharePdfAsset("your_pdf_asset.pdf", Intent.ACTION_SEND) 并随后将其删除。通过更改startActivityForResult(),您还可以使用intentAction将此过程用于“用...打开”操作。 对于Intent.ACTION_VIEWassets,...您当然需要处于filesDir或拥有Activity的位置。