【问题标题】:Set an image picked from gallery in an imageView在 imageView 中设置从图库中选取的图像
【发布时间】:2015-07-10 22:39:57
【问题描述】:

从图库/相机中选择图像后,我想在 imageView 中设置图像。我面临两个问题 1. imageView中只设置了低质量图片,没有设置相机质量。
2.设置设备倾斜时的低质量图像后,imageView变为空白(就像在imageView中设置图像之前一样)。

package com.example.faizantahir.naughtyfire;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class PicUpload extends ActionBarActivity {
    Button button,button1;
    ImageView imageview;
    String selectedImagePath;
    private static final int SELECT_PICTURE = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.pic_upload);
    button=(Button)findViewById(R.id.button5);
    button1=(Button)findViewById(R.id.button6);
    imageview=(ImageView)findViewById(R.id.imageView);
    button1.setOnClickListener(
            new Button.OnClickListener(){
                public void onClick(View view){


                    Intent intent3=new Intent();
                    intent3.setClass(PicUpload.this,FirstFragment.class);
                    startActivity(intent3);
                }
            }

    );



    button.setOnClickListener(
        new Button.OnClickListener(){
            public void onClick(View view){
                Toast t= Toast.makeText(PicUpload.this,"Yoaklfhlkas",Toast.LENGTH_LONG);
                t.show();
                CustomDialogClass cdd = new CustomDialogClass(PicUpload.this);
                cdd.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                cdd.show();

            }

    }

    );
}
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
    switch(requestCode) {
        case 1:
                if(resultCode == RESULT_OK){
                    Uri selectedImageUri = imageReturnedIntent.getData();
                    selectedImagePath = getPath(selectedImageUri);
                    System.out.println("Image Path : " + selectedImagePath);
                    imageview.setImageURI(selectedImageUri);
                }
            break;
    }
}

public String getPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

}

【问题讨论】:

    标签: java android image import


    【解决方案1】:

    您可以使用一个库来加快图像加载速度, 它以您要求的质量缓存图像。

    Universal-Image-Loader

    很容易实现为页面中给出的sn-p。

    【讨论】:

      【解决方案2】:

      我采用了一种简单的方法,因为它们通常是最好和最快的:

      1- 从ImageView 获取位图 2- 获取位图的尺寸 3-计算缩放倍数 4- 规模 5- 获取缩放位图尺寸 6-应用缩放的图像 7- 将ImageView 调整为缩放位图的精确尺寸 8- 瞧

      这里是代码并使用它来适应从相机点击的图像ImageView

      private void scaleImage(ImageView view, int boundBoxInDp)
      

      {

      Drawable drawing = view.getDrawable();
      
      Bitmap bitmap = ((BitmapDrawable)drawing).getBitmap();
      
      // Get current dimensions
      int width = bitmap.getWidth();
      int height = bitmap.getHeight();
      
      // Determine how much to scale: the dimension requiring less scaling is
      // closer to the its side. This way the image always stays inside your
      // bounding box AND either x/y axis touches it.
      float xScale = ((float) boundBoxInDp) / width;
      float yScale = ((float) boundBoxInDp) / height;
      float scale = (xScale <= yScale) ? xScale : yScale;
      
      // Create a matrix for the scaling and add the scaling data
      Matrix matrix = new Matrix();
      matrix.postScale(scale, scale);
      
      // Create a new bitmap and convert it to a format understood by the ImageView
      Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
      BitmapDrawable result = new BitmapDrawable(scaledBitmap);
      width = scaledBitmap.getWidth();
      height = scaledBitmap.getHeight();
      
      // Apply the scaled bitmap
      view.setImageDrawable(result);
      
      // Now change ImageView's dimensions to match the scaled image
      LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams();
      params.width = width;
      params.height = height;
      view.setLayoutParams(params);
      

      }

      private int dpToPx(int dp)
      {
          float density = getApplicationContext().getResources().getDisplayMetrics().density;
          return Math.round((float)dp * density);
      }
      

      并且要使用使用这个

      public class TestActivity extends Activity
      

      {

          @Override public void onCreate(Bundle savedInstanceState)
          {
      
      
       super.onCreate(savedInstanceState);
          setContentView(R.layout.test);
      
          // ImageViews must be under LinearLayout in the xml or the code crashes into scaleImage(). Tune scaleImage() into your needs.
          ImageView view1 = (ImageView) findViewById(R.id.test1);
          ImageView view2 = (ImageView) findViewById(R.id.test2);
      
          scaleImage(view1, 250); // in dp
          scaleImage(view2, 100); // in dp
      }       
      

      }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-10
        • 2018-12-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-19
        相关资源
        最近更新 更多