how to upload images from sd card by picking over them in android

前端 未结 1 1456
时光说笑
时光说笑 2021-01-22 14:01

in my app i am trying to send images to a server that are stored in sd card. When i click a button it opens the sd card and shows the image in a grid view. From that the i want

1条回答
  •  再見小時候
    2021-01-22 14:31

    This is the Intent for getting Gallery images :

        Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
        intent.setType("image/*");
        intent.putExtra("return-data", true);
        startActivityForResult(intent, 1);
    

    Then this code is to set the image selected from the Gallery in the Image View : Use On ActivityResult fro this :

        @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 1:
                if(requestCode == 1 && data != null && data.getData() != null){
                    Uri _uri = data.getData();
    
                    if (_uri != null) {
                        //User had pick an image.
                        Cursor cursor = getContentResolver().query(_uri, new String[] { android.provider.MediaStore.Images.ImageColumns.DATA }, null, null, null);
                        cursor.moveToFirst();
    
                        //Link to the image
                        final String imageFilePath = cursor.getString(0);
                        Log.v("imageFilePath", imageFilePath);
                        File photos= new File(imageFilePath);
                        Bitmap b = decodeFile(photos);
                        b = Bitmap.createScaledBitmap(b,150, 150, true);
                        ImageView imageView = (ImageView) findViewById(R.id.select_image);
                        imageView.setImageBitmap(b);
                        cursor.close();
                    }
                }
                super.onActivityResult(requestCode, resultCode, data);
            }
        }
    

    This Method for Reducing the Size of Image ;

     private Bitmap decodeFile(File f){
            try {
                //decode image size
                BitmapFactory.Options o = new BitmapFactory.Options();
                o.inJustDecodeBounds = true;
                BitmapFactory.decodeStream(new FileInputStream(f),null,o);
    
                //Find the correct scale value. It should be the power of 2.
                final int REQUIRED_SIZE=70;
                int width_tmp=o.outWidth, height_tmp=o.outHeight;
                int scale=1;
                while(true){
                    if(width_tmp/2

    In these method if you Pass a File that contains image it will Resize the image and returns BITMAP..

    0 讨论(0)
提交回复
热议问题