2015-09-13 140 views
2

我需要帮助来改进我的代码。如何在不模糊的情况下调整图像大小或使用picasso

我在做什么:有一个在主要活动,点击后按钮,用户选择图像,在这之后,图像是通过一个意图另一个活动(add_image.java)通过,显示的图像查看,之后我将图像发送到服务器。

我的问题:1)我想最好的方式路径图像发送到第二意图然后将其转换成图像
2)然后,就像我可以压缩它没有松动了很多的质量。 image现在的大小是376kb。所以在我的我的应用程序生病显示多个图像,所以在这样的尺寸会消耗时间和网络加载(我用毕加索和配合()didnt减小尺寸。)

这里是我的代码:

@Override 
     protected void onActivityResult(int requestCode, int resultCode, Intent data) { 

      super.onActivityResult(requestCode, resultCode, data); 

      if (requestCode == 1 && resultCode == RESULT_OK && data != null && data.getData() != null) { 

       //file name 
       filePath = data.getData(); 
       try { 
       // Bundle extras2 = data.getExtras(); 
        bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath); 
        ByteArrayOutputStream stream = new ByteArrayOutputStream();    
        byte imageInByte[] = stream.toByteArray();      
        Intent i = new Intent(this, AddImage.class); 
        i.putExtra("image", imageInByte); 
        startActivity(i); 
       } catch (IOException e) { 
        e.printStackTrace();  } } }  

在这里,我收到的图像

 byte[] byteArray = getIntent().getByteArrayExtra("image"); 
      encodedImage = Base64.encodeToString(byteArray, Base64); 
      bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length); 
      ImageView imageview = (ImageView) findViewById(R.id.imageView); 
      imageview.setImageBitmap(bmp); 
+1

尝试此链接的http://计算器.com/a/20824141/3678308 –

+0

1)尝试发送路径到第二个活动,您将在imageview中显示图像。通过使用putExtra(KEY,PATH_OF_IMAGE)的意图可以将路径发送到第二个活动。在add_image_activity上收回路径时,可以使用路径加载图像。 2)要压缩,你可以检查[this](http://stackoverflow.com/questions/18545246/how-to-compress-image-size)链接。 – avinash

+0

@avinash我试图做到这一点,但它没有工作http://stackoverflow.com/questions/32584648/how-to-send-from-onactivityresult-uri-path-to-another-activity-and-change-it-到 – Moudiz

回答

1

尝试下面的代码,你可能需要修改一些参数按照您的要求:

创建MainActivi TY如下:

public class MainActivity extends Activity implements OnClickListener { 
private final int REQUEST_IMAGE_GALLERY = 2000; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    findViewById(R.id.button).setOnClickListener(this); 
} 

@Override 
public void onClick(View v) { 
    switch (v.getId()) { 
    case R.id.button: 
     pickImageFromGallery(); 
     break; 

    default: 
     break; 
    } 
} 

private void pickImageFromGallery() { 
    Intent intent = new Intent(Intent.ACTION_PICK, 
      android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
    intent.setType("image/*"); 
    startActivityForResult(Intent.createChooser(intent, "Select File"), 
      REQUEST_IMAGE_GALLERY); 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    // TODO Auto-generated method stub 
    super.onActivityResult(requestCode, resultCode, data); 

    if (resultCode != Activity.RESULT_OK) { 
     return; 
    } 

    if (requestCode == REQUEST_IMAGE_GALLERY) { 
     Uri selectedImageUri = data.getData(); 

     String[] filePathColumn = { MediaStore.Images.Media.DATA }; 
     Cursor cursor = getContentResolver().query(selectedImageUri, 
       filePathColumn, null, null, null); 
     cursor.moveToFirst(); 
     int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
     String picturePath = cursor.getString(columnIndex); 
     cursor.close(); 

     Log.e("PATH", "" + picturePath); 
     Intent intent = new Intent(this, AddImage.class); 
     intent.putExtra("PATH", picturePath); 
     startActivity(intent); 

    } 
} 
} 

现在创建activity_main.xml中如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<Button 
    android:id="@+id/button" 
    style="?android:attr/buttonStyleSmall" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="ChooseButton" /> 

    </LinearLayout> 

然后我们需要如下创建AddImage活动:

public class AddImage extends Activity { 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.add_image); 
    ImageView imageView = (ImageView) findViewById(R.id.image_view); 
    if (getIntent() != null) { 
     String path = getIntent().getStringExtra("PATH"); 
     Log.e("PATHR", "" + path); 
     new BitmapWorkerTask(imageView).execute(path); 
    } 

} 

private boolean isNeedToBeScaled(String path) { 
    File file = new File(path); 

    if (file.length() > (1024 * 1024) && isExist(path)) { 
     Log.e("SCALEIMAGE", "SACLE"); 
     return true; 
    } 
    return false; 
} 

private boolean isExist(String path) { 
    File file = new File(path); 
    return file.exists(); 
} 

private Bitmap getScaledImage(String path) { 
    BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inJustDecodeBounds = true; 
    BitmapFactory.decodeFile(path, options); 

    int srcWidth = options.outWidth; 
    int srcHeight = options.outHeight; 
    int[] newWH = new int[2]; 
    newWH[0] = srcWidth/2; 
    newWH[1] = (newWH[0] * srcHeight)/srcWidth; 

    int inSampleSize = 2; 
    while (srcWidth/2 >= newWH[0]) { 
     srcWidth /= 2; 
     srcHeight /= 2; 
     inSampleSize *= 2; 

     options.inJustDecodeBounds = false; 
     options.inDither = false; 
     options.inSampleSize = inSampleSize; 
     options.inScaled = false; 
     options.inPreferredConfig = Bitmap.Config.RGB_565; 
    } 
    Bitmap sampledSrcBitmap = BitmapFactory.decodeFile(path, options); 

    return sampledSrcBitmap; 
} 

class BitmapWorkerTask extends AsyncTask<String, Void, Bitmap> { 
    private final WeakReference<ImageView> imageViewReference; 

    public BitmapWorkerTask(ImageView mImageView) { 
     imageViewReference = new WeakReference<ImageView>(mImageView); 
    } 

    @Override 
    protected Bitmap doInBackground(String... params) { 
     Bitmap scaled = null; 
     if (isNeedToBeScaled(params[0])) { 
      Bitmap d; 

      d = getScaledImage(params[0]); 
      int nh = (int) (d.getHeight() * (512.0/d.getWidth())); 
      scaled = Bitmap.createScaledBitmap(d, 512, nh, true); 

     } else { 
      scaled = BitmapFactory.decodeFile(params[0], null); 
     } 
     return scaled; 

    } 

    @Override 
    protected void onPostExecute(Bitmap result) { 

     if (imageViewReference != null && result != null) { 
      final ImageView imageView = imageViewReference.get(); 
      if (imageView != null) { 
       imageView.setImageBitmap(result); 
       imageView.setVisibility(View.VISIBLE); 

      } 
     } 
    } 

} 
} 
+0

好吧,我能够做到这一点,但无论如何检查你的代码,也许我可以提高一些东西..不接受它,但请你请检查我现在问的这个quetios? http://stackoverflow.com/questions/32669433/uploading-image-not-displaying-however-others-is-displaying – Moudiz

相关问题