I am putting 3 Fragments with their 3 corresponding xml with a background that is an image but they are not very big only 3 MB, but they have a resolution of approximately 4K and it gives me the following error
2019-07-29 22:36:11.418 21832-21832/com.example.images E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.images, PID: 21832 java.lang.RuntimeException: Unable to start activity ComponentInfo{ com.example.images/com.example.images.MainActivity}: android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class android.widget.RelativeLayout at android.app.ActivityThread. performLaunchActivity(ActivityThread.java:2665) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread. java:1477) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java: 886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) Caused by: android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class android.widget .RelativeLayout Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class android.widget.RelativeLayout Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Constructor.newInstance0(Native Method) at java.lang.reflect.Constructor.newInstance(Constructor.java:430) at android.view.LayoutInflater.createView(LayoutInflater.java:645) at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58) at android.view.LayoutInflater.onCreateView(LayoutInflater.java:717) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java :785) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727) at android.view.LayoutInflater.inflate(LayoutInflater.java:495) at android.view.LayoutInflater.inflate(LayoutInflater.java:426) at android .view.LayoutInflater.inflate(LayoutInflater.java:377) at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287) at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139 ) at com.example.images.MainActivity.onCreate(MainActivity.java:35) at android.app.Activity.performCreate(Activity.java:6679) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) at android.os. Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke (Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) Caused by: java.lang.OutOfMemoryError: Failed to allocate a 697250412 byte allocation with 4194304 free bytes and 377MB until OOM at dalvik.system.VMRuntime.newNonMovableArray(Native Method) at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:620) at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:455) at android.graphics.drawable.Drawable .createFromResourceStream(Drawable.java:1155) at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:720) at android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:571) at android.content.res. Resources.loadDrawable(Resources.java:858) at android.content.res.TypedArray.getDrawable(TypedArray.java:928) at android.view.View.(View.java:4202) at android.view.ViewGroup.(ViewGroup.java:579) at android. widget.RelativeLayout.(RelativeLayout.java:248) 2019-07-29 22:36:11.418 21832-21832/com.example.images E/AndroidRuntime: at android.widget.RelativeLayout.(RelativeLayout.java:244) at android .widget.RelativeLayout.(RelativeLayout.java:240) ... 25 more(RelativeLayout.java:244) at android.widget.RelativeLayout.(RelativeLayout.java:240) ... 25 more(RelativeLayout.java:244) at android.widget.RelativeLayout.(RelativeLayout.java:240) ... 25 more
Do I have to use Glide
?
Glide.with(getApplicationContext()).load(mBundle.getInt("Image")).into(mPlace);
How would I use it in a Fragment?
Here is my Fragment (the other 2 are the same):
public class PageFragment1 extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.page_1, container,false);
return rootView;
}
}
and my activity page_1.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/portada">
<TextView
android:id="@+id/file_type_logo_1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:fontFamily="@font/simonetta_black_italic"
android:text="Divertido"
android:textColor="#FFFFFF"
android:textSize="50sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_alignParentBottom="true">
<ImageView
android:id="@+id/file_type_logo_2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@drawable/downarrow" />
</RelativeLayout>
cover is the 4K image or I don't know why the app crashes when I launch it...
EDIT
MainActivity.java :
public class MainActivity extends AppCompatActivity {
private VerticalViewPager viewPager;
private PagerAdapter pagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.page_1);
List<Fragment> list = new ArrayList<>();
list.add(new PageFragment1());
list.add(new PageFragment2());
list.add(new PageFragment3());
viewPager = findViewById(R.id.pager);
pagerAdapter = new VPagerAdapter(getSupportFragmentManager(),list);
viewPager.setAdapter(pagerAdapter);
}
}
EDIT2:
Now it won't let me start the App, the error I have is the following and I don't know why:
2019-07-29 23:07:21.161 22572-22572/? E/cutils-trace: Error opening trace file: Permission denied (13) 2019-07-29 23:07:21.281 22572-22572/? E/memtrack: Couldn't load memtrack module (No such file or directory) 2019-07-29 23:07:21.281 22572-22572/? E/android.os.Debug: failed to load memtrack module: -2 2019-07-29 23:07:23.305 22583-22583/? E/cutils-trace: Error opening trace file: Permission denied (13) 2019-07-29 23:07:23.389 22583-22583/? E/memtrack: Couldn't load memtrack module (No such file or directory) 2019-07-29 23:07:23.389 22583-22583/? E/android.os.Debug: failed to load memtrack module: -2 2019-07-29 23:07:32.672 22596-22596/? E/cutils-trace: Error opening trace file: Permission denied (13) 2019-07-29 23:07:32.719 22596-22596/? E/memtrack: Couldn' t load memtrack module (No such file or directory) 2019-07-29 23:07:32.719 22596-22596/? E/android.os.Debug: failed to load memtrack module: -2 2019-07-29 23:07:36.569 22610-22610/? E/cutils-trace: Error opening trace file: Permission denied (13) 2019-07-29 23:07:36.608 22610-22610/? E/memtrack: Couldn't load memtrack module (No such file or directory) 2019-07-29 23:07:36.608 22610-22610/? E/android.os.Debug: failed to load memtrack module: -2 2019-07-29 23:07:50.991 22622-22622/? E/cutils-trace: Error opening trace file: Permission denied (13) 2019-07-29 23:07:51.030 22622-22622/? E/memtrack: Couldn't load memtrack module (No such file or directory) 2019-07-29 23:07:51.030 22622-22622/? E/android.os.Debug: failed to load memtrack module: -2 2019-07-29 23:07:51.259 22637-22637/? E/cutils-trace: Error opening trace file: Permission denied (13) 2019-07-29 23:07:51.298 22637-22637/? E/memtrack: Couldn't load memtrack module (No such file or directory) 2019-07-29 23:07:51.298 22637-22637/? E/android.os.Debug: failed to load memtrack module: -2 2019-07-29 23:08:13.788 22651-22651/? E/cutils-trace: Error opening trace file: Permission denied (13) 2019-07-29 23:08:13.837 22651-22651/? E/memtrack: Couldn't load memtrack module (No such file or directory) 2019-07-29 23:08:13.837 22651-22651/? E/android.os.Debug: failed to load memtrack module: -2 2019-07-29 23:08:31.742 22663-22663/? E/cutils-trace: Error opening trace file: Permission denied (13) 2019-07-29 23:08:31.785 22663-22663/? E/memtrack: Couldn't load memtrack module (No such file or directory) 2019-07-29 23:08:31.785 22663-22663/? E/android.os.Debug: failed to load memtrack module: -2 2019-07-29 23:08:42.659 22687-22687/? E/cutils-trace: Error opening trace file: Permission denied (13) 2019-07-29 23:08:42.680 22689-22689/? E/cutils-trace: Error opening trace file: Permission denied (13) 2019-07-29 23:08:42.733 22687-22687/? E/memtrack: Couldn't load memtrack module (No such file or directory) 2019-07-29 23:08:42.733 22687-22687/? E/android.os.Debug: failed to load memtrack module: -2 2019-07-29 23:08:42.758 22689-22689/? E/memtrack: Couldn't load memtrack module (No such file or directory) 2019-07-29 23:08:42.758 22689-22689/? E/android.os.Debug: failed to load memtrack module: -2 2019-07-29 23:08:43.141 22711-22711/? E/cutils-trace: Error opening trace file: Permission denied (13) 2019-07-29 23:08:43.186 22711-22711/? E/memtrack: Couldn' t load memtrack module (No such file or directory) 2019-07-29 23:08:43.186 22711-22711/? E/android.os.Debug: failed to load memtrack module: -2 2019-07-29 23:08:44.223 22723-22723/? E/cutils-trace: Error opening trace file: Permission denied (13) 2019-07-29 23:08:44.269 22723-22723/? E/memtrack: Couldn't load memtrack module (No such file or directory) 2019-07-29 23:08:44.270 22723-22723/? E/android.os.Debug: failed to load memtrack module: -2
edit3:
07-30 00:00:00.720 30496-30496/? E/ImageLoader: /storage/emulated/0/.CacheOfEUI/com.letv.android.note/cache/journal.tmp: open failed: ENOENT (No such file or directory) java.io.FileNotFoundException: /storage/emulated/ 0/.CacheOfEUI/com.letv.android.note/cache/journal.tmp: open failed: ENOENT (No such file or directory) at libcore.io.IoBridge.open(IoBridge.java:452) at java.io. FileOutputStream.(FileOutputStream.java:87) at java.io.FileOutputStream.(FileOutputStream.java:72) at com.nostra13.universalimageloader.cache.disc.impl.ext.DiskLruCache.rebuildJournal(DiskLruCache.java:358) at com .nostra13.universalimageloader.cache.disc.impl.ext.DiskLruCache.open(DiskLruCache.java:250) at com.nostra13.universalimageloader.cache.disc.impl.ext.LruDiskCache.initCache(LruDiskCache.java:112) at com .
It is definitely a memory consumption problem in fact when starting your application it indicates that it cannot inflate
page_1.xml
that is loaded by yourActivity
main.In addition, as I mentioned, you are using a pager where 3 fragments are regularly loaded, the one that is visible on the screen and the ones that are at the ends, if the images are not optimized, you will have the same problem.
To solve this problem consider:
• Move high resolution images from /drawable to /drawable-xxhdpi or /drawable-xxxhdpi This causes the operating system to scale the image down from a high density. If the images are added to /drawable and they are of high resolution, it will try to load a bitmap with these characteristics.
• In your
MainActivity
you do not have to load the layout that will load one of the Fragments, here you must define only the pager class (VerticalViewPager
) to which theFragments
:• I suggest you also use
Glide
to load the images as background of the layoutsSet background image in relative layout using
Glide
in AndroidDefine an id to your Layout
This would be an example loading the image through Glide in a Fragment: