如何在android中通过多个按钮上传多个图像

如何在android中通过多个按钮上传多个图像,android,android-layout,android-imageview,android-image,Android,Android Layout,Android Imageview,Android Image,我有两个按钮和两个imageview。当我点击第一个按钮时,它会打开并上传图片。第二个按钮点击并上传图片怎么办。我的代码如下 private static final int PICK_IMAGE = 1; upload1=(Button)findViewById(R.id.uploadimage1); upload2=(Button)findViewById(R.id.uploadimage2); imgView1=(ImageView)findViewById(R.id.image1); i

我有两个按钮和两个imageview。当我点击第一个按钮时,它会打开并上传图片。第二个按钮点击并上传图片怎么办。我的代码如下

private static final int PICK_IMAGE = 1;
upload1=(Button)findViewById(R.id.uploadimage1);
upload2=(Button)findViewById(R.id.uploadimage2);
imgView1=(ImageView)findViewById(R.id.image1);
imgView2=(ImageView)findViewById(R.id.image2);
upload1.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
             try {
                 Intent intent = new Intent();
                 intent.setType("image/*");
                 intent.setAction(Intent.ACTION_GET_CONTENT);
                 startActivityForResult(
                         Intent.createChooser(intent, "Select Picture"),
                         PICK_IMAGE);
             } catch (Exception e) {
                 Toast.makeText(getApplicationContext(),
                         "No image found", Toast.LENGTH_LONG).show();
                 Log.e(e.getClass().getName(), e.getMessage(), e);

        }
        }
    });

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
        case PICK_IMAGE:
            if (resultCode == Activity.RESULT_OK) {
                Uri selectedImageUri = data.getData();
                String filePath = null;

                try {
                    // OI FILE Manager
                    String filemanagerstring = selectedImageUri.getPath();

                    // MEDIA GALLERY
                    String selectedImagePath = getPath(selectedImageUri);

                    if (selectedImagePath != null) {
                        filePath = selectedImagePath;
                    } else if (filemanagerstring != null) {
                        filePath = filemanagerstring;
                    } else {
                        Toast.makeText(getApplicationContext(), "Unknown path",
                                Toast.LENGTH_LONG).show();
                        Log.e("Bitmap", "Unknown path");
                    }

                    if (filePath != null) {
                        decodeFile(filePath);
                    } else {
                        bitmap = null;
                    }
                } catch (Exception e) {
                    Toast.makeText(getApplicationContext(), "Internal error",
                            Toast.LENGTH_LONG).show();
                    Log.e(e.getClass().getName(), e.getMessage(), e);
                }
            }
            break;
        default:
        }
    }
    public String getPath(Uri uri) {
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        if (cursor != null) {
            // HERE YOU WILL GET A NULLPOINTER IF CURSOR IS NULL
            // THIS CAN BE, IF YOU USED OI FILE MANAGER FOR PICKING THE MEDIA
            int column_index = cursor
                    .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        } else
            return null;
    }
    public void decodeFile(String filePath) {
        // Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filePath, o);

        // The new size we want to scale to
        final int REQUIRED_SIZE = 1024;

        // Find the correct scale value. It should be the power of 2.
        int width_tmp = o.outWidth, height_tmp = o.outHeight;
        int scale = 1;
        while (true) {
            if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE)
                break;
            width_tmp /= 2;
            height_tmp /= 2;
            scale *= 2;
        }

        // Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        bitmap = BitmapFactory.decodeFile(filePath, o2);
                    //first image i have uploaded by using first button
        imgView1.setImageBitmap(bitmap);


    }
private static final int PICK_IMAGE=1;
upload1=(按钮)findViewById(R.id.uploadimage1);
upload2=(按钮)findViewById(R.id.uploadimage2);
imgView1=(ImageView)findViewById(R.id.image1);
imgView2=(ImageView)findViewById(R.id.image2);
upload1.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
试一试{
意图=新意图();
intent.setType(“image/*”);
intent.setAction(intent.ACTION\u GET\u CONTENT);
startActivityForResult(
Intent.createChooser(Intent,“选择图片”),
拾取图像);
}捕获(例外e){
Toast.makeText(getApplicationContext(),
“未找到图像”,Toast.LENGTH_LONG.show();
Log.e(e.getClass().getName(),e.getMessage(),e);
}
}
});
@凌驾
受保护的void onActivityResult(int请求代码、int结果代码、意图数据){
开关(请求代码){
案例选取图像:
if(resultCode==Activity.RESULT\u确定){
Uri selectedImageUri=data.getData();
字符串filePath=null;
试一试{
//文件管理器
字符串filemanagerstring=selectedImageUri.getPath();
//媒体画廊
字符串selectedImagePath=getPath(selectedImageUri);
如果(selectedImagePath!=null){
filePath=selectedImagePath;
}else if(filemanagerstring!=null){
filePath=filemanagerstring;
}否则{
Toast.makeText(getApplicationContext(),“未知路径”,
Toast.LENGTH_LONG).show();
Log.e(“位图”,“未知路径”);
}
if(filePath!=null){
解码文件(文件路径);
}否则{
位图=空;
}
}捕获(例外e){
Toast.makeText(getApplicationContext(),“内部错误”,
Toast.LENGTH_LONG).show();
Log.e(e.getClass().getName(),e.getMessage(),e);
}
}
打破
违约:
}
}
公共字符串getPath(Uri){
字符串[]投影={MediaStore.Images.Media.DATA};
Cursor Cursor=managedQuery(uri、投影、null、null、null);
如果(光标!=null){
//在这里,如果游标为NULL,您将得到一个NULL指针
//如果使用OI文件管理器拾取介质,则可能会出现这种情况
int column_index=游标
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
返回cursor.getString(列索引);
}否则
返回null;
}
公共文件(字符串文件路径){
//解码图像大小
BitmapFactory.Options o=新的BitmapFactory.Options();
o、 inJustDecodeBounds=true;
解码文件(文件路径,o);
//我们要扩展到的新尺寸
所需的最终int_SIZE=1024;
//找到正确的刻度值。它应该是2的幂。
内部宽度=o.向外宽度,高度=o.向外高度;
int标度=1;
while(true){
如果(宽度\u tmp<要求的\u尺寸和高度\u tmp<要求的\u尺寸)
打破
宽度_tmp/=2;
高度_tmp/=2;
比例*=2;
}
//用inSampleSize解码
BitmapFactory.Options o2=新的BitmapFactory.Options();
o2.inSampleSize=刻度;
位图=BitmapFactory.decodeFile(文件路径,o2);
//我用第一个按钮上传的第一张图片
imgView1.setImageBitmap(位图);
}
现在我的问题是第二个按钮如何上传第二张图片。请帮我解决这个问题。提前谢谢

private static final int PICK_IMAGE = 1;
private static final int PICK_IMAGE_2 = 2;
上传设置click listener 2
。然后在
OnClick()中

最后,在onActivityResult()中


您可以简单地重复对第一个按钮所做的操作,但使用不同的请求代码,这样您就知道如何使用onActivityResult中的数据:

public void onClick(View v) {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_2);
}
case PICK_IMAGE :
imgView1.setImageBitmap(bitmap);
break;
CASE PICK_IMAGE :
imgView2.setImageBitmap(bitmap);
break;
public void onClick(View v) {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_2);
}