gpt4 book ai didi

android - 从图库中的选定图像中获取图像的确切文件大小

转载 作者:行者123 更新时间:2023-11-29 14:20:21 24 4
gpt4 key购买 nike

这是我的文件选择器代码

 private void showFileChooser() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}

然后 onActivityResult()

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {

filePath = data.getData();
try {
bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), filePath);

System.out.println(bitmap.getByteCount());

int w = bitmap.getWidth();
int h = bitmap.getHeight();



if (w!= 512 || h!= 512)
{
txvLogoError.setText("Invalid image dimensions. Please choose another.");
}
else
{
txvLogoError.setText("");
imbAppLogo.setPadding(10,10,10,10);
imbAppLogo.setImageBitmap(bitmap);
}


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

我的问题是,如何获得所选图像的确切文件大小?我试过 File.length() 但结果是 0。

最佳答案

试试这个。

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
int dataSize=0;
if (requestCode == 2 && resultCode == RESULT_OK)
{
Uri uri = data.getData();
String scheme = uri.getScheme();
System.out.println("Scheme type " + scheme);
if(scheme.equals(ContentResolver.SCHEME_CONTENT))
{
try {
InputStream fileInputStream=getApplicationContext().getContentResolver().openInputStream(uri);
dataSize = fileInputStream.available();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("File size in bytes"+dataSize);

}
else if(scheme.equals(ContentResolver.SCHEME_FILE))
{
String path = uri.getPath();
try {
f = new File(path);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("File size in bytes"+f.length());
}

}
}

关于android - 从图库中的选定图像中获取图像的确切文件大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42639388/

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