我需要将操作栏的背景更改为自定义图像,但每次我尝试使用此代码时都不会更改任何内容.
Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.navbarbg);
BitmapDrawable bd = new BitmapDrawable(getResources(), b);
bar.setBackgroundDrawable(bd);
Run Code Online (Sandbox Code Playgroud)
我也尝试了这段代码,但它没有用.
Resources res = getResources();
xpp =res.getXml(R.drawable.actionbar_background);
bitmapDrawable = (BitmapDrawable) BitmapDrawable.createFromXml (res, xpp);
Run Code Online (Sandbox Code Playgroud)
actionbar_background.xml的内容是
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/navbarbg"
android:tileMode="repeat" />
Run Code Online (Sandbox Code Playgroud)
编辑:我用这个代码工作正常:
Bitmap bMap = BitmapFactory.decodeResource(res, R.drawable.action_bar_bg);
BitmapDrawable actionBarBackground = new BitmapDrawable(res, bMap);
ActionBar bar = getActionBar();
bar.setBackgroundDrawable(actionBarBackground);
Run Code Online (Sandbox Code Playgroud) 我有这个代码裁剪图像表的一部分.
问题是当我使用BitmapFactory.decodeResource和记录宽度和高度时,大小与原始大小不同.
Bitmap spriteSheet = BitmapFactory.decodeResource(context.getResources(), R.drawable.imageSheet);
Log.i("Sprite Sheet Size",spriteSheet.getWidth()+"w "+spriteSheet.getHeight()+"h");
Run Code Online (Sandbox Code Playgroud)
这是日志,1536w 330h但是drawable(png)的原始大小是1024x220
如何将我的资源解码为原始大小的位图.
我尝试在我的Activity中使用类BitmapFactory中的方法decodeResource,如下所示:
Bitmap image = new BitmapFactory.decodeResource(getResources(),R.drawable.my_image);
Run Code Online (Sandbox Code Playgroud)
我做了所有必要的导入,IntelliJ甚至为我填写方法,因为我输入它但是当我尝试编译它时给了我错误:
java: cannot find symbol
symbol: class decodeResource
location: class android.graphics.BitmapFactory
Run Code Online (Sandbox Code Playgroud) 我有处理相机的片段.我的问题是它需要一张照片并将其显示在imageView上,就好了.
我第二次尝试拍摄一张OutOfMemory错误.任何提示如何在第一张照片后释放内存?
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
System.out.println("tesstssaffsafdfsdfsd");
switch (requestCode) {
case TAKE_PICTURE:
if (resultCode == Activity.RESULT_OK) {
getActivity().getContentResolver().notifyChange(mUri, null);
ContentResolver cr = getActivity().getContentResolver();
try {
mPhoto = android.provider.MediaStore.Images.Media.getBitmap(cr, mUri);
((ImageView)rootView.findViewById(R.id.snap)).setImageBitmap(mPhoto);
} catch (Exception e) {
Toast.makeText(getActivity(), e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是堆栈跟踪:
08-21 16:33:27.450: E/AndroidRuntime(1840): FATAL EXCEPTION: main
08-21 16:33:27.450: E/AndroidRuntime(1840): java.lang.OutOfMemoryError
08-21 16:33:27.450: E/AndroidRuntime(1840): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
08-21 16:33:27.450: E/AndroidRuntime(1840): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:650)
08-21 16:33:27.450: E/AndroidRuntime(1840): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:722)
08-21 16:33:27.450: …Run Code Online (Sandbox Code Playgroud) 根据文件Bitmap createBitmap (Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter)方法:
从源位图的子集返回不可变位图,由可选矩阵转换.新位图可以是与源相同的对象,也可以是副本.它使用与原始位图相同的密度进行初始化.如果源位图是不可变的并且请求的子集与源位图本身相同,则返回源位图并且不创建新的位图.
我有一个方法,将方向应用于现有的位图:
private Bitmap getOrientedPhoto(Bitmap bitmap, int orientation) {
int rotate = 0;
switch (orientation) {
case ORIENTATION_ROTATE_270:
rotate = 270;
break;
case ORIENTATION_ROTATE_180:
rotate = 180;
break;
case ORIENTATION_ROTATE_90:
rotate = 90;
break;
default:
return bitmap;
}
int w = bitmap.getWidth();
int h = bitmap.getHeight();
Matrix mtx = new Matrix();
mtx.postRotate(rotate);
return Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true);
}
Run Code Online (Sandbox Code Playgroud)
我是从这里打电话的: …
基本上我是为Android 4.4.2锁屏背景添加一个壁纸选择器,当图像设置后我关闭屏幕然后重新开启以查看锁屏我的屏幕变黑并且logcat给我一个内存不足分配错误.到目前为止,我已经尝试使用Bitmap decodeFile(String pathName),我也重新使用Bitmap decodeFile(String pathName,Options opts),但结果是每次都相同...
以下是用于设置图像的原始方法:
private static final String WALLPAPER_IMAGE_PATH =
"/data/data/com.android.settings/files/lockscreen_wallpaper.png";
private KeyguardUpdateMonitorCallback mBackgroundChanger = new KeyguardUpdateMonitorCallback() {
@Override
public void onSetBackground(Bitmap bmp) {
if (bmp != null) {
mKeyguardHost.setCustomBackground(
new BitmapDrawable(mContext.getResources(), bmp));
}
else {
File file = new File(WALLPAPER_IMAGE_PATH);
if (file.exists()) {
mKeyguardHost.setCustomBackground(
new BitmapDrawable(mContext.getResources(), WALLPAPER_IMAGE_PATH));
}
else {
mKeyguardHost.setCustomBackground(null);
}
}
updateShowWallpaper(bmp == null);
}
};
Run Code Online (Sandbox Code Playgroud)
这是从案例1中调用的:
public void setCustomBackground(Drawable d) {
if (!mAudioManager.isMusicActive()) {
int mBackgroundStyle = Settings.System.getInt(mContext.getContentResolver(),
Settings.System.LOCKSCREEN_BACKGROUND_STYLE, 2);
int mBackgroundColor …Run Code Online (Sandbox Code Playgroud) 今天我的应用程序崩溃,内存不足错误.
android.graphics.BitmapFactory.nativeDecodeAsset中的java.lang.OutOfMemoryError
我只使用Bitmap Factory为我的操作栏制作了一个背景
代码:
BitmapDrawable background = new BitmapDrawable (BitmapFactory.decodeResource(getResources(), R.drawable.actionbar));
background.setTileModeX(android.graphics.Shader.TileMode.REPEAT);
actionbar.setBackgroundDrawable(background);
Run Code Online (Sandbox Code Playgroud)
活动开始时不会发生此错误,它会在更改活动后发生.
有人可以告诉我如何解决这个问题
编辑编辑
以下是开发人员控制台中的错误消息:
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3838)
at android.view.View.performClick(View.java:4475)
at android.view.View$PerformClick.run(View.java:18786)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5419)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at android.view.View$1.onClick(View.java:3833)
... 11 more
Caused by: java.lang.OutOfMemoryError
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:596)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:832)
at android.content.res.Resources.loadDrawable(Resources.java:2988)
at …Run Code Online (Sandbox Code Playgroud) 我使用以下方法从URL检索位图并将其传递给imageview,但imageview没有更新.
public static Bitmap LoadImageFromWebOperations(String url) {
Bitmap bitmap;
try {
InputStream is = new URL(url).openStream();
bitmap = BitmapFactory.decodeStream(is);
return bitmap;
} catch (Exception e) {
return null;
}
Run Code Online (Sandbox Code Playgroud)
电话 -
mov1_poster.setImageBitmap(VPmovies.LoadImageFromWebOperations(mov_details[0][7]));
//doesn't work
Toast.makeText(this,"url is \n"+mov_details[0][7],Toast.LENGTH_LONG).show();
// shows the url of the image successfully (just to check the url is not null)
Run Code Online (Sandbox Code Playgroud)
我有什么问题吗?请帮忙.
我正在尝试显示通过存储的相机意图(触发内置相机应用程序)捕获的图片external SD-card.
文件路径和文件名存储在String变量中'mCurrentPhotoPath'.
我已经验证捕获的图像存储在'mCurrentPhotoPath'使用ES File Explorerapp 指定的位置,但BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions)始终返回FileNotFoundException.这是我存储捕获图像的代码:
private void addPicToGallery() {
File f = new File(mCurrentPhotoPath);
//for debug purpose only
Log.i(MYTAG, "value of mCurrentPhotoPath in addPicToGallery: " +mCurrentPhotoPath);
Log.i(MYTAG, "value of f in addPicToGallery: " +f);
Uri contentUri = Uri.fromFile(f);
//for debug only
Log.i(MYTAG, "value of contentUri in addPicToGallery: " +contentUri);
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
mediaScanIntent.setData(contentUri);
sendBroadcast(mediaScanIntent);
}
Run Code Online (Sandbox Code Playgroud)
解码缩放图像的代码,用于显示ImageView:
private void setFullImageFromFilePath() {
Log.i(MYTAG, …Run Code Online (Sandbox Code Playgroud) 我看到我的一个用户遇到了崩溃.我怎么知道这次事故发生的时间?它发生了哪些活动?哪行代码?
我不在任何地方使用Bitmap工厂,但我会在ImageView中显示一些图片.
以下是Firebase控制台中显示的崩溃:
Fatal Exception: java.lang.OutOfMemoryError
at android.graphics.Bitmap.nativeCreateFromParcel(Bitmap.java)
at android.graphics.Bitmap.access$000(Bitmap.java:31)
at android.graphics.Bitmap$1.createFromParcel(Bitmap.java:1308)
at android.graphics.Bitmap$1.createFromParcel(Bitmap.java:1300)
at android.app.IUiAutomationConnection$Stub$Proxy.takeScreenshot(IUiAutomationConnection.java:224)
at android.app.UiAutomation.takeScreenshot(UiAutomation.java:599)
at com.google.android.apps.mtaas.crawler.platform.common.Utils.takeScreenshot(Utils.java:42)
at com.google.android.apps.mtaas.crawler.platform.hybrid.ViewScreenStateBuilder.traverseAndBuild(ViewScreenStateBuilder.java:41)
at com.google.android.apps.mtaas.crawler.platform.hybrid.HybridStateExtractor.tryExtractingScreenState(HybridStateExtractor.java:40)
at com.google.android.apps.mtaas.crawler.platform.hybrid.HybridStateExtractor.getStableScreen(HybridStateExtractor.java:8)
at com.google.android.apps.mtaas.crawler.controller.remote.RemotePlatform.handlePerformScrape(RemotePlatform.java:40)
at com.google.android.apps.mtaas.crawler.controller.remote.RemotePlatform.access$400(RemotePlatform.java:81)
at com.google.android.apps.mtaas.crawler.controller.remote.RemotePlatform$ControllerMessageHandler.handleMessage(RemotePlatform.java:10)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.os.HandlerThread.run(HandlerThread.java:61)
Run Code Online (Sandbox Code Playgroud)
我自己从未遇到过这次撞车事故 我可以看到它发生在哪个设备上,(Galaxy S3),所以我在模拟器中尝试过它,一切正常.
谢谢
android ×10
bitmapfactory ×10
bitmap ×6
background ×1
crashlytics ×1
firebase ×1
image ×1
java ×1
url ×1