【问题标题】:Show selected image in another activity在另一个活动中显示选定的图像
【发布时间】:2017-04-09 06:02:20
【问题描述】:

我在 stackoverflow 上搜索过,但从未找到解决方案

这是我的情况:

我有一个按钮,可以启动一个选择图像的意图

如何按比例调整大小(以保持纵横比)并将其显示在另一个活动上(在图像视图中)?

我还想将此调整大小的图像保存到用户存储中。我该怎么做?

谢谢!

这是我的代码

MainActivity.java

btn.setOnClickListener(new View.OnClickListener() {

		@Override
        public void onClick(View arg0) {
				Intent intent = new Intent();
	            intent.setType("image/*");
	            intent.setAction(Intent.ACTION_GET_CONTENT);

	            startActivityForResult(
	                    Intent.createChooser(intent, getString(R.string.completeaction)),
	                    PICK_FROM_FILE);        	
        }

    });
}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	     Bitmap selectedphoto   = null;

	     super.onActivityResult(requestCode, resultCode, data);
	    if (requestCode == RequestExternal && resultCode == RESULT_OK && null != data) {

	         Uri selectedImage = data.getData();
	         String [] filePathColumn = {MediaStore.Images.Media.DATA};
	         Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
	         cursor.moveToFirst();   
	         int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
	         String filePath = cursor.getString(columnIndex);
	         selectedphoto = BitmapFactory.decodeFile(filePath);
	         cursor.close();
	         Intent intent = new Intent(MainActivity.this,PhotoResized.class);
	         intent.putExtra("data", selectedphoto);
	         startActivity(intent);
	    }
	     }

PhotoResized.java

@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.photoresized);
		img = (ImageView) findViewById(R.id.imageView1);
}

【问题讨论】:

  • 一些照片裁剪器怎么样?
  • 不,我希望将图像调整为特定的高度和宽度,而不是裁剪

标签: java android bitmap resize uri


【解决方案1】:

要调整图像大小,请使用 dev.android here 提供的采样技术

使用 Lrucache 存储图像。使用显示的技术here

【讨论】:

  • 如何将所选图像显示给另一个活动?
  • 您将使用哈希图之类的键将图像存储在缓存中,
  • 并使用该键从 Lrucache 检索图像,如果您还有任何疑问,请告诉我 :)
  • 用 uri 或类似的方式获取意图数据不是更好吗?
  • 在其他activity中显示图片
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多