为什么上传android后图像会模糊?

为什么上传android后图像会模糊?,android,image,upload,imageview,blur,Android,Image,Upload,Imageview,Blur,我正在学习将图片上传到服务器,从画廊或安卓相机拍摄的图片 当我使用图像解码从画廊或相机拍摄到图像视图后显示图像时,图像不会模糊。。。 但我上传后,像这样的图像可能会变小并且模糊。 我不知道,哪里出了错。无论是在解码图像上还是上传图像上 public void decodeFile(String filePath) { // Decode image size BitmapFactory.Options o = new BitmapFactory.Options(); o.

我正在学习将图片上传到服务器,从画廊或安卓相机拍摄的图片

当我使用图像解码从画廊或相机拍摄到图像视图后显示图像时,图像不会模糊。。。 但我上传后,像这样的图像可能会变小并且模糊。

我不知道,哪里出了错。无论是在解码图像上还是上传图像上

public void decodeFile(String filePath) {
    // Decode image size
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filePath, o);

    // The new size we want to scale to
    final int REQUIRED_SIZE = 1024;

    // Find the correct scale value. It should be the power of 2.
    int width_tmp = o.outWidth, height_tmp = o.outHeight;
    int scale = 1;
    while (true) {
        if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE)
            break;
        width_tmp /= 2;
        height_tmp /= 2;
        scale *= 2;
    }

    // Decode with inSampleSize
    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize = scale;
    bitmap = BitmapFactory.decodeFile(filePath, o2);

    imgView.setImageBitmap(bitmap);

}
try {

            DatabaseHandler userDB = new DatabaseHandler(getApplicationContext());      
            HashMap<String, String> userDetail = userDB.getUserDetails();
            String uid= userDetail.get("uid");  

            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            bitmap.compress(CompressFormat.JPEG, 100, bos);
            byte[] data = bos.toByteArray();
            HttpClient httpClient = new DefaultHttpClient();                
            HttpPost postRequest = new HttpPost(PHP_URL);               
            ByteArrayBody bab = new ByteArrayBody(data,file_name);              
            MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
            reqEntity.addPart("uploadedfile", bab);
            postRequest.setEntity(reqEntity);
            HttpResponse response = httpClient.execute(postRequest);
            BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(),"UTF-8"));
            String sResponse;
            StringBuilder s = new StringBuilder();

            while ((sResponse = reader.readLine()) != null) {
                s = s.append(sResponse);
            }

            return s.toString().trim();

        } catch (Exception e) {

            err="error"+e.getMessage();
            Log.e(e.getClass().getName(), e.getMessage());

            return e.getMessage();
        }   
这是我的部分代码

解码代码

public void decodeFile(String filePath) {
    // Decode image size
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filePath, o);

    // The new size we want to scale to
    final int REQUIRED_SIZE = 1024;

    // Find the correct scale value. It should be the power of 2.
    int width_tmp = o.outWidth, height_tmp = o.outHeight;
    int scale = 1;
    while (true) {
        if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE)
            break;
        width_tmp /= 2;
        height_tmp /= 2;
        scale *= 2;
    }

    // Decode with inSampleSize
    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize = scale;
    bitmap = BitmapFactory.decodeFile(filePath, o2);

    imgView.setImageBitmap(bitmap);

}
try {

            DatabaseHandler userDB = new DatabaseHandler(getApplicationContext());      
            HashMap<String, String> userDetail = userDB.getUserDetails();
            String uid= userDetail.get("uid");  

            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            bitmap.compress(CompressFormat.JPEG, 100, bos);
            byte[] data = bos.toByteArray();
            HttpClient httpClient = new DefaultHttpClient();                
            HttpPost postRequest = new HttpPost(PHP_URL);               
            ByteArrayBody bab = new ByteArrayBody(data,file_name);              
            MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
            reqEntity.addPart("uploadedfile", bab);
            postRequest.setEntity(reqEntity);
            HttpResponse response = httpClient.execute(postRequest);
            BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(),"UTF-8"));
            String sResponse;
            StringBuilder s = new StringBuilder();

            while ((sResponse = reader.readLine()) != null) {
                s = s.append(sResponse);
            }

            return s.toString().trim();

        } catch (Exception e) {

            err="error"+e.getMessage();
            Log.e(e.getClass().getName(), e.getMessage());

            return e.getMessage();
        }   
公共无效解码文件(字符串文件路径){
//解码图像大小
BitmapFactory.Options o=新的BitmapFactory.Options();
o、 inJustDecodeBounds=true;
解码文件(文件路径,o);
//我们要扩展到的新尺寸
所需的最终int_SIZE=1024;
//找到正确的刻度值。它应该是2的幂。
内部宽度=o.向外宽度,高度=o.向外高度;
int标度=1;
while(true){
如果(宽度\u tmp<要求的\u尺寸和高度\u tmp<要求的\u尺寸)
打破
宽度_tmp/=2;
高度_tmp/=2;
比例*=2;
}
//用inSampleSize解码
BitmapFactory.Options o2=新的BitmapFactory.Options();
o2.inSampleSize=刻度;
位图=BitmapFactory.decodeFile(文件路径,o2);
imgView.setImageBitmap(位图);
}
上传代码

public void decodeFile(String filePath) {
    // Decode image size
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filePath, o);

    // The new size we want to scale to
    final int REQUIRED_SIZE = 1024;

    // Find the correct scale value. It should be the power of 2.
    int width_tmp = o.outWidth, height_tmp = o.outHeight;
    int scale = 1;
    while (true) {
        if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE)
            break;
        width_tmp /= 2;
        height_tmp /= 2;
        scale *= 2;
    }

    // Decode with inSampleSize
    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize = scale;
    bitmap = BitmapFactory.decodeFile(filePath, o2);

    imgView.setImageBitmap(bitmap);

}
try {

            DatabaseHandler userDB = new DatabaseHandler(getApplicationContext());      
            HashMap<String, String> userDetail = userDB.getUserDetails();
            String uid= userDetail.get("uid");  

            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            bitmap.compress(CompressFormat.JPEG, 100, bos);
            byte[] data = bos.toByteArray();
            HttpClient httpClient = new DefaultHttpClient();                
            HttpPost postRequest = new HttpPost(PHP_URL);               
            ByteArrayBody bab = new ByteArrayBody(data,file_name);              
            MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
            reqEntity.addPart("uploadedfile", bab);
            postRequest.setEntity(reqEntity);
            HttpResponse response = httpClient.execute(postRequest);
            BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(),"UTF-8"));
            String sResponse;
            StringBuilder s = new StringBuilder();

            while ((sResponse = reader.readLine()) != null) {
                s = s.append(sResponse);
            }

            return s.toString().trim();

        } catch (Exception e) {

            err="error"+e.getMessage();
            Log.e(e.getClass().getName(), e.getMessage());

            return e.getMessage();
        }   
试试看{
DatabaseHandler userDB=新的DatabaseHandler(getApplicationContext());
HashMap userDetail=userDB.getUserDetails();
字符串uid=userDetail.get(“uid”);
ByteArrayOutputStream bos=新建ByteArrayOutputStream();
压缩(CompressFormat.JPEG,100,bos);
字节[]数据=bos.toByteArray();
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost postRequest=新的HttpPost(PHP_URL);
ByteArrayBody bab=新的ByteArrayBody(数据、文件名);
MultipartEntity reqEntity=新的MultipartEntity(HttpMultipartMode.BROWSER_兼容);
reqEntity.addPart(“上传文件”,bab);
postRequest.setEntity(reqEntity);
HttpResponse response=httpClient.execute(postRequest);
BufferedReader=新的BufferedReader(新的InputStreamReader(response.getEntity().getContent(),“UTF-8”);
字符串响应;
StringBuilder s=新的StringBuilder();
而((sResponse=reader.readLine())!=null){
s=s.append(sResponse);
}
返回s.toString().trim();
}捕获(例外e){
err=“error”+e.getMessage();
Log.e(e.getClass().getName(),e.getMessage());
返回e.getMessage();
}   
上传前,图像显示在ImageView中

上传后,在listview中显示

我希望任何人都能帮助我。抱歉,如果我的英语不好…

您正在100%压缩图像,这会降低图像质量

        bitmap.compress(CompressFormat.JPEG, 100, bos);

我建议使用小于100的值并进行调整,直到质量和尺寸之间达到平衡

压缩机提示,0-100。0表示压缩小尺寸,100表示压缩最大质量Dante和@Selvin,在此之前。。。我已经设置了bitmap.compress(CompressFormat.JPEG,70,bos),但它仍然模糊和更小…是的。。搞错了,解码时应该有问题。。请尝试将下载后的位图大小与上载的位图大小进行比较。我不知道大小的值如何Din listview我使用懒散加载,是否因为这个?