cant convert bytes to KB
Asked Answered
W

2

8

I have an android videoplayer which displays all videos on SD card with name and file size, but the size is displayed in bytes and I have not been able to convert it to KB,MB,GB etc.I tried dividing the int value by 1024 but its not working.It prints the wrong value. Can anyone help ?

SDVideos.java

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.text.DecimalFormat;

public class SDVideos extends Activity
{
    private Cursor videoCursor;
    int videoColumnIndex;
    ListView videolist;
    int count;

    String[] thumbColumns = { MediaStore.Video.Thumbnails.DATA,MediaStore.Video.Thumbnails.VIDEO_ID };
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sdvideos);
        initialization();
    }

    private void initialization()
    {
        System.gc();
        String[] videoProjection = { MediaStore.Video.Media._ID,MediaStore.Video.Media.DATA,
                MediaStore.Video.Media.DISPLAY_NAME,MediaStore.Video.Media.SIZE };
        videoCursor =getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,videoProjection, null, null, null);
        count = videoCursor.getCount();
        videolist = (ListView) findViewById(R.id.PhoneVideoList);

        videolist.setAdapter(new VideoListAdapter(this.getApplicationContext()));
        videolist.setOnItemClickListener(videogridlistener);
    }

    private AdapterView.OnItemClickListener videogridlistener = new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView parent, View v, int position, long id)
        {
            videoColumnIndex = videoCursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
            videoCursor.moveToPosition(position);
            String filename = videoCursor.getString(videoColumnIndex);
            Log.i("FileName: ", filename);
//Intent intent = new Intent(VideoActivity.this, ViewVideo.class);
//intent.putExtra("videofilename", filename);
//startActivity(intent);
        }};

    public class VideoListAdapter extends BaseAdapter
    {
        private Context vContext;

        public VideoListAdapter(Context c)
        {
            vContext = c;
        }

        public int getCount()
        {
            return videoCursor.getCount();
        }

        public Object getItem(int position)
        {
            return position;
        }

        public long getItemId(int position)
        {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent)
        {
            View listItemRow;
            listItemRow = LayoutInflater.from(vContext).inflate(R.layout.listitem, parent, false);

            TextView txtTitle = (TextView)listItemRow.findViewById(R.id.txtTitle);
            TextView txtSize = (TextView)listItemRow.findViewById(R.id.txtSize);
            ImageView thumbImage = (ImageView)listItemRow.findViewById(R.id.imgIcon);

            videoColumnIndex = videoCursor.getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME);
            videoCursor.moveToPosition(position);
            txtTitle.setText(videoCursor.getString(videoColumnIndex));

            videoColumnIndex = videoCursor.getColumnIndexOrThrow(MediaStore.Video.Media.SIZE);
            videoCursor.moveToPosition(position);
            txtSize.setText(" Size(KB):" + (videoCursor.getString(videoColumnIndex/205870)));


            int videoId = videoCursor.getInt(videoCursor.getColumnIndexOrThrow(MediaStore.Video.Media._ID));
            ContentResolver crThumb = getContentResolver();
            BitmapFactory.Options options=new BitmapFactory.Options();
            options.inSampleSize = 1;
            Bitmap curThumb = MediaStore.Video.Thumbnails.getThumbnail(crThumb, videoId, MediaStore.Video.Thumbnails.MICRO_KIND, options);
            thumbImage.setImageBitmap(curThumb);
            return listItemRow;
        }
    }

}

listitem. xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp">

    <ImageView android:id="@+id/imgIcon"
        android:layout_width="90dip"
        android:layout_height="90dip"
        android:paddingLeft="10dip"
        android:paddingRight="10dip"/>

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginRight="10dip">

        <TextView android:id="@+id/txtTitle"
            android:layout_width="250dip"
            android:layout_height="wrap_content"
            android:gravity="left"
            android:textStyle="normal"
            android:textSize="15dp"
            android:textColor="#FF0000"
            android:paddingTop="30dp"
            android:paddingLeft="20dp"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp" />

        <TextView android:id="@+id/txtSize"
            android:layout_width="250dip"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:paddingRight="20dp"
            android:paddingTop="20dp"
            android:textStyle="italic"
            android:textSize="12dp"
            android:textColor="#00FF00" />
    </LinearLayout>
</LinearLayout>
Wilton answered 6/8, 2017 at 14:56 Comment(7)
If you can do it for 1, just loop through your list and convert for allNitrobacteria
I can do it for none and I am using mediastore for getting file size in bytesWilton
What is significant about the value: 205870 in your code ?Nashua
You should re-use convertView in the getView() method, if it isn't null.Zelma
nothing @Nashua its just a random number, no matter by which number I divide , I always get the same valueWilton
can you explain a bit more @Ridcully, I am really new to android developmentWilton
Basic idea is, that if you have a list of e.g. 1000 rows but only 10 of them are visible at once, you should not create 1000 views (as you are doing right now), but reuse already existing ones with a different content. You can find a tutorial about listviews here: vogella.com/tutorials/AndroidListView/article.htmlZelma
N
8

You can first use the length method to get the size of the file. then do it this way -

videoColumnIndex = videoCursor.getColumnIndexOrThrow(MediaStore.Video.Media.SIZE);

long fileSizeInBytes = videoCursor.getLong(videoColumnIndex)
// Convert the bytes to Kilobytes (1 KB = 1024 Bytes)
long fileSizeInKB = fileSizeInBytes / 1024;
// Convert the KB to MegaBytes (1 MB = 1024 KBytes)
long fileSizeInMB = fileSizeInKB / 1024;
Nitrobacteria answered 6/8, 2017 at 15:3 Comment(4)
can't I do something similar using MediaStore.Video.Media.DATA ?Wilton
Aren't you getting the size in your code in the below line - txtSize.setText(" Size(KB):" + (videoCursor.getString(videoColumnIndex/205870)));Nitrobacteria
I am getting the size , in bytes, but dividing videocolumnindex by 1024 gives wrong valueWilton
Thanks a lot, I figured it out too at the same time, I just needed to use a long variable. Thanks anyways, your answer is right so I will accept itWilton
C
2

You have to divide the byte by 1024 to get them in kb and divide kb by 1024 to get mb and divide mb by 1024 to get gb and so on.

e.g.:

long kilobytes = file.length() / 1024;
long megabytes = kilobytes / 1024;
long gigabytes = megabytes / 1024;
Coffer answered 6/8, 2017 at 15:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.