onActivityResult什么也没做

时间:2019-02-21 07:24:06

标签: java android

我正在尝试创建一个图像选择器,用户将在其中选择一个图像,并将DSLContext create; Table table = new Table(/* Whatever Arguments */); TableUnmapper unmapper = new TableRecordUnmapper(); // Insert create.insertInto(TABLES).set(unmapper.unmap(table)); // update create.executeUpdate(unmapper.unmap(table)); 附加到ImageView上,并选择该图像作为其图像资源。这是我的代码,我不知道怎么了,但是没有linear layout附加到LinearLayout

ImageView
通过按钮调用

private void pickImage() { Intent intent = new Intent(Intent.ACTION_PICK); intent.setType("image/*"); startActivityForResult(intent, 1); } @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { if (resultCode != RESULT_OK) { return; } else if(resultCode == 1) { final Bundle extras = data.getExtras(); if (extras != null) { //IMAGE SELECTED Bitmap imageSelected = extras.getParcelable("data"); final View listingImage = getLayoutInflater().inflate(R.layout.image_listing, null); Button remove = (Button)listingImage.findViewById(R.id.removeImage); ImageView imageView = (ImageView)listingImage.findViewById(R.id.imageView); //SET IMAGE OF IMAGEVIEW TO SELECTED IMAGE imageView.setImageBitmap(imageSelected); remove.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { linearLayout.removeView(listingImage); } }); //ADD IMAGEVIEW TO LINEARLAYOUT linearLayout.addView(listingImage, 0); } } }

1 个答案:

答案 0 :(得分:0)

@Mikael王子,您无法进入onActivityResult,因为您输入的条件错误。这将是请求代码,而不是结果代码,因此您无法保存所选的图像。

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

        if (requestCode == 1 && resultCode == RESULT_OK){

            Uri contentURI = data.getData();
            try {
                Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), contentURI);
                String path = saveImage(bitmap);
                Toast.makeText(MainActivity.this, "Image Saved!", Toast.LENGTH_SHORT).show();
                listing_image.setImageBitmap(bitmap);
                ByteArrayOutputStream baos=new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.PNG,100, baos);
                byte [] b=baos.toByteArray();
                String temp= Base64.encodeToString(b, Base64.DEFAULT);

            } catch (IOException e) {
                e.printStackTrace();
                Toast.makeText(MainActivity.this, "Failed!", Toast.LENGTH_SHORT).show();
            }
        }
}

现在您可以将图像附加到线性布局。