无法从Assets文件夹中读取图像文件并在Android中使用Intent共享它?

时间:2014-01-27 10:44:47

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

按照本教程从资源文件夹中读取图像。这是链接Link to read image from Assets folder Contentclass从ContentProvider扩展但是我在第一行上遇到错误。这个错误出现在第一行Contentclass1 Line.Please让我知道我需要在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>

1 个答案:

答案 0 :(得分:3)

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

这些被称为“存根”,因为它们不会进行任何处理(除了返回null / zero / false值)。