gpt4 book ai didi

java - 无法将字节转换为 KB

转载 作者:数据小太阳 更新时间:2023-10-29 02:52:48 25 4
gpt4 key购买 nike

我有一个 android 视频播放器,它显示 SD 卡上的所有视频名称和文件大小,但大小以字节显示,我无法将它转换为 KB、MB、GB 等。我尝试除以 int值增加 1024 但它不起作用。它打印出错误的值。谁能帮忙?

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;
}
}

}

列表项。 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>

最佳答案

可以先使用length方法获取文件的大小。然后这样做 -

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;

关于java - 无法将字节转换为 KB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45533225/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com