Android 无法使用contentResolver

Android 无法使用contentResolver,android,uri,android-contentresolver,Android,Uri,Android Contentresolver,我几乎完成了我的申请。。。但是现在我不能像这样使用ContentResolver: Bitmap imageBitmap = MediaStore.Images.Media.getBitmap( getContentResolver(), imageUri); // Problem!!!!! 我不知道如何解决这个问题…如果我在我的主要活动中使用这个语法,它会起作用。。。 但是它在我的类CustomItemizedOverlay中不起作用 我标记了我要修的线 e

我几乎完成了我的申请。。。但是现在我不能像这样使用ContentResolver:

Bitmap imageBitmap = MediaStore.Images.Media.getBitmap(
            getContentResolver(), imageUri);   // Problem!!!!!   
我不知道如何解决这个问题…如果我在我的主要活动中使用这个语法,它会起作用。。。 但是它在我的类CustomItemizedOverlay中不起作用

我标记了我要修的线

eclipse说:对于CustomItemizeOverlay类型,methode getContentResolver()是未定义的

import java.util.ArrayList;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.net.Uri;

import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;
import android.app.Dialog;
import android.app.AlertDialog;
import android.widget.TextView;
import android.view.View;
import android.widget.ImageView;
import android.view.LayoutInflater;
import android.widget.Toast;
import android.view.ViewGroup;
import android.provider.MediaStore; 
import android.provider.MediaStore.Images;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.app.Application;
import android.content.ContentResolver;
import android.content.Context;




public class CustomItemizedOverlay extends ItemizedOverlay<OverlayItem> {

   private ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();
   private String title;
   private String id_string;
   private String type;
   private String name;
   private String adress;
   private String webpage;
   private String opening_hours;
   private Context context;
   private Uri imageUri;

   public CustomItemizedOverlay(Drawable defaultMarker) {
        super(boundCenterBottom(defaultMarker));
   }

   public CustomItemizedOverlay(Drawable defaultMarker, Context context,String title,String id_string,String type,String name,String adress,String webpage,String opening_hours,Uri imageUri) {
        this(defaultMarker);
        this.context = context;
        this.title = title;
        this.id_string = id_string;
        this.type = type;
        this.name =name;
        this.adress = adress;
        this.webpage=webpage;
        this.opening_hours = opening_hours;
        this.imageUri = imageUri;

   }

   @Override
   protected OverlayItem createItem(int i) {
      return mapOverlays.get(i);
   }

   @Override
   public int size() {
      return mapOverlays.size();
   }

   @Override
   protected boolean onTap(int index) {
      OverlayItem item = mapOverlays.get(index);
      AlertDialog.Builder dialog = new AlertDialog.Builder(context);

      dialog.setTitle(title);
      View layout = View.inflate(context, R.layout.dialog, null);

      TextView id_text = (TextView)layout.findViewById(R.id.id);
      id_text.setText("id:"+id_string);
      TextView type_text = (TextView)layout.findViewById(R.id.type);
      type_text.setText("type:"+type);
      TextView name_text = (TextView)layout.findViewById(R.id.name);
      name_text.setText("name:"+name);
      TextView adress_text = (TextView)layout.findViewById(R.id.adress);
      adress_text.setText("adress:"+adress);
      TextView webpage_text = (TextView)layout.findViewById(R.id.webpage);
      webpage_text.setText("webpage:"+webpage);
      TextView opening_hours_text = (TextView)layout.findViewById(R.id.opening_hours);
      opening_hours_text.setText("opening_hours:"+opening_hours);


      ContentResolver con = new Contentresolver();

      ImageView imageView = (ImageView) layout.findViewById(R.id.photo);
      Bitmap imageBitmap = MediaStore.Images.Media.getBitmap(
                getContentResolver(), imageUri);   // Problem! HERE!!!!
      imageView.setImageBitmap(imageBitmap);


      dialog.setView(layout);
      dialog.show();
      return true;
   }

