简体   繁体   English

在Android中共享图库中的图像

[英]Share images from gallery in Android

i have written a program which has two buttons , one is for gallery which when clicked displays my images from gallery and another button which starts my camera which displays the captured images. 我编写了一个程序,该程序有两个按钮,一个用于画廊,单击该按钮可显示画廊中的图像,另一个按钮可启动我的相机以显示捕获的图像。

The images are getting displayed properly , but I am not able to understand how to share the displayed images to other apps , please need some help. 图像可以正确显示,但是我无法理解如何将显示的图像共享给其他应用,请寻求帮助。

Below here is my code for sharing : I have put question mark because i need to know what i should pass inside my Uri so that i can get the path of my images 下面是我的共享代码:我已打了问号,因为我需要知道我应该在Uri中传递的内容,以便获取图像的路径。

imgShare.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Intent intent = new Intent(Intent.ACTION_SEND);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
            intent.setType("image/*");
            Uri selectedImage = ????
            intent.putExtra(Intent.EXTRA_STREAM, selectedImage);
            startActivityForResult(
                    Intent.createChooser(intent, "Select Picture"),
                    SELECT_PICTURE);


        }

My code for startActivityForResult 我的代码startActivityForResult

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub

    if (requestCode == SELECT_PICTURE && resultCode == RESULT_OK
            && null != data) {

        // Toast.makeText(getBaseContext(),
        // "File saved at " + imageFile.getAbsolutePath(),
        // Toast.LENGTH_SHORT).show();
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };

        Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        /*
         * Bitmap bitmap = BitmapFactory .decodeFile( Environment
         * .getExternalStoragePublicDirectory
         * (Environment.DIRECTORY_PICTURES) + File.separator + ".jpg",
         * options);
         */
        Bitmap bitmap = BitmapFactory.decodeFile(picturePath);
        int newWidth = bitmap.getWidth();
        int newHeight = bitmap.getHeight();

        while (newWidth > 1200) {
            newWidth = newWidth / 2;
            newHeight = newHeight / 2;
        }
        img.setImageBitmap(Bitmap.createScaledBitmap(bitmap, newWidth,
                newHeight, false));

    }

any suggestions are welcomed , thanking you 任何建议都欢迎,谢谢

These are called intents, some application are listening for contents like images and plain text. 这些称为意图,某些应用程序正在侦听图像和纯文本之类的内容。 when you call these intent, a dialog will open of those applications are listening for your content. 当您调用这些意图时,将打开一个对话框,显示正在侦听您的内容的那些应用程序。

Share single Image 分享单张图片

Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage);
shareIntent.setType("image/jpeg");
startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.send_to)));

Share multiple Image 分享多张图片

ArrayList<Uri> imageUris = new ArrayList<Uri>();
imageUris.add(imageUri1); // Add your image URIs here
imageUris.add(imageUri2);

Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "Share images to.."));

Get ImageURI 获取ImageURI

public Uri getImageUri(Context inContext, Bitmap inImage) {
  ByteArrayOutputStream bytes = new ByteArrayOutputStream();
  inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
  String path = Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
  return Uri.parse(path);
}

OR 要么

Uri.parse(new File("/sdcard/yourImage.jpg").toString())

More you can here on Android official website 您可以在Android官方网站上找到更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM