我正在尝试使用Google示例页面从网址下载图片.我在BitmapFactory.decodeStream方法中使用InputStream时读过,我不能使用两次.我试图这样做,但它不起作用'因为它在解码图像中返回null,我不知道我能做什么.
这是我的代码:
这部分是在AsyncTask类中的doInBackground方法中
Bitmap bitmapImage;
URL imageUrl = null;
try {
imageUrl = new URL(url[0]);
HttpGet httpRequest = null;
httpRequest = new HttpGet(imageUrl.toURI());
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);
HttpEntity entity = response.getEntity();
BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
InputStream instream = bufHttpEntity.getContent();
bitmapImage = CommonMethods.decodeSampledBitmapFromResource(instream, thumb_width, thumb_width);
instream.close();
return bitmapImage;
} catch (URISyntaxException e) {
e.printStackTrace();
return null;
} catch (MalformedURLException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace(); …Run Code Online (Sandbox Code Playgroud) 我已将位图图像转换为字符串以保存它:
............
Bitmap photo = extras.getParcelable("data");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] b = baos.toByteArray();
String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
Run Code Online (Sandbox Code Playgroud)
然后我从字符串中检索位图以设置活动的背景,如下所示:
byte[] temp = Base64.decode(encodedImage, Base64.DEFAULT);
Options options = new BitmapFactory.Options();
options.inScaled = false;
Bitmap bitmap = BitmapFactory.decodeByteArray(temp, 0,
temp.length, options);
Drawable d = new BitmapDrawable(getResources(), bitmap);
getWindow().setBackgroundDrawable(d);
Run Code Online (Sandbox Code Playgroud)
一切正常,但图像质量大大降低.如何保持图像质量与原始图像相同?我在这里做错了什么,降低了质量?
我有一些问题在android中调整图像大小.我有一个base64字符串(我没有文件或网址,只有字符串),到目前为止,我尝试解码它时,我得到一个内存不足的异常.
public String resizeBase64Image(String base64image){
byte [] encodeByte=Base64.decode(base64image,Base64.DEFAULT); //out of memory exception...
BitmapFactory.Options options=new BitmapFactory.Options();
options.inPurgeable = true;
Bitmap image = BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length,options);
image = Bitmap.createScaledBitmap(image, IMG_WIDTH, IMG_HEIGHT, false);
ByteArrayOutputStream baos=new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.PNG,100, baos);
byte [] newbytes=baos.toByteArray();
return Base64.encodeToString(newbytes, Base64.DEFAULT);
}
Run Code Online (Sandbox Code Playgroud)
有人有想法吗?
我已经尝试了我在stackoverflow上找到的所有解决方案来解决这个问题,但是徒劳无功.在运行以下代码时,mAlbumPhotoUri是"/mnt/sdcard/photo/1342147146535.jpg",它是Uri类型.file.exists()表示此文件存在,但在执行最后一行代码后resultBitmap为null.
我究竟做错了什么?
File file = new File(mAlbumPhotoUri.toString());
if(file.exists()){
Toast.makeText(this, "File exists in /mnt", Toast.LENGTH_LONG);}
else {
Toast.makeText(this, "File NOT exists in /mnt", Toast.LENGTH_LONG);}
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true; //only get the size of the bitmap
if (resultPhotoBitmap != null) {
resultPhotoBitmap.recycle();
}
String fname=new File(mAlbumPhotoUri.toString()).getAbsolutePath();
resultPhotoBitmap = BitmapFactory.decodeFile(fname, options);
Run Code Online (Sandbox Code Playgroud) 我使用https://github.com/thest1/LazyList进行图像缓存.我必须全屏显示图像.但是图像质量有很大的损失.哪个代码我必须改变以获得原始image.thanks提前.
我知道 - 标题可能听起来很奇怪.让我解释:
我创建了一个图像来向您展示我正在谈论的内容:

