getcontentresolver() 未为该类型定义

Posted

技术标签:

【中文标题】getcontentresolver() 未为该类型定义【英文标题】:getcontentresolver() is undefined for the type 【发布时间】:2015-05-26 12:26:15 【问题描述】:

您好,我对 java 很陌生,正在尝试创建一个应用程序来从我的手机内存中上传图片。我正在尝试将图像从我的画廊上传到我的应用程序,我收到错误“方法 getConstentResolver() 未定义上传类型”我该如何解决这个问题?

下面是我的代码...

public class Upload extends Fragment 

    private static final int SELECTED_PICTURE=1;
    ImageView iv;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) 

        View rootView = inflater.inflate(R.layout.upload, container, false);

        iv=(ImageView)rootView.findViewById(R.id.imageView1);

        return rootView;

    

    public void btnClick(View v)
        Intent i=new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(i, SELECTED_PICTURE);

    

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data)
        super.onActivityResult(requestCode,resultCode,data);
        switch(requestCode)
        case SELECTED_PICTURE:
            if(resultCode==RESULT_OK)
                Uri uri =data.getData();
                String[]projection=MediaStore.Images.Media.DATA;

                Cursor cursor=getConstentResolver().query(uri,projection,null,null,null);
                cursor.moveToFirst();
                int columnIndex=cursor.getColumnIndex(projection[0]);
                String filePath=cursor.getString(columnIndex);
                cursor.close();

                Bitmap yourSelectedImage=BitmapFactory.decodeFile(filePath);
                Drawable d=new BitmapDrawable(yourSelectedImage);
                iv.setBackground(d);


            
                    
    


    

【问题讨论】:

getConstentResolver() ,试试getActivity().getContentResolver(). 您好,请参阅下面我发布的代码,它工作正常。 【参考方案1】:

使用此代码:

public class EditProfileFragment extends Fragment 
private Button bt_browse;
private int REQUEST_CAMERA = 0, SELECT_FILE = 1;
private String uploadImagePath = "";

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) 

    View rootView = inflater.inflate(R.layout.edit_profile_fragment,
            container, false);
    bt_browse = (Button) rootView.findViewById(R.id.bt_browse);


    bt_browse.setOnClickListener(new OnClickListener() 

        @Override
        public void onClick(View v) 
            // TODO Auto-generated method stub
            selectImage();
        
    );



    return rootView;





private void selectImage() 
    final CharSequence[] items =  "Take Photo", "Choose from Library",
            "Cancel" ;

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setTitle("Select Photo!");
    builder.setItems(items, new DialogInterface.OnClickListener() 
        @Override
        public void onClick(DialogInterface dialog, int item) 
            if (items[item].equals("Take Photo")) 
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                File f = new File(android.os.Environment
                        .getExternalStorageDirectory(), "temp.jpg");
                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
                startActivityForResult(intent, REQUEST_CAMERA);
             else if (items[item].equals("Choose from Library")) 
                Intent intent = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                intent.setType("image/*");
                startActivityForResult(
                        Intent.createChooser(intent, "Select File"), SELECT_FILE);
             else if (items[item].equals("Cancel")) 
                dialog.dismiss();
            
        
    );
    builder.show();


public void onActivityResult(int requestCode, int resultCode, Intent data) 
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == Activity.RESULT_OK) 
        if (requestCode == REQUEST_CAMERA) 
            File f = new File(Environment.getExternalStorageDirectory()
                    .toString());
            for (File temp : f.listFiles()) 
                if (temp.getName().equals("temp.jpg")) 
                    f = temp;
                    break;
                
            
            try 
                Bitmap bm;
                BitmapFactory.Options btmapOptions = new BitmapFactory.Options();

                bm = BitmapFactory.decodeFile(f.getAbsolutePath(), btmapOptions);

                bm = Bitmap.createScaledBitmap(bm, 70, 70, true);
                iv_photo.setImageBitmap(bm);
                uploadImagePath = f.getAbsolutePath();

             catch (Exception e) 
                e.printStackTrace();
            
         else if (requestCode == SELECT_FILE) 
            Uri selectedImageUri = data.getData();

            String tempPath = getPath(selectedImageUri, getActivity());
            Bitmap bm;
            BitmapFactory.Options btmapOptions = new BitmapFactory.Options();
            bm = BitmapFactory.decodeFile(tempPath, btmapOptions);
            iv_photo.setImageBitmap(bm);
            uploadImagePath = tempPath;

        
    


