相关疑难解决方法(0)

如何在上传到服务器之前减小图像文件大小

许多应用程序允许共享从图库中挑选的图像.

他们上传原始图片文件吗?这是1-3 MB?或者他们处理?

在任何情况下,我如何从文件路径中获取图像,通过降低分辨率来减小其大小,并将其保存在其他地方并尝试上传?

我试过了:

Bitmap photo = decodeSampledBitmapFromFile(filePath, DESIRED_WIDTH,
                    DESIRED_HEIGHT);

FileOutputStream out = new FileOutputStream(filePath);
photo.compress(Bitmap.CompressFormat.JPEG, 100, out);

public static Bitmap decodeSampledBitmapFromFile(String path, int reqWidth,
        int reqHeight) {

    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(path, options);

    final int height = options.outHeight;
    final int width = options.outWidth;
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    int inSampleSize = 1;

    if (height > reqHeight) {
        inSampleSize = Math.round((float) height / (float) reqHeight);
    }
    int expectedWidth = width / inSampleSize;

    if (expectedWidth > reqWidth) …
Run Code Online (Sandbox Code Playgroud)

upload android image

73
推荐指数
5
解决办法
7万
查看次数

在Java中将文件读入byte []数组的优雅方式

可能重复:
Java中的文件到byte []

我想从文件中读取数据并将其解组为Parcel.在文档中不清楚,FileInputStream具有读取其所有内容的方法.为了实现这一点,我做了以下事情:

FileInputStream filein = context.openFileInput(FILENAME);


int read = 0;
int offset = 0;
int chunk_size = 1024;
int total_size = 0;

ArrayList<byte[]> chunks = new ArrayList<byte[]>();
chunks.add(new byte[chunk_size]);
//first I read data from file chunk by chunk
while ( (read = filein.read(chunks.get(chunks.size()-1), offset, buffer_size)) != -1) {
    total_size+=read;
    if (read == buffer_size) {
         chunks.add(new byte[buffer_size]);
    }
}
int index = 0;

// then I create big buffer        
byte[] rawdata = new byte[total_size];

// then I copy data …
Run Code Online (Sandbox Code Playgroud)

java file-io android file

70
推荐指数
6
解决办法
16万
查看次数

标签 统计

android ×2

file ×1

file-io ×1

image ×1

java ×1

upload ×1