android - Questions about the method intent.putExtra(MediaStore.EXTRA_OUTPUT, uri)
过去多啦不再A梦
过去多啦不再A梦 2017-05-16 13:32:33
0
2
1106

Why is the function of this method to specify the output address of taking pictures? The prototype of this method is

public Intent putExtra(String name, Parcelable value)

Doesn’t this mean to store data in the form of key-value pairs? Why can you specify the output address of the image?
The following is the complete code. The main purpose is to click the Button to enter the photo-taking interface, and then save the photos taken to the application-associated directory. However, during the learning process, I did not understand how to save the pictures to the uri specified by me. .

public class MainActivity extends AppCompatActivity
{

    public static final int TAKE_PHOTO = 1;
    private ImageView picture;
    private Uri imageUri;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button takePhoto= (Button) findViewById(R.id.take_photo);
        picture= (ImageView) findViewById(R.id.pircture);
        takePhoto.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                //创建File对象,用于存储拍照后的图片
                File outputImage = new File(getExternalCacheDir(),"output_image.jpg");
                if(outputImage.exists())
                {
                    outputImage.delete();
                    try
                    {
                        outputImage.createNewFile();
                    }
                    catch (IOException e)
                    {
                        e.printStackTrace();
                    }
                }

                if (Build.VERSION.SDK_INT>=24)
                {
                    imageUri = FileProvider.getUriForFile(MainActivity.this,"com.studio.cameraalbumtest.fileprovider",outputImage);
                }
                else
                {
                    imageUri = Uri.fromFile(outputImage);
                }

                //启动相机程序
                Intent intent=new Intent("android.media.action.IMAGE_CAPTURE");
                intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);
                startActivityForResult(intent,TAKE_PHOTO);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        switch (requestCode)
        {
            case TAKE_PHOTO:
            {
                if(resultCode == RESULT_OK)
                {
                    //将拍摄的照片显示出来
                    try
                    {
                        Bitmap bitmap= BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));
                        picture.setImageBitmap(bitmap);
                    }
                    catch (FileNotFoundException e)
                    {
                        e.printStackTrace();
                    }
                }
            }
            break;
        }
    }
}
过去多啦不再A梦
过去多啦不再A梦

reply all(2)
習慣沉默
if (Build.VERSION.SDK_INT>=24)
                {
                    imageUri = FileProvider.getUriForFile(MainActivity.this,"com.studio.cameraalbumtest.fileprovider",outputImage);
                }
                else
                {
                    imageUri = Uri.fromFile(outputImage);
                }

Isn’t this where you specify the save location of the picture?

巴扎黑

putExtra is used to pass parameters. I think the author did not understand the real purpose of Intent

By calling the intent.putExtra(String name, Parcelable value) method, you pass the path of the picture you want to save. After opening the camera, click the photo button, and the system will save the picture according to the address you provided

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template