¿Cómo obtener el tamaño de una imagen en Android?
-
26-10-2019 - |
Pregunta
Puedo obtener la altura y el ancho de una imagen. Pero, ¿hay un método para recuperar el tamaño (en bytes o KB o MB) para una imagen almacenada en el teléfono?
Solución
Gracias por sus respuestas. Así es como finalmente lo resolví:
Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_launcher);
Bitmap bitmap = bitmapOrg;
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] imageInByte = stream.toByteArray();
long lengthbmp = imageInByte.length;
Aprecia tu tiempo y sus respuestas :)
Otros consejos
Solo necesitas crear un nuevo File
objeto como ...
String filepath = Environment.getExternalStorageDirectory() + "/file.png";
File file = new File(filepath);
long length = file.length();
Suponiendo que hables de un mapa de bits (y no de ImageView), hay un método Bitmap.getByTecount ().
File file = new File("/sdcard/imageName.jpeg");
long length = file.length();
length = length/1024;
System.out.println("File Path : " + file.getPath() + ", File size : " + length +" KB");
Esto devuelve el tamaño real que coincide en las computadoras cuando ve los detalles del archivo con el clic derecho.
File file = new File("/sdcard/my_video.mp4");
long length = file.length() / 1024; // Size in KB
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow