如何从android中的画廊中选择图像?

时间:2013-02-20 11:19:58

标签: android

我遇到了一个问题:我有一个功能可以从画廊中获取图像;但是当我选择图像时,我没有得到它。

public void funzione(View v){
int SELECT_IMAGE=1;
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"),SELECT_IMAGE);

我必须通过电子邮件发送此图片,但我现在不知道如何实现这个:

Intent i = new Intent();
i.setType("application/octet-stream"); 
i.setAction(Intent.ACTION_SEND);
i.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{ "prova@libero.it" });
i.putExtra(android.content.Intent.EXTRA_SUBJECT, "Test subj");
i.putExtra(android.content.Intent.EXTRA_TEXT, "corpo mail test");
startActivity(Intent.createChooser(i, "Send email"));

}

4 个答案:

答案 0 :(得分:12)

在您的顶级类声明之后声明全局变量:

 private static int RESULT_LOAD_IMAGE = 1;
 private static final int PICK_FROM_GALLERY = 2;
 Bitmap thumbnail = null; 

像这样调用意图:(你的funizone()函数)

   public void funzione(){
       Intent in = new   Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                      startActivityForResult(in, RESULT_LOAD_IMAGE);

   }

像这样处理结果:在课堂的任何地方将你的onCreate声明。

 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();
            thumbnail = (BitmapFactory.decodeFile(picturePath));

缩略图是你的照片,现在玩吧!

答案 1 :(得分:3)

package com.example.assignment;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

private static int RESULT_LOAD_IMAGE = 1;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture);
    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.imgView);
        imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
    }
    }
    }

答案 2 :(得分:0)

**将其放置在Action Listener中,从中选择图片**

Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(i, 100);

第二个动作侦听器

Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    startActivityForResult(i, 200);

从画廊归还后,结果获得通过请求代码**这里的代码分别为100和200 **

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

        if (requestCode == 100 && resultCode == RESULT_OK && data != null){
            Uri imageUri=data.getData();
            try {
                Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
                reg_certificate.setImageBitmap(bitmap);

            } catch (IOException e) {
                e.printStackTrace();
            }

        }

        if(requestCode == 200 && resultCode == RESULT_OK && data !=null){
            Uri imageUri=data.getData();
            try {
                Bitmap bitmap= MediaStore.Images.Media.getBitmap(this.getContentResolver(),imageUri);
                org_logo.setImageBitmap(bitmap);

            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

答案 3 :(得分:-6)

使用以下代码在应用中添加图片。

public class MainActivity extends Activity {

@Override
         public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            ImageView image = (ImageView) findViewById(R.id.test_image);
            image.setImageResource(R.drawable.test2);
        }
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    /* Inflate the menu; this adds items to the action bar if it is present.*/
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

 }