【问题标题】:Get Bitmap image details (such as width and height)获取位图图像详细信息(例如宽度和高度)
【发布时间】:2017-11-09 22:04:43
【问题描述】:

如何获取在图像视图中选择的图像细节(名称、宽度、高度、大小等)

我生成 ImageViewXML

<ImageView android:id="@+id/imageView" android:layout_width="match_parent" android:layout_height="match_parent"/>  

并获得一个函数将所选图像从文件夹放入该 ImageView

Uri uri = data.getData();

        try {
            Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);

            //Log.d(TAG, String.valueOf(bitmap));

            ImageView imageView = (ImageView) findViewById(R.id.imageView);
            imageView.setImageBitmap(bitmap);

        } catch (IOException e) {
            e.printStackTrace();
        }

当我想通过使用 String.valueOf(bitmap) 获取图像详细信息(例如名称)时,它一切正常,没有错误,但它返回:android.graphics.Bitmap @5280fcb4

如何获得图像细节?

【问题讨论】:

  • 试试这个String filename = uri.getLastPathSegment();
  • 这不起作用,它返回 integer 值,在我的例子中是 30

标签: android image bitmap imageview


【解决方案1】:
String path = (String) imageView .getTag();

你可以通过这个 getTag() 获取图像的路径,从中应该得到图像的名称

【讨论】:

  • 它返回 null ,就像我现在一样,我们需要在 xml 文件中手动标记它: android:tag = "任何名字” ?
【解决方案2】:

试试这个

Bitmap bitmap = ((BitmapDrawable)imageView.getBackground()).getBitmap();
int width = bitmap .getWidth();
int height = bitmap .getHeight();

图片大小

 Bitmap bitmap = ((BitmapDrawable)imageView.getBackground()).getBitmap();
 ByteArrayOutputStream bytestream = new ByteArrayOutputStream();   
 bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytestream );   
 byte[] imageByte = bytestream .toByteArray(); 
 long lengthbmp = imageByte .length; 

【讨论】:

  • 图片名称怎么样?
猜你喜欢
  • 2010-11-18
  • 1970-01-01
  • 2017-05-23
  • 2011-11-26
  • 1970-01-01
  • 1970-01-01
  • 2016-08-29
  • 1970-01-01
  • 2011-05-31
相关资源
最近更新 更多