我无法在ImagevView中看到我的图片

时间:2019-02-07 17:06:20

标签: android android-intent

我没有得到ImageView中的图片(可点击的图片)。 单击了这些图像,但我无法在ImageView中显示它们。

MainActivity:

public class MainActivity extends AppCompatActivity {

    Button btn,btn1;
    ImageView iv;
    Bitmap bmp;

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

        btn= (Button) findViewById(R.id.button1);
        btn1= (Button) findViewById(R.id.button2);
        iv= (ImageView) findViewById(R.id.imageview);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

                startActivityForResult(intent,101);
            }
        });


        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_SEND);
                intent.setType("image/jpeg");
                intent.putExtra(Intent.EXTRA_STREAM, bmp);
                startActivity(Intent.createChooser(intent, "Share picture with..."));

            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Uri picturePath=data.getData();
        iv.setImageURI(picturePath);
        try {
            Bundle b=data.getExtras();
            bmp=(Bitmap) b.get("Data");
            iv.setImageBitmap(bmp);
        } catch (Exception  e) {
            Toast.makeText(this, "try Again", Toast.LENGTH_SHORT).show();
        }

        super.onActivityResult(requestCode, resultCode, data);
    }
}

1 个答案:

答案 0 :(得分:0)

intent中缺少Intent权限“ FLAG_GRANT_READ_URI_PERMISSION”。请尝试在Intent中使用addFlags行添加权限。

例如:

final Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/jpeg");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.putExtra(Intent.EXTRA_STREAM, bmp);
startActivity(Intent.createChooser(intent, "Share picture with..."));