无法从资产文件夹读取图像文件,并在Android中使用意图共享



遵循本教程以读取资产文件夹的图像。在这里,链接链接到资产文件夹读取图像的链接ContentClass从Content -Provider延伸,但我在第一行中遇到错误。此错误在很第一行ContentClass1行上。请让我知道我需要在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);
    }
}

subest.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>

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值)。

相关内容

最新更新