【问题标题】:Failed to Read Image file from Assets folder and share it using Intent in Android?无法从 Assets 文件夹中读取图像文件并在 Android 中使用 Intent 共享它?
【发布时间】:2014-02-18 03:59:32
【问题描述】:

按照本教程从资产文件夹中读取图像。这是链接Link to read image from Assets folder Contentclass 从 ContentProvider 扩展,但我在第一行出现错误。此错误出现在 Contentclass1 Line 的第一行。请让我知道我需要在 Contentclass1.java 中实现什么

Multiple markers at this line
    - The type Contentclass1 must implement the inherited abstract method ContentProvider.onCreate()
    - The type Contentclass1 must implement the inherited abstract method ContentProvider.delete(Uri, String, String[])
    - The type Contentclass1 must implement the inherited abstract method ContentProvider.query(Uri, String[], String, String[], 
     String)
    - The type Contentclass1 must implement the inherited abstract method ContentProvider.getType(Uri)
    - The type Contentclass1 must implement the inherited abstract method ContentProvider.update(Uri, ContentValues, String, 
     String[])
    - The type Contentclass1 must implement the inherited abstract method ContentProvider.insert(Uri, ContentValues)

Contentclass1.java

package com.example.shareima;
import java.io.FileNotFoundException;
import java.io.IOException;
import android.content.ContentProvider;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.net.Uri;

public class Contentclass1 extends ContentProvider
{
    @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);
    }
}

Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.shareima"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="12"
        android:targetSdkVersion="17" />
 <uses-permission android:name="android.permission.INTERNET"/>
 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
 <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.shareima.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
         <provider android:name=".Contentclass1"               
            android:authorities="com.example.shareima"/>
 </application>
 </manifest>

【问题讨论】:

    标签: java android android-intent android-contentprovider android-assets


    【解决方案1】:

    ContentProvider 是一个抽象类。这意味着它具有某些方法的定义,但没有为它们提供实现。因此,当您扩展 ContentProvider 时,您需要在您的类中提供这些方法的实现(即使您无意在代码中调用它们)。这就是教程所说的“为所需的抽象方法实现存根”的意思,也是您的编译错误所指的意思。

    您可以通过将以下内容添加到您的类来实现它们:

    @Override
    public int delete(Uri arg0, String arg1, String[] arg2) {
        return 0;
    }
    
    @Override
    public String getType(Uri arg0) {
        return null;
    }
    
    @Override
    public Uri insert(Uri arg0, ContentValues arg1) {
        return null;
    }
    
    @Override
    public boolean onCreate() {
        return false;
    }
    
    @Override
    public Cursor query(Uri arg0, String[] arg1, String arg2, String[] arg3, String arg4) {
        return null;
    }
    
    @Override
    public int update(Uri arg0, ContentValues arg1, String arg2, String[] arg3) {
        return 0;
    }
    

    这些被称为“存根”,因为它们本身不做任何处理(除了返回空/零/假值)。

    【讨论】:

    • 教程 Uri theUri = Uri.parse("content://com.yourdomain.whatever/someFileInAssetsFolder");
    • 无论教程中有“com.yourdomain.whatever”,请将其更改为您的包名称,我认为在您的情况下为“com.example.shareima”。然后将“someFileInAssetsFolder”替换为您要共享的文件的名称(您已添加到项目中的资产)。所以像:Uri.parse("content://com.example.shareima/myfile.txt")
    • 你能告诉我本教程中的问题是什么,它只能通过 Gmail 分享图片,而无法在 FB、蓝牙等中分享
    • 在 Intent 上设置选择器:Intent.createChooser(theIntent, "...share to");这应该允许用户从所有能够处理共享文件的已安装应用程序中进行选择。
    • 是的,我也做了同样的事情,它也打开了所有应用程序,但如果是蓝牙,当它从 gmail 应用程序成功发送时,我会收到消息“文件未发送”和“FB”中的类似消息跨度>
    猜你喜欢
    • 2013-02-21
    • 1970-01-01
    • 2016-04-01
    • 2013-06-09
    • 2015-04-18
    • 1970-01-01
    • 2021-01-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多