Java 如何从多媒体资料中拍照并在我的应用程序中使用此照片

Java 如何从多媒体资料中拍照并在我的应用程序中使用此照片,java,android,eclipse,Java,Android,Eclipse,我想从我的图库中拍摄一张照片,我想给它一个“id”来使用我的android应用程序。在按钮上单击“偶数”,调用以下代码 Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(intent, GET_IMAGE); 在“活动结果”上,编写以下代码: if (requestCode ==

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

在按钮上单击“偶数”,调用以下代码

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

在按钮上单击偶数,调用以下代码

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

您可以尝试下面的代码

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

    }
}
你可以试试下面的代码

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

    }
}

除了你的手机,这个应用程序还能在其他手机上运行吗?有很多类似的帖子。。。这个应用程序的第一个可能的副本是否应该在除您之外的任何手机上运行?有很多类似的帖子。。。谢谢你的回答。但我不会将此图片用作图像视图。我想用这个图片改变另一个班级的背景。我能做什么?谢谢你的回答。但我不会将此图片用作图像视图。我想用这个图片改变另一个班级的背景。我能做什么?