无法使用contentResolver

Rob*_* El 2 android uri android-contentresolver

我几乎完成了我的应用......但现在我不能像这样使用ContentResolver:

Bitmap imageBitmap = MediaStore.Images.Media.getBitmap(
            getContentResolver(), imageUri);   // Problem!!!!!   
Run Code Online (Sandbox Code Playgroud)

我不知道如何解决这个问题...如果我在main-Activity中使用这种语法,它会起作用...但它在我的类CustomItemizedOverlay中不起作用.

我标记了我必须解决的问题......

eclipse说:对于CustomItemizedOverlay类型,未定义了方法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();
   }
Run Code Online (Sandbox Code Playgroud)

}

ale*_*ull 5

getContentResolver是一个属于Context的子类的方法,例如Activity.你的班级没有这样的方法,所以你不能使用它.最简单的修复方法是在新课程中存储对您的活动的引用:

Context context;
public CustomItemizedOverlay(Context myContext, Drawable defaultMarker) {
    super(boundCenterBottom(defaultMarker));
    context = myContext;
}
Run Code Online (Sandbox Code Playgroud)

然后context.getContentResolver(...)在需要时拨打电话.