@SuppressWarnings("deprecation")
public String getPath(Uri uri, Activity activity) 
    String[] projection =  MediaColumns.DATA ;
    Cursor cursor = activity.managedQuery(uri, projection, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);






您选择的图像存储在uploadImagePath 字符串中。 只需将其打印在 Log 中,然后您就明白了..

如果有任何进一步的帮助,请不要犹豫,问我..

【讨论】:

感谢您的回复,很抱歉很痛苦,但我收到错误 iv_photo 无法解决,我应该将 iv_photo 添加到我的 xml 文件中还是什么? 是的,如果您选择照片,那么照片将显示在该 ImageView 上。其实我很抱歉写了那个代码。 对不起,我只是不明白。如何摆脱 iv_photo 的错误? 首先在xml文件中声明一个ImageView。在此之后在私有按钮 bt_browse 之后声明这一行;该行是私有的 ImageView iv_photo;在此之后在 bt_browse = (Button) rootView.findViewById(R.id.bt_browse); 之后声明这一行该行是 iv_photo = (ImageView) rootView.findViewById(R.id.iv_photo); 我的课程还有一件事扩展了 Fragment,我希望你正在使用 Activity,所以请确保删除“rootView”和我正在使用的另一件事 onCreateView(--,--, --) 和确保你的函数是 onCreate(--,--)【参考方案2】:

您好,请参阅下面的代码,它工作正常。

package test.actionbariconupdate;

import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;


public class ActionBarIconUpdate extends Activity 

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_action_bar_icon_update2);
        if (savedInstanceState == null) 
            getFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
        
    


    @Override
    public boolean onOptionsItemSelected(MenuItem item) 
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) 
            return true;
        

        return super.onOptionsItemSelected(item);
    

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment 

        private Button buttonclick;
        private ImageView imageview;

        public PlaceholderFragment() 
            Log.d("TAG","PlaceholderFragment");
        


        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) 
            setHasOptionsMenu(true);
            View rootView = inflater.inflate(R.layout.fragment_action_bar_icon_update, container, false);
            imageview = (ImageView)rootView.findViewById(R.id.imageview);
            buttonclick = (Button)rootView.findViewById(R.id.buttonclick);
            buttonclick.setOnClickListener(new View.OnClickListener() 
                @Override
                public void onClick(View view) 
                    btnClick();
                
            );
            return rootView;
        

        @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data)
            super.onActivityResult(requestCode,resultCode,data);
            switch(requestCode)
                case 1:
                    if(resultCode==RESULT_OK)
                        Uri uri =data.getData();
                        String[]projection=MediaStore.Images.Media.DATA;

                        Cursor cursor= getActivity().getApplicationContext().getContentResolver().query(uri, projection, null, null, null);
                        cursor.moveToFirst();
                        int columnIndex=cursor.getColumnIndex(projection[0]);
                        String filePath=cursor.getString(columnIndex);
                        cursor.close();

                        Bitmap yourSelectedImage= BitmapFactory.decodeFile(filePath);
                        Drawable d=new BitmapDrawable(yourSelectedImage);
                        imageview.setBackground(d);
                    
            
        

        public void btnClick()
            Intent i=new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(i, 1);

        
    

【讨论】:

以上是关于getcontentresolver() 未为该类型定义的主要内容,如果未能解决你的问题,请参考以下文章

getcontentresolver() 在服务中返回 null

如何在 Android 中调用 getContentResolver()?

未为 InvoiceTemplateGet 类型定义方法 execute()

未为 API 侦听器调用 NSFetchedResultsController 委托

未为调试版本生成 Firebase 消息令牌

未为“对象”类型定义运算符“[]”[重复]