I'm working on a small test code right now. It selects an image, resizes it to fit the screen based on the screen density. While it does this, it makes a copy of the image at the size it would be in an xxhdpi
display, and the converts into a Bitmap, then into a String. The String is then carried through an Intent to the next screen where the String is turned back into a Bitmap and then placed in an ImageButton
.
This worked fine until I added in a chunk of code to resize the image given by the string based on the density. The odd thing is I copied and pasted the DPI() method from another activity and there were no issues in that activity.
package com.example.zachary.imagetesting.Resizing;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Base64;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import com.example.zachary.imagetesting.R;
public class PictureDecoded extends Activity {
TextView Text;
ImageButton Image;
String string;
Bitmap picture;
public float DPI(){return getResources().getDisplayMetrics().density;}
float dpi = DPI() * 180;
int size = (int) dpi;
public Bitmap Pic(String string){
try{
byte[] b = Base64.decode(string, Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(b, 0, b.length);
return bitmap;
} catch (Exception e) {
e.getMessage();
return null;
}
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.resizing);
string = getIntent().getStringExtra("String");
Image = (ImageButton) findViewById(R.id.image);
Text = (TextView) findViewById(R.id.text);
picture = Pic(string);
Image.setImageBitmap(Bitmap.createScaledBitmap(picture,size,size,false));
}
public void next(View view){ }
// I also tried addring DPI() here as well to no success.
//public float DPI(){return getResources().getDisplayMetrics().density;}
}
01-15 19:55:17.104 30570-30570/com.example.zachary.imagetesting D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
01-15 19:55:17.194 30570-30570/com.example.zachary.imagetesting E/Bitmap String:: /9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEB
AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/2wBDAQEBAQEBAQEBAQEBAQEBAQEBAQEB
AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/wAARCAGVAhwDASIA
AhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQA
AAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3
ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWm
p6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEA
AwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSEx
BhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElK
U1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3
uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwDuKKKK
/wAVz/UgKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKK
ACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooA
KKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAo
oooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACii
igAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKK
ACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooA
KKKKACiiigAooooAKKKKACiiigA45Gem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reoUUUVIgooooAMf4Z5zjnv1/+sT1GQfDfH37Onwm+IMd7LqH
hq20fWLs7/8AhIfDqx6Vqqz5YmeZYonsNQd8gSHUrK6Yj7jo43V7lRXVg8bjMvrKvgcVXwtZW/eU
Ks6cmoybUZcskpwvq4T5oSTalFq97hOdN81OcoPvFtXt3s9V5PTvc/I34r/so+Pfh6l7rHh/d408
L2y+c93YW7R67YQguJG1HRlMrzRQgAveaZLdRiMNPcwWkQc19r/shaNcaR8DtFmuYmhfW9W17W1R
1KsYZL4aZbSEH+Ga20mKeMnJMcgOSDX03gf5/H/Pr05OKZHHHFGsUSpFGoCqqIERRuP3UQAAclsK
PzJ4+kzXjLMs6yWnlWYU6VSrTxVKu8bBeznVp06deHs6tGK9nzuVRTdSlyRaUYOjf94+mrjKlagq
NRJtSUvaLRtRUrJxStf3m7rva29/xq/akvY7749+P3iZWS2m0KyLD/n
01-15 19:55:17.194 30570-30570/com.example.zachary.imagetesting I/Timeline: Timeline: Activity_launch_request id:com.example.zachary.imagetesting time:50420468
01-15 19:55:17.244 30570-30570/com.example.zachary.imagetesting D/AndroidRuntime: Shutting down VM
01-15 19:55:17.244 30570-30570/com.example.zachary.imagetesting E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.zachary.imagetesting, PID: 30570
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.zachary.imagetesting/com.example.zachary.imagetesting.Resizing.PictureDecoded}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2546)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758)
at android.app.ActivityThread.access$900(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5942)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.content.ContextWrapper.getResources(ContextWrapper.java:90)
at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:74)
at com.example.zachary.imagetesting.Resizing.PictureDecoded.DPI(PictureDecoded.java:20)
at com.example.zachary.imagetesting.Resizing.PictureDecoded.<init>(PictureDecoded.java:21)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.Class.newInstance(Class.java:1650)
at android.app.Instrumentation.newActivity(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2536)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758)
at android.app.ActivityThread.access$900(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5942)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
The large block of text is the String converted from Bitmap that was passed to the Activity. I left it in just in case it was useful. The line before the large block of text was the button click that started the intent to the activity which crashes due to NullPointerException.
I can add the other activities on request, but since it worked before I tried resizing based on screen density I didn't feel it was needed.
An Activity
's base Context
won't have been setup until after onCreate()
has finished executing. Simply move the initialization of dpi
to onCreate()
after super.onCreate()
has been called.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.