改变图像的高度和宽度 - android

时间:2016-05-27 18:18:26

标签: android imageview

我正在创建一个Android应用程序,人们可以将一些文本和图像发布到服务器,然后其他人可以看到照片,但我想改变照片的高度和宽度,但我不知道如何更改。是否应在发送之前更改大小,或者应在从服务器获取后更改大小。如果有人有想法从服务器获取后更改它,请告诉我发布其他课程。 这是我将数据发送到服务器的类。任何帮助!

package naqibshayea.afghanbazaar;


public class insert_ads extends AppCompatActivity {

private String url_insert_Ads , seller1;

private String[] id;
private String[] name;

private EditText title , intro ,  seller ,  phone;

private TextView selected_cat , selected_img_txt;

private ImageView selected_img;

private Spinner my_spinner;

private String[] selected_cat_info = new String[2];
ProgressDialog pDialog;

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_insert_ads);

    getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
    Toolbar toolbar = (Toolbar) findViewById( R.id.app_bar );
    setSupportActionBar(toolbar);
    /*----------------------------------------------------------------------------------------*/
    selected_cat = (TextView) findViewById( R.id.txt_insert_ads_selected_cat );
    selected_img_txt = (TextView) findViewById( R.id.insert_ads_selected_img_txt );
    selected_img = (ImageView) findViewById( R.id.insert_ads_selected_img );

    title = (EditText) findViewById( R.id.insert_ads_title );
    intro = (EditText) findViewById( R.id.insert_ads_intro );
    seller = (EditText) findViewById( R.id.insert_ads_seller );
    phone = (EditText) findViewById( R.id.insert_ads_phone );

    my_spinner = (Spinner) findViewById(R.id.insert_ads_spinner);

    /*----------------------------------------------------------------------------------------*/
    Bundle data = getIntent().getExtras();

    url_insert_Ads = data.getString("url");

    seller1 = data.getString("UNAME");

    seller.setText(seller1);

    id = data.getStringArray("cat_id");

    name = data.getStringArray("cat_name");

    ArrayList<String> cats = new ArrayList<>();

    for (int i = 0; i < id.length ; i++)
    {
        cats.add(i , name[i]);
    }

    ArrayAdapter<String> adb = new ArrayAdapter<String>(this , android.R.layout.simple_list_item_1 , cats);

    my_spinner.setAdapter(adb);

    my_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int position, long _id) {

            selected_cat_info[0] = id[position];
            selected_cat_info[1] = name[position];

            selected_cat.setText(name[position]);
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    });
}



private int my_requestCode = 1;
private Bitmap myBitmap;
private String my_finalImage;

public void onBtnCameraClick(View v)
{
    if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY))
    {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        startActivityForResult(intent, my_requestCode);
    }
    else
    {
        Toast.makeText(getApplicationContext() , getString(R.string.no_camera_error) ,Toast.LENGTH_LONG).show();
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if ( requestCode == my_requestCode && resultCode == RESULT_OK)
    {
        Bundle e = data.getExtras();

        myBitmap = (Bitmap) e.get("data");

        show_captured_image();
    }
   else if (requestCode == My_requestCode_gallery && resultCode == RESULT_OK)
    {
        Uri image = data.getData();

        show_internal_image(image);
    }
    else
    {
        Toast.makeText(getApplicationContext() , getString(R.string.get_image_error) ,Toast.LENGTH_LONG).show();

    }
}

public void show_captured_image()
{
    AlertDialog.Builder imageLoader = new AlertDialog.Builder(this);

    LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);

    View layout = inflater.inflate(R.layout.full_screen_image , (ViewGroup) findViewById(R.id.full_img_layout_root));

    ImageView bigImage = (ImageView) layout.findViewById(R.id.full_img_img);

    bigImage.setImageBitmap(myBitmap);

    TextView imageTitle = (TextView) layout.findViewById(R.id.full_image_title);

    imageTitle.setText(R.string.captured_img_title);

    imageLoader.setView(layout);

    imageLoader.setCancelable(false);

    imageLoader.setPositiveButton(R.string.captured_img_btn_ok, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

            selected_img.setImageBitmap(myBitmap);

            selected_img_txt.setText(R.string.captured_img_is_true);

            dialog.dismiss();
        }
    });

    imageLoader.setNegativeButton(R.string.captured_img_btn_again, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
            onBtnCameraClick(null);
        }
    });

    imageLoader.setNeutralButton(R.string.btn_Back_to_home, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });

    imageLoader.create();

    imageLoader.show();
}

private int My_requestCode_gallery = 2;

public void onBtnGalleryClick(View v)
{
    Intent i = new Intent(Intent.ACTION_PICK , MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

    startActivityForResult(i, My_requestCode_gallery);
}

public void onImageClick(View v)
{
    Intent i = new Intent(Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(i, My_requestCode_gallery);
}


public void show_internal_image(final Uri imageUri)
{
    AlertDialog.Builder imageLoader = new AlertDialog.Builder(this);

    LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);

    View layout = inflater.inflate(R.layout.full_screen_image , (ViewGroup) findViewById(R.id.full_img_layout_root));

    ImageView bigImage = (ImageView) layout.findViewById(R.id.full_img_img);

    bigImage.setImageURI(imageUri);

    TextView imageTitle = (TextView) layout.findViewById(R.id.full_image_title);

    imageTitle.setText(R.string.captured_img_title);

    imageLoader.setView(layout);

    imageLoader.setCancelable(false);

    imageLoader.setPositiveButton(R.string.captured_img_btn_ok, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

            selected_img.setImageURI(imageUri);
            selected_img_txt.setText(R.string.captured_img_is_true);
            dialog.dismiss();
        }
    });

    imageLoader.setNegativeButton(R.string.captured_img_btn_again, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
            onBtnGalleryClick(null);
        }
    });

    imageLoader.setNeutralButton(R.string.btn_Back_to_home, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });

    imageLoader.create();

    imageLoader.show();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_insert_ads, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();

    if (id == R.id.cancel_post)
    {
        finish();
    }

    if (id == R.id.share_post)
    {
        if (title.getText().length() > 1)
        {

            if (intro.getText().length() > 1)

            {
                if( seller.getText().length() > 1 )
                {
                    if (phone.getText().length() > 1)
                    {
                        if (selected_cat.getText() != getString(R.string.insert_ads_form_sel_cat_text)){

                            if (selected_img_txt.getText() != getString(R.string.insert_ads_form_error_sel_cat)){

                                Bitmap image =( (BitmapDrawable) selected_img.getDrawable()).getBitmap();

                                HashMap<String , String> my_data = new HashMap<>();

                                my_data.put( "title" , title.getText().toString() );
                                my_data.put( "intro" , intro.getText().toString() );
                                my_data.put("seller", seller.getText().toString());
                                my_data.put("phone", phone.getText().toString());
                                my_data.put("cat", selected_cat_info[0]);


                                UploadImage upload = new UploadImage( my_data , image , this );

                                upload.execute();

                            }
                            else
                            {
                                Toast.makeText(getApplicationContext() , R.string.insert_ads_form_error_sel_img , Toast.LENGTH_LONG).show();
                            }
                        }
                        else
                        {
                            Toast.makeText(getApplicationContext() , R.string.insert_ads_form_error_sel_cat , Toast.LENGTH_LONG).show();
                        }
                    }
                    else
                    {
                        phone.setHint(R.string.insert_ads_form_error_hint);
                    }

                }
                else
                {
                    seller.setHint( R.string.insert_ads_form_error_hint );
                }
            }
            else
            {
                intro.setHint(R.string.insert_ads_form_error_hint);
            }

        }
        else
        {
            title.setHint(R.string.insert_ads_form_error_hint);
        }
    }
    return super.onOptionsItemSelected(item);
}

private class UploadImage extends AsyncTask<Void , Void ,Boolean>
{
    private Context main_con;

    private HashMap<String , String> main_hm;
    private Bitmap main_image;

    public UploadImage(HashMap<String , String> hm , Bitmap image , Context con)
    {
        main_hm = hm;
        main_image = image;
        main_con = con;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(insert_ads.this);
        pDialog.setMessage("درحال آماده سازی");
        pDialog.setCancelable(false);
        pDialog.show();
    }
    @Override
    protected Boolean doInBackground(Void... voids)
    {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

        main_image.compress(Bitmap.CompressFormat.JPEG , 100 , outputStream);

        String encoded_image = Base64.encodeToString(outputStream.toByteArray() , Base64.DEFAULT);

        ArrayList<NameValuePair> dataToSend = new ArrayList<>();

        dataToSend.add( new BasicNameValuePair("title" , main_hm.get("title") ) );
        dataToSend.add( new BasicNameValuePair("intro" , main_hm.get("intro") ) );
        dataToSend.add(new BasicNameValuePair("seller" , main_hm.get("seller") ) );
        dataToSend.add( new BasicNameValuePair("phone" , main_hm.get("phone") ) );
        dataToSend.add( new BasicNameValuePair("cat" , main_hm.get("cat") ) );
        dataToSend.add(new BasicNameValuePair("image", encoded_image));

        HttpParams myHttpParams = new BasicHttpParams();

        HttpConnectionParams.setConnectionTimeout(myHttpParams, 1000 * 30);

        HttpConnectionParams.setSoTimeout(myHttpParams, 1000 * 30);

        HttpClient client = new DefaultHttpClient( myHttpParams );

        HttpPost post = new HttpPost( url_insert_Ads );

        try {

            post.setEntity( new UrlEncodedFormEntity( dataToSend , "UTF-8"));

            client.execute( post );

            return true;

        }
        catch ( Exception e )
        {
            /*
             * Log.i( "MatiMessage" , "error in posting data -> " + e.toString() );
             */
            return false;
        }

    }

    @Override
    protected void onPostExecute(Boolean result)
    {
        pDialog.dismiss();
        String message = "";

        if( result == true )
        {
            message = getString( R.string.result_of_insert_message_success );
        }
        else
        {
            message = getString( R.string.result_of_insert_message_failure );
        }

        AlertDialog.Builder alert = new AlertDialog.Builder( main_con );
        alert.setCancelable(false);
        alert.setTitle(R.string.result_of_insert_title);
        alert.setMessage(message);
        alert.setPositiveButton(R.string.btn_Back_to_home,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }
        );
        alert.create();
        alert.show();
    }

}

}

1 个答案:

答案 0 :(得分:1)

收到你的位图后,调整图像大小并设置到你的imageview。

ImageView iv = (ImageView) findViewById(R.id.imageView);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.picture);
Bitmap resizedBmp = Bitmap.createScaledBitmap(bitmap, newWidth, newHeight, true);
iv.setImageBitmap(resizedBmp);