1

我想从我的画廊拍一张照片,我想给它一个“id”来使用我的 android 应用程序。

4

2 回答 2

1

甚至在按钮单击时,调用以下代码....

Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, GET_IMAGE);

并且,在活动结果上,编写以下代码:

if (requestCode == GET_IMAGE) {    
  targetUri = data.getData();
  imageView.setImageURI(targetUri);                 
}

在哪里,private static final int GET_IMAGE = 2;

于 2012-07-23T09:15:56.537 回答
0

你可以试试下面的代码

    btnChoosePhoto.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent i = new Intent(Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);

            startActivityForResult(i, 1);
        }
    });


public void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);

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

        final Uri selectedImage = data.getData();

        try {
            bitmap = Media.getBitmap(getContentResolver(),selectedImage);
            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 90, bytes);

            //path = selectedImage.getEncodedPath();

            // create new file to write the encrypted bytes of image in file
            File f = new File(Environment.getExternalStorageDirectory()
                    + File.separator
                    + filename);
            f.createNewFile();
            FileOutputStream fo = new FileOutputStream(f);
            fo.write(bytes.toByteArray());

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        imageView.setImageBitmap(bitmap);

    }
}
于 2012-07-23T09:19:34.957 回答