0

我有两个按钮和两个图像视图。当我点击第一个按钮时,它会打开并上传图片。如何点击第二个按钮并加载图片。我的代码如下

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


    }

现在我的问题是第二个按钮如何上传第二张图片。请帮我解决这个问题。提前致谢

4

2 回答 2

0
private static final int PICK_IMAGE = 1;
private static final int PICK_IMAGE_2 = 2;

设置点击监听器upload2。然后在OnClick()

startActivityForResult(Intent.createChooser(intent, "Select Picture"),
                             PICK_IMAGE_2);

最后,在onActivityResult()

case PICK_IMAGE :
imgView1.setImageBitmap(bitmap);
break;
CASE PICK_IMAGE :
imgView2.setImageBitmap(bitmap);
break;
于 2013-10-25T09:00:58.753 回答
0

您可以简单地重复您对第一个按钮所做的操作,但使用不同的请求代码,因此您知道如何使用 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);
}
于 2013-10-25T09:03:08.977 回答