问题
Following this tutorial to read image from assets folder.Here is the link Link to read image from Assets folder Contentclass extends from ContentProvider but i am getting error on first Line.This error is on very first line Contentclass1 Line.Please let me know that what things i need to implement in 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:
ContentProvider is an abstract class. That means that it has definitions for certain methods but does not provide an implementation for them. Therefore when you extend ContentProvider, you need to provide implementations of these methods in your class (even if you have no intention of calling them in your code). This is what the tutorial means when it says "implement stubs for the needed abstract methods" and also what your compile errors are referring to.
You can implement them by adding the following to your class:
@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;
}
These are referred to as 'stubs' because they aren't do any processing as such (other than returning a null/zero/false value).
来源:https://stackoverflow.com/questions/21378738/failed-to-read-image-file-from-assets-folder-and-share-it-using-intent-in-androi