无法读取从Assets文件夹图片文件,并在Android中使用意向共享呢? [英] Failed to Read Image file from Assets folder and share it using Intent in Android?

查看:309
本文介绍了无法读取从Assets文件夹图片文件,并在Android中使用意向共享呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面这个教程从资产读取图像folder.Here是链接的链接到阅读Assets文件夹图像
内容类ContentProvider的距离延伸,但我得到的第一个错误Line.This错误是在第一行Contentclass1 Line.Please让我知道我需要Contentclass1.java实现什么事情

 在该行多个标记
     - 类型Contentclass1必须实现继承的抽象方法ContentProvider.onCreate()
     - 类型Contentclass1必须实现继承的抽象方法ContentProvider.delete(URI,字符串,字符串[])
     - 类型Contentclass1必须实现继承的抽象方法ContentProvider.query(URI,字符串[],字符串,字符串[],
     串)
     - 类型Contentclass1必须实现继承的抽象方法ContentProvider.getType(URI)
     - 类型Contentclass1必须实现继承的抽象方法ContentProvider.update(URI,ContentValues​​,弦乐,
     串[])
     - 类型Contentclass1必须实现继承的抽象方法ContentProvider.insert(URI,ContentValues​​)

Contentclass1.java

 包com.example.shareima;
进口java.io.FileNotFoundException;
进口java.io.IOException异常;
进口android.content.ContentProvider;
进口android.content.res.AssetFileDescriptor;
进口android.content.res.AssetManager;
进口android.net.Uri;公共类Contentclass1扩展的ContentProvider
{
    @覆盖
    公共AssetFileDescriptor openAssetFile(URI URI,字符串模式)抛出FileNotFoundException异常{
        AssetManager AM =的getContext()getAssets()。
        串FILE_NAME = uri.getLastPathSegment();
        如果(FILE_NAME == NULL)
            抛出新的FileNotFoundException异常();
        AssetFileDescriptor AFD = NULL;
        尝试{
            AFD = am.openFd(FILE_NAME);
        }赶上(IOException异常五){
            e.printStackTrace();
        }
        返回AFD; // super.openAssetFile(URI,模式);
    }
}

的Manifest.xml

 <?XML版本=1.0编码=UTF-8&GT?;
<清单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    包=com.example.shareima
    安卓版code =1
    机器人:=的versionName1.0>
    <用途-SDK
        安卓的minSdkVersion =12
        机器人:targetSdkVersion =17/>
 <使用许可权的android:NAME =android.permission.INTERNET对/>
 <使用许可权的android:NAME =android.permission.READ_EXTERNAL_STORAG​​E/>
 <应用
        机器人:allowBackup =真
        机器人:图标=@绘制/ ic_launcher
        机器人:标签=@字符串/ APP_NAME
        机器人:主题=@风格/ AppTheme>
        <活动
            机器人:名字=com.example.shareima.MainActivity
            机器人:标签=@字符串/ APP_NAME>
            &所述;意图滤光器>
                <作用机器人:名字=android.intent.action.MAIN/>
                <类机器人:名字=android.intent.category.LAUNCHER/>
            &所述; /意图滤光器>
        < /活性GT;
         <供应商的android:NAME =。Contentclass1
            机器人:当局=com.example.shareima/>
 < /用途>
 < /清单>


解决方案

ContentProvider的是一个抽象类。这意味着它具有用于某些方法定义,但它们并没有提供一种实现。因此,当你扩展的ContentProvider,你需要提供这些方法的实现在你的类(即使你没有叫他们在你的code的意图)。这就是本教程意味着当它说实施所需的抽象方法存根,也有你的编译错误都指的是什么。

您可以通过添加实现它们的下面到类:

  @覆盖
公众诠释删除(URI为arg0,ARG1字符串,字符串[] ARG2){
    返回0;
}@覆盖
公共字符串的getType(URI为arg0){
    返回null;
}@覆盖
公共乌里插入(URI为arg0,ARG1 ContentValues​​){
    返回null;
}@覆盖
公共布尔的onCreate(){
    返回false;
}@覆盖
公共光标查询(URI为arg0,字符串[] ARG1,ARG2字符串,字符串[] ARG3,弦乐ARG4){
    返回null;
}@覆盖
公众诠释更新(URI为arg0,ARG1 ContentValues​​,ARG2字符串,字符串[] ARG3){
    返回0;
}

这些被称为存根,因为它们不执行任何处理本身(其他比返回一个空/零/假值)。

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>

解决方案

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

这篇关于无法读取从Assets文件夹图片文件,并在Android中使用意向共享呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