如何在Android中挑选了他们上传从SD卡的图像 [英] how to upload images from sd card by picking over them in android

查看:95
本文介绍了如何在Android中挑选了他们上传从SD卡的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 to upload the image which i am touching on it. I know to upload an image to a server, but i dont know to select an image from the sd card please help me.....

以下是我的code ....

Following is my code....

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

如何处理,请帮我.....

how to proceed please help me.....

推荐答案

这是意图获取图库图片:

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

那么这code是设置从图片查看画廊选择的图像:  使用在ActivityResult来回这样的:

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

在,如果你传递一个包含图像将调整的图像,并返回一个文件中的这些方法,位图..

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

这篇关于如何在Android中挑选了他们上传从SD卡的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