Question

Je suis en mesure d'obtenir la hauteur et la largeur d'une image. Mais est-il une méthode pour récupérer la taille (en octets ou kb ou mb) pour une image stockée dans le téléphone?

Était-ce utile?

La solution

Thank you for your answers. This is how I finally resolved it:

 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; 

Appreciate your time and answers :)

Autres conseils

You just need to create a new File object such as...

String filepath = Environment.getExternalStorageDirectory() + "/file.png";
File file = new File(filepath);
long length = file.length();

Assuming your talking about a bitmap (and NOT an ImageView), there is a method 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");

This returns the true size that is match in computers when you see file details with right click.

File file = new File("/sdcard/my_video.mp4");
long length = file.length() / 1024; // Size in KB
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top