【问题标题】:how to upload images from sd card by picking over them in android如何通过在android中挑选它们来从sd卡上传图像
【发布时间】:2023-04-09 07:08:01
【问题描述】:

在我的应用程序中,我试图将图像发送到存储在 sd 卡中的服务器。当我单击一个按钮时,它会打开 sd 卡并在网格视图中显示图像。从那我想上传我正在触摸它的图像。我知道将图像上传到服务器,但我不知道从 sd 卡中选择图像请帮助我.....

以下是我的代码....

public void library()
{
   Intent myIntent = new Intent();
   myIntent.setType("image/*");
   myIntent.setAction(Intent.ACTION_GET_CONTENT);
   startActivityForResult(Intent.createChooser(myIntent,"Select Picture"), 101);
}
public void onActivityResult(int requestCode, int resultCode, Intent myIntent)
{
    Intent data = null;
    if (requestCode == 101 && data != null) 
    {
    Uri selctedImageUri =data.getData(); 
    }
    else 
    {
        Toast toast = Toast.makeText(this, "No Image is selected.", Toast.LENGTH_LONG);
    toast.show();
    }
 }

如何进行请帮助我.....

【问题讨论】:

    标签: android image upload sd-card


    【解决方案1】:

    这是获取图库图片的意图:

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

    那么这段代码就是在 Image View 中设置从 Gallery 中选择的图片: 在 ActivityResult 上使用:

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

    这种减小图像尺寸的方法;

     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<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
                        break;
                    width_tmp/=2;
                    height_tmp/=2;
                    scale++;
                }
    
                //decode with inSampleSize
                BitmapFactory.Options o2 = new BitmapFactory.Options();
                o2.inSampleSize=scale;
                return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
            } catch (FileNotFoundException e) {}
            return null;
        }
    

    在这些方法中,如果您传递包含图像的文件,它将调整图像大小并返回 BITMAP..

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-05
      • 1970-01-01
      • 1970-01-01
      • 2011-08-27
      • 2011-10-07
      • 2011-08-06
      • 1970-01-01
      相关资源
      最近更新 更多