相关疑难解决方法(0)

711
推荐指数
20
解决办法
100万
查看次数

BitmapFactory.decodeFile内存不足,图像为2400x2400

我需要将文件从文件发送到服务器.服务器以2400x2400的分辨率请求图像.

我想要做的是:

1)使用正确的inSampleSize使用BitmapFactory.decodeFile获取位图.

2)以JPEG格式压缩图像,质量为40%

3)在base64中对图像进行编码

4)发送到服务器

我无法实现第一步,它抛出一个内存不足的异常.我确信inSampleSize是正确的,但我想即使使用inSampleSize,Bitmap也很大(在DDMS中大约30 MB).

任何想法怎么办呢?我可以在不创建位图对象的情况下执行这些步骤吗?我的意思是在文件系统而不是RAM内存上进行.

这是当前的代码:

// The following function calculate the correct inSampleSize
Bitmap image = Util.decodeSampledBitmapFromFile(imagePath, width,height);   
// compressing the image
ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 40, baos);
// encode image
String encodedImage = Base64.encodeToString(baos.toByteArray(),Base64.DEFAULT));


public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > …
Run Code Online (Sandbox Code Playgroud)

android bitmap

16
推荐指数
1
解决办法
2万
查看次数

FileInputStream.read(byte [])有什么问题?

在回答我对文件阅读问题的回答时,一位评论者表示FileInputStream.read(byte[])"不能保证填补缓冲区".

File file = /* ... */  
long len = file.length();
byte[] buffer = new byte[(int)len];
FileInputStream in = new FileInputStream(file);
in.read(buffer);
Run Code Online (Sandbox Code Playgroud)

(代码假定文件长度不超过2GB)

除了IOException,什么可能导致该read方法无法检索整个文件内容?

编辑:

代码的概念(以及我回答的问题的OP的目标)是将整个文件一次性读入一块内存,这就是buffer_size = file_size的原因.

java fileinputstream java-io

7
推荐指数
2
解决办法
3736
查看次数

从库中选择图像,对于某些设备,它为null

我从下面的代码中获取图库中的图像

 Uri filePath = data.getData();
 String[] filePathColumn = {MediaStore.Images.Media.DATA,
                    MediaStore.Images.Media.DISPLAY_NAME, MediaStore.Images.Media.MIME_TYPE};
            Cursor cursor =
                    mCon.getContentResolver().query(filePath, filePathColumn, null, null, null);
Run Code Online (Sandbox Code Playgroud)

但是当我在某些设备中从文件探索中选择图像时,光标始终为空,但如果我从图库应用程序中选择相同的图像则可以工作......为什么会这样?

android android-cursor

2
推荐指数
1
解决办法
1749
查看次数

将docx,doc转换为base64 android

我想获取docx文件,如下所示

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

    if (resultCode == MainActivity.RESULT_OK) {

        if (resultCode == RESULT_OK) {
            // Get the Uri of the selected file
            Uri uri = data.getData();
            String uriString = uri.toString();
            File myFile = new File(uriString);
            String path = myFile.getAbsolutePath();
            filepath =path;
            String displayName = null;

            if (uriString.startsWith("content://")) {
                Cursor cursor = null;
                try {
                    cursor = this.getContentResolver().query(uri, null, null, null, null);
                    if (cursor != null && cursor.moveToFirst()) {
                        displayName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)); …
Run Code Online (Sandbox Code Playgroud)

base64 android document doc docx

0
推荐指数
1
解决办法
3793
查看次数

如何从文件读取字节到byte []数组?

我正在编写一个Java程序,它使用RSA加密给定的文本,并将加密的字节(byte []数组)保存在.txt文件中.有一个单独的解密程序读取这些字节.现在我想将相同的字节读入解密程序的byte []中.如何使用Java完成?

BufferedReader brip = new BufferedReader(new FileReader("encrypted.txt"));
Strings CurrentLine = brip.readLine();
byte[] b = sCurrentLine.getBytes();
Run Code Online (Sandbox Code Playgroud)

这就是我从文件中读取数据的方式.但这是错误的,因为它将sCurrentLine变量中的字节转换为再次字节.

java byte bytearray file

-2
推荐指数
1
解决办法
6万
查看次数