我有一个图像(位图(1)),大小为150w/200h.
现在我需要使位图更大((2)400w/400h),但原始图像必须具有相同的大小.使图像嵌入白色背景中.
我认为解决它的一种方法是:*创建一个大位图*为它创建一个画布*在画布上绘制原始位图*绘制画布*生成画布的位图
对我来说问题是,它必须在后台线程中完成而不绘制视图.我希望你能理解我.
我正在写一个自定义类.在实际绘制之前,需要为某些预处理和大小调整计算位图.位图本身是预处理的9补丁图像.在构造函数中,有以下代码:
BitmapFactory.Options bmpOptions = new BitmapFactory.Options();
bmpOptions.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), BITMAP_ID, bmpOptions);
Bitmap bmp = BitmapFactory.decodeResource(getResources(), BITMAP_ID);
Log.d(getClass().getSimpleName(), "width: " + bmp.getWidth() + " " + bmpOptions.outWidth + "; height: " + bmp.getHeight() + " " + bmpOptions.outHeight);
Run Code Online (Sandbox Code Playgroud)
运行Android 3.2.2的7"三星Galaxy Tab 7上的输出:
宽度:556 556; 身高:890 890
运行Android 4.1的10"Motorola Xoom上的输出:
宽度:556 556; 身高:890 890
运行Android 4.2.2的7"Nexus 7上的输出:
宽度:740 834; 高度; 1185 1335
位图的实际尺寸为:
mdpi:558 x 892
hdpi:836 x 1337
位图是预处理的9补丁,这就是尺寸偏离2个像素的原因.我无法理解为什么Nexus 7上的hdpi资产会产生很大的影响.
我也试过这些配置:
bmpOptions.inScaled = false;
Run Code Online (Sandbox Code Playgroud)
和
bmpOptions.inTargetDensity = getResources().getDisplayMetrics().densityDpi;
Run Code Online (Sandbox Code Playgroud)
和
bmpOptions.inTargetDensity …Run Code Online (Sandbox Code Playgroud) 我正在创建一个Android应用,您可以在其中将图像从一个位置复制到另一个x位置y.复制完成后,我希望看到图片中的图片ImageView.我知道图像的位置,但无论我尝试什么,我都无法创建bitmap它的对象.
造成我问题的那条线是这样的:
BitmapFactory.decodeFile(dir+s);
Run Code Online (Sandbox Code Playgroud)
dir = getCacheDir().getAbsolutePath()+"/images/";
s = file name (eg. 1275123123.jpg)
如果我创建一个File具有相同路径的对象,并调用f.isFile()它返回true.
在android或windows中打开图像都不是问题.
我有一个应用程序,您可以从库中选择图像或设备上的照片文件夹.选定文件的路径存储在Intent中,因此可以在Activities之间传递它们.我通过intent.getDataString()访问路径.
一旦我拥有了所有选定的图像路径,我将它们存储在ArrayList中并将其传递给ImageAdapter,以便在ListView中显示.
我正在设置FileNotFoundException,有没有人有任何想法?
提前致谢
马特.
import java.util.ArrayList;
import uk.co.mobilewebexpert.infowrapsynclibrary.ApplicationObj;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
public class QueuedImagesActivity extends Activity {
private static final String TAG = QueuedImagesActivity.class.getSimpleName();
private ImageAdapter adapter;
private ListView imageList;
ApplicationObj appObj;
Intent[] photos;
String path;
private ArrayList<String> imagePaths= new ArrayList<String>(); // Edit your code here..
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.image_listview);
appObj = (ApplicationObj) getApplication();
boolean includeBeingProcessed = true;
try {
photos = appObj.getQueuedPhotos(includeBeingProcessed); …Run Code Online (Sandbox Code Playgroud) 位图压缩过程需要太多时间。我该如何解决这个问题?
在活动中:
icon= BitmapFactory.decodeResource(getResources(),R.mipmap.image);
Run Code Online (Sandbox Code Playgroud)
在回调类中:
synchronized (holder) {
stream = new ByteArrayOutputStream();
Log.d("LIFE_CYCLE", "settingImage 1=" + System.currentTimeMillis());
icon.compress(Bitmap.CompressFormat.PNG, 100, stream);
Log.d("LIFE_CYCLE", "settingImage 2=" + System.currentTimeMillis());
byteArray = stream.toByteArray();
b = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
if(mWidth<mHeight){
icon= Bitmap.createScaledBitmap(b, (int)(mWidth*0.75), (int)(mWidth*0.75), false);
}
else{
icon= Bitmap.createScaledBitmap(b, (int)(mHeight*0.75), (int)(mHeight*0.75), false);
}
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
canvas.drawBitmap(icon, ((mWidth)-icon.getWidth())/2, (mHeight-icon.getHeight())/2, new Paint());
draw_target(canvas);
}
Run Code Online (Sandbox Code Playgroud)
此行大约需要 2 秒:
icon.compress(Bitmap.CompressFormat.PNG, 100, stream);
Run Code Online (Sandbox Code Playgroud)
PS我的图像是部分透明的,所以我需要使用.PNG而不是.JPG