   public void addOverlay(OverlayItem overlay) {
      mapOverlays.add(overlay);
       this.populate();
   }
import java.util.ArrayList;
导入android.app.AlertDialog;
导入android.content.Context;
导入android.graphics.drawable.drawable;
导入android.net.Uri;
导入com.google.android.maps.ItemizedOverlay;
导入com.google.android.maps.OverlayItem;
导入android.app.Dialog;
导入android.app.AlertDialog;
导入android.widget.TextView;
导入android.view.view;
导入android.widget.ImageView;
导入android.view.LayoutInflater;
导入android.widget.Toast;
导入android.view.ViewGroup;
导入android.provider.MediaStore;
导入android.provider.MediaStore.Images;
导入android.graphics.Bitmap;
导入android.graphics.BitmapFactory;
导入android.app.Application;
导入android.content.ContentResolver;
导入android.content.Context;
公共类CustomItemizeOverlay扩展了ItemizeOverlay{
私有ArrayList mapOverlays=新ArrayList();
私有字符串标题;
私有字符串id_字符串;
私有字符串类型;
私有字符串名称;
私有字符串地址;
私有字符串网页;
私人字符串开放时间;
私人语境;
私有Uri-imageUri;
公共自定义项目化Overlay(可绘制的defaultMarker){
super(boundCenterBottom(defaultMarker));
}
public CustomItemizeOverlay(可绘制的默认标记、上下文上下文、字符串标题、字符串id\u字符串、字符串类型、字符串名称、字符串地址、字符串网页、字符串打开时间、Uri imageUri){
这(默认标记);
this.context=上下文;
this.title=标题;
this.id\u string=id\u string;
this.type=type;
this.name=名称;
this.address=地址;
this.webpage=网页;
此项。营业时间=营业时间;
this.imageUri=imageUri;
}
@凌驾
受保护的OverlayItem createItem(int i){
返回mapOverlays.get(i);
}
@凌驾
公共整数大小(){
返回mapOverlays.size();
}
@凌驾
受保护的布尔onTap(整数索引){
OverlayItem item=mapOverlays.get(索引);
AlertDialog.Builder dialog=新建AlertDialog.Builder(上下文);
对话框。设置标题(标题);
视图布局=视图.充气(上下文,R.layout.dialog,null);
TextView id_text=(TextView)layout.findviewbyd(R.id.id);
id_text.setText(“id:+id_字符串”);
TextView type_text=(TextView)layout.findViewById(R.id.type);
键入_text.setText(“type:+type”);
TextView name_text=(TextView)layout.findviewbyd(R.id.name);
name_text.setText(“name:+name”);
TextView adress_text=(TextView)layout.findViewById(R.id.adress);
Address_text.setText(“Address:+Address”);
TextView网页\ u text=(TextView)布局。findViewById(R.id.webpage);
webpage_text.setText(“网页:+网页”);
TextView营业时间text=(TextView)layout.findViewById(R.id.opening\u hours);
营业时间text.setText(“营业时间:+营业时间”);
ContentResolver con=新的ContentResolver();
ImageView=(ImageView)layout.findViewById(R.id.photo);
位图imageBitmap=MediaStore.Images.Media.getBitmap(
getContentResolver(),imageUri);//问题!这里!!!!
设置图像位图(图像位图);
对话框.setView(布局);
dialog.show();
返回true;
}
公共void addOverlay(OverlayItem overlay){
mapOverlays.add(叠加);
这个。填充();
}

}

getContentResolver
是属于上下文子类(如活动)的方法。您的类没有这样的方法,因此无法使用它。最简单的修复方法是在新类中存储对活动的引用:

Context context;
public CustomItemizedOverlay(Context myContext, Drawable defaultMarker) {
    super(boundCenterBottom(defaultMarker));
    context = myContext;
}
然后在需要时调用
context.getContentResolver(…)

如果我的类扩展到fragment,getActivity().getContentResolver()会工作吗?