简体   繁体   English

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

[英]Failed to Read Image file from Assets folder and share it using Intent in Android?

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 按照本教程从资源文件夹中读取图像。这是链接从Assets文件夹读取图像的链接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 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 的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>

ContentProvider is an abstract class. ContentProvider是一个抽象类。 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). 因此,当您扩展ContentProvider时,您需要在类中提供这些方法的实现(即使您无意在代码中调用它们)。 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). 这些被称为“存根”,因为它们不进行任何处理(除了返回null / zero / false值)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM