来个大神帮帮忙,这种String的值怎么读取 就显示只有7个字符,但实际上有300多个,怎么读取实际的值,或者设置String的count,offset?? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 楼主贴代码出来吧,这value存的就是变量的值,不明白什么意思 package com.yaxh.picCut;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStream;import android.graphics.Bitmap;import android.graphics.Bitmap.CompressFormat;import android.graphics.BitmapFactory;import android.graphics.Matrix;import android.media.ExifInterface;import android.util.Log;public class BitmapUtil { /** * 压缩图片 如果图片宽高有一个大于2000,并且压缩比例小于等于2,则调为4 如果图片宽高小于1000*1000则不压缩 * * @param orgImagePath * 图片原路径 * @param desertImagePath * 压缩图片保存路径 * @param scale * 压缩比例,为2,4,8 * @return true,压缩成功;false,压缩失败。 */ public static boolean compressImageFile(String orgImagePath, String desertImagePath, int scale) { CompressFormat format = Bitmap.CompressFormat.JPEG; OutputStream stream = null; Bitmap bitmap = null; boolean b = false; try { int quality = 100; File file = new File(orgImagePath); long fileSize = file.length(); if (200 * 1024 < fileSize && fileSize <= 1024 * 1024) { quality = 75; } else if (1024 * 1024 < fileSize) { quality = 70; } BitmapFactory.Options o = new BitmapFactory.Options(); o.inJustDecodeBounds = true; BitmapFactory.decodeFile(orgImagePath, o); // 如果图片宽高大于2000,并且压缩比例小于等于2,则调为4 int width_tmp = o.outWidth, height_tmp = o.outHeight; if ((width_tmp > 2000 || height_tmp > 2000) && scale <= 2) { scale = 4; } // 如果图片小于1000*1000则不压缩 if (width_tmp < 1000 && height_tmp < 1000) { scale = 1; } if (scale == 1 && quality == 100) { File file2 = new File(desertImagePath); boolean b2 = copyfile(file, file2); Log.i("xpf", "desertImagePath=" + desertImagePath); Log.i("xpf", "无损压缩" + b2 + "原图大小=" + file.length() / 1024 + "KB"); return b2; } BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = scale; options.inPreferredConfig = Bitmap.Config.ARGB_8888; options.inDither = false; bitmap = BitmapFactory.decodeFile(orgImagePath, options); // 旋转图片 int degrees = readPictureDegree(orgImagePath); if (degrees != 0 && bitmap != null) { Log.i("xpf", "图片旋转=" + degrees); Matrix m = new Matrix(); m.setRotate(degrees, (float) bitmap.getWidth() / 2, (float) bitmap.getHeight() / 2); bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), m, true); } stream = new FileOutputStream(desertImagePath); if (bitmap != null) { b = bitmap.compress(format, quality, stream); File file2 = new File(desertImagePath); Log.i("xpf", "desertImagePath=" + desertImagePath); Log.i("xpf", "scale=" + scale + " 图片质量=" + quality + " 原图为" + width_tmp + "*" + height_tmp + " 压缩后为" + options.outWidth + "*" + options.outHeight); Log.i("xpf", "原图大小=" + file.length() / 1024 + "KB" + " 压缩后图片=" + file2.length() / 1024 + "kb" + " 实际压缩率为:" + file2.length() * 100 / file.length() + "% 标准压缩率为:" + 100 / (scale * scale) + "%"); } bitmap.recycle(); bitmap = null; System.gc(); if (stream != null) { stream.close(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return b; } /** * 复制文件 * * @param fromFile * 源文件路径 * @param toFile * 目标文件路径 * @return true 复制成功;false 复制失败 */ public static boolean copyfile(File fromFile, File toFile) { if (!fromFile.exists() || !fromFile.isFile() || !fromFile.canRead()) { return false; } try { if (!toFile.getParentFile().exists()) { toFile.getParentFile().mkdirs(); } toFile.delete(); java.io.FileInputStream fisfrom = new java.io.FileInputStream(fromFile); java.io.FileOutputStream fosto = new FileOutputStream(toFile); byte bt[] = new byte[1024]; int c; while ((c = fisfrom.read(bt)) > 0) { fosto.write(bt, 0, c); // 将内容写到新文件当中 } if (fisfrom != null) { fisfrom.close(); } fosto.flush(); if (fosto != null) { fosto.close(); } return true; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return false; } /** * 获取图片的旋转角度 * * @Title: readPictureDegree * @param path * @return int * @date 2013-11-27 上午9:22:33 */ @SuppressWarnings("deprecation") private static int readPictureDegree(String path) { int degree = 0; try { ExifInterface exifInterface = new ExifInterface(path); //下面这个String的长度比实际的要小,怎么完全读出来? String orientations = exifInterface.getAttribute(ExifInterface.TAG_MAKE); StringBuilder sb=new StringBuilder();// StringBuilder sb = new StringBuilder();// for (int i = 0; i < 300; i++) {// sb.append(orientations.charAt(i));// }// Log.i("xpf", "sb=" + sb.toString()); byte[] bs = orientations.getBytes(); // byte[] bs2 = new byte[300]; // orientations.getBytes(0, 300, bs2, 0); char[] chs = orientations.toCharArray(); Log.i("xpf", "chs.leng=" + chs.length); Log.i("xpf", "chs=" + chs.toString()); int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); String string = "csdfa"; switch (orientation) { case ExifInterface.ORIENTATION_ROTATE_90: degree = 90; break; case ExifInterface.ORIENTATION_ROTATE_180: degree = 180; break; case ExifInterface.ORIENTATION_ROTATE_270: degree = 270; break; default: degree = 0; } } catch (IOException e) { e.printStackTrace(); } return degree; }} 看了你的155行,我自己写了一个 String hello = "Samsung";进行了debug,也没见过你遇到的问题啊。 因为你的String的offset是0啊,我的是29,我的不是我写的,是api返回的 这个问题我已经解决了,只要把 155 String orientations = exifInterface.getAttribute(ExifInterface.TAG_MAKE); 改成String orientations = exifInterface.getAttribute(ExifInterface.TAG_MODEL);就可以拿到手机型号了 oracle数据库 这种问题有点钻牛角尖,相信想把JAVA学好学透的人都会喜欢。 msnm获得msn联系人列表后main函数还一直运行,怎么才能在我获得列表后main不处于监听状态? 如何 程序实现 设置 系统的环境变量? 有没有免安装的jdk,我的电脑不能安装jdk?谢谢 我要计算10W的阶乘,怎么办? 谁有windows编程相关的书要转让,或者换书. 我有《java编程思想》南宁市!!! ++java中已经定义大小的数组能不能改大小++ 请教!可能是一个简单的问题。 B/S数据传输加密 JNI新手求助,创建不了新的jdouble数组 大神求助java中action接受大数据量
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.util.Log;public class BitmapUtil {
/**
* 压缩图片 如果图片宽高有一个大于2000,并且压缩比例小于等于2,则调为4 如果图片宽高小于1000*1000则不压缩
*
* @param orgImagePath
* 图片原路径
* @param desertImagePath
* 压缩图片保存路径
* @param scale
* 压缩比例,为2,4,8
* @return true,压缩成功;false,压缩失败。
*/
public static boolean compressImageFile(String orgImagePath, String desertImagePath, int scale) {
CompressFormat format = Bitmap.CompressFormat.JPEG;
OutputStream stream = null;
Bitmap bitmap = null;
boolean b = false;
try {
int quality = 100;
File file = new File(orgImagePath);
long fileSize = file.length();
if (200 * 1024 < fileSize && fileSize <= 1024 * 1024) {
quality = 75;
} else if (1024 * 1024 < fileSize) {
quality = 70;
}
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeFile(orgImagePath, o);
// 如果图片宽高大于2000,并且压缩比例小于等于2,则调为4
int width_tmp = o.outWidth, height_tmp = o.outHeight;
if ((width_tmp > 2000 || height_tmp > 2000) && scale <= 2) {
scale = 4;
}
// 如果图片小于1000*1000则不压缩
if (width_tmp < 1000 && height_tmp < 1000) {
scale = 1;
}
if (scale == 1 && quality == 100) {
File file2 = new File(desertImagePath);
boolean b2 = copyfile(file, file2);
Log.i("xpf", "desertImagePath=" + desertImagePath);
Log.i("xpf", "无损压缩" + b2 + "原图大小=" + file.length() / 1024 + "KB");
return b2;
}
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = scale;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
options.inDither = false;
bitmap = BitmapFactory.decodeFile(orgImagePath, options);
// 旋转图片
int degrees = readPictureDegree(orgImagePath);
if (degrees != 0 && bitmap != null) {
Log.i("xpf", "图片旋转=" + degrees);
Matrix m = new Matrix();
m.setRotate(degrees, (float) bitmap.getWidth() / 2, (float) bitmap.getHeight() / 2);
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), m, true);
}
stream = new FileOutputStream(desertImagePath);
if (bitmap != null) {
b = bitmap.compress(format, quality, stream);
File file2 = new File(desertImagePath);
Log.i("xpf", "desertImagePath=" + desertImagePath);
Log.i("xpf", "scale=" + scale + " 图片质量=" + quality + " 原图为" + width_tmp + "*" + height_tmp + " 压缩后为" + options.outWidth
+ "*" + options.outHeight);
Log.i("xpf", "原图大小=" + file.length() / 1024 + "KB" + " 压缩后图片=" + file2.length() / 1024 + "kb" + " 实际压缩率为:" + file2.length()
* 100 / file.length() + "% 标准压缩率为:" + 100 / (scale * scale) + "%");
}
bitmap.recycle();
bitmap = null;
System.gc();
if (stream != null) {
stream.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return b;
} /**
* 复制文件
*
* @param fromFile
* 源文件路径
* @param toFile
* 目标文件路径
* @return true 复制成功;false 复制失败
*/
public static boolean copyfile(File fromFile, File toFile) {
if (!fromFile.exists() || !fromFile.isFile() || !fromFile.canRead()) {
return false;
}
try {
if (!toFile.getParentFile().exists()) {
toFile.getParentFile().mkdirs();
}
toFile.delete();
java.io.FileInputStream fisfrom = new java.io.FileInputStream(fromFile);
java.io.FileOutputStream fosto = new FileOutputStream(toFile);
byte bt[] = new byte[1024];
int c;
while ((c = fisfrom.read(bt)) > 0) {
fosto.write(bt, 0, c); // 将内容写到新文件当中
}
if (fisfrom != null) {
fisfrom.close();
}
fosto.flush();
if (fosto != null) {
fosto.close();
}
return true;
} catch (FileNotFoundException e) {
e.printStackTrace(); } catch (IOException e) {
e.printStackTrace();
}
return false;
} /**
* 获取图片的旋转角度
*
* @Title: readPictureDegree
* @param path
* @return int
* @date 2013-11-27 上午9:22:33
*/
@SuppressWarnings("deprecation")
private static int readPictureDegree(String path) {
int degree = 0;
try { ExifInterface exifInterface = new ExifInterface(path);
//下面这个String的长度比实际的要小,怎么完全读出来?
String orientations = exifInterface.getAttribute(ExifInterface.TAG_MAKE);
StringBuilder sb=new StringBuilder();
// StringBuilder sb = new StringBuilder();
// for (int i = 0; i < 300; i++) {
// sb.append(orientations.charAt(i));
// }
// Log.i("xpf", "sb=" + sb.toString());
byte[] bs = orientations.getBytes();
// byte[] bs2 = new byte[300];
// orientations.getBytes(0, 300, bs2, 0);
char[] chs = orientations.toCharArray();
Log.i("xpf", "chs.leng=" + chs.length);
Log.i("xpf", "chs=" + chs.toString());
int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
String string = "csdfa";
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
degree = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
degree = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
degree = 270;
break;
default:
degree = 0;
}
} catch (IOException e) {
e.printStackTrace();
}
return degree;
}
}
String orientations = exifInterface.getAttribute(ExifInterface.TAG_MAKE); 改成String orientations = exifInterface.getAttribute(ExifInterface.TAG_MODEL);就可以拿到手机型号了