如何从图库中选择图片?

时间:2013-04-01 11:42:04

标签: android

我试图在模拟器中从图库中选择图片。当我点击浏览日食在logcat中显示W/IInputConnectionWrapper: showStatusIcon on inactive InputConnection但模拟器将我带到画廊。当我选择一张图片时,它没有被选中。我使用下面的代码:

package com.textapi;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class SendMMSActivity extends Activity {
    private static final int SELECT_PICTURE = 1;
    private String selectedPath, extension = "";
    Uri selectedVideoUri;
    Button btnBrowse;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.send_mms);
        btnBrowse=(Button)findViewById(R.id.bnBrowse);
        btnBrowse.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(
                        Intent.createChooser(intent,"Select Picture"),
                        SELECT_PICTURE);
            }
        });
    }

    public void onActivityResult(int requestCode, int resultCode,
            final Intent data) {
        if (resultCode == RESULT_OK) {
            if (requestCode == SELECT_PICTURE) {
                selectedVideoUri = data.getData();
                selectedPath = getPath(selectedVideoUri);
                System.out.println("Path--->>"+selectedPath);
                int pos = selectedPath.lastIndexOf(".");
                if (pos > 0) {
                    extension = selectedPath.substring(pos + 1);
                }
            }
        }
    }

    // We can get the path of the video using this method.
    public String getPath(Uri uri) {
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }


}

请帮我选择图片。而且我还需要使用MVAAYOO api将图片从图库发送到手机号码。如果有人知道,请帮助我。

3 个答案:

答案 0 :(得分:4)

public class MainActivity extends Activity {
    private static int RESULT_LOAD_IMAGE = 1;
    Uri myPicture = null;
    Button buttonLoadImage;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //getting View 
         buttonLoadImage = (Button) findViewById(R.id.button2);

        buttonLoadImage.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

                Intent i = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

                startActivityForResult(i, RESULT_LOAD_IMAGE);
            }
        });
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };

            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();

            ImageView imageView = (ImageView) findViewById(R.id.imageView1);
            imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));    
        }
    }
}

答案 1 :(得分:0)

  @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
          Intent i = new Intent(
                Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(i, RESULT_LOAD_IMAGE);
        }
    });
}
   // For Image Loading
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK
            && null != data) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };

        Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        picturePath = cursor.getString(columnIndex);
        cursor.close();

        ImageView imageView = (ImageView) findViewById(R.id.imageView1);
        imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

    }

}

答案 2 :(得分:0)

btnBrowse.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            getfrmGallery()
        }
    });


File fp;
Drawable d;   

public void  getfrmGallery()
{
// To open up a gallery browser
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"),1);
// To handle when an image is selected from the browser, add the following to your Activity 
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {// if image selected
if (requestCode == 1) {
// currImageURI is the global variable I�m using to hold the content:// URI of the image
Uri currImageURI = data.getData(); 
System.out.println("Hello======="+getRealPathFromURI(currImageURI));
String s= getRealPathFromURI(currImageURI);//get the image path
File file = new File(s); 

if (file.exists()) { // if the file exists in the path
 fp=file.getAbsolutePath(); //get the absolute path of the image
 d = Drawable.createFromPath(file.getAbsolutePath());//create a drawable form the path
 imageView.setBackgroundDrawable(d);//set imageview with the selected image
 }
else
{
System.out.println("File Not Found"); // image not found
}
}
}