1

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

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

4 回答 4

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));

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

于 2013-02-20T11:28:39.393 回答
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));
    }
    }
    }
于 2013-12-19T17:41:22.333 回答
0

**把这个放在行动监听器中,从哪里选择图像**

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();
            }

        }
    }
于 2019-06-12T18:51:13.733 回答
-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;
}

 }
于 2013-02-20T11:30:38.687 回答