looking for some solutions? You are welcome.

SOLVED: Works in emulator but not in real device

Bailey Bash:

METHODS

 private static String  UID;
private void getID(String MessageRecieverID) {
        UID = MessageRecieverID; // Assigning a value;
    }

public static String getOnClickID() {
        return UID;
    }


public void saveImage(Context context, Bitmap b, String imageName) {
    FileOutputStream foStream;
    try {
        foStream = context.openFileOutput(imageName, Context.MODE_PRIVATE);
        b.compress(Bitmap.CompressFormat.PNG, 100, foStream);
        foStream.close();
        Log.d("saveImage", "SUCCESS");

    } catch (Exception e) {
        Log.d("saveImage", "Exception 2, Something went wrong!");
        e.printStackTrace();
    }
}



private class DownloadImage extends AsyncTask<String, Void, Bitmap> {
    private Bitmap downloadImageBitmap(String sUrl) {
        Bitmap bitmap = null;
        try {
            InputStream inputStream = new URL(sUrl).openStream();   // Download Image from URL
            bitmap = BitmapFactory.decodeStream(inputStream);       // Decode Bitmap
            inputStream.close();
            Log.d("saveImage", "SUCCESSFULL DOWNLOAD");

        } catch (Exception e) {
            Log.d("saveImage", "Exception 1, Something went wrong!");
            e.printStackTrace();
        }
        return bitmap;
    }

    @Override
    protected Bitmap doInBackground(String... params) {
        return downloadImageBitmap(params[0]);
    }

    @Override
    protected void onPostExecute(Bitmap result) {
        String MessageRecieverID = Search.getOnClickID();
        Log.d("AAAAAAA",MessageRecieverID);
        saveImage(getActivity(), result, MessageRecieverID+".jpg");
        Log.d("saveImage", "SUCCESSFULL SAVE");

    }
}



public Bitmap loadImageBitmap(Context context, String imageName) {
    Bitmap bitmap = null;
    FileInputStream fiStream;
    try {
        fiStream    = context.openFileInput(imageName);
        bitmap      = BitmapFactory.decodeStream(fiStream);
        fiStream.close();
        Log.d("saveImage", "DOOWNLOADED");
    } catch (Exception e) {
        Log.d("saveImage123", e.getMessage());
        e.printStackTrace();
    }
    return bitmap;
}


static String convert(String str) {
    char ch[] = str.toCharArray();
    for (int i = 0; i < str.length(); i++) {
        if (i == 0 && ch[i] != ' ' ||
                ch[i] != ' ' && ch[i - 1] == ' ') {
            if (ch[i] >= 'a' && ch[i] <= 'z') {
                ch[i] = (char)(ch[i] - 'a' + 'A');
            }
        }
        else if (ch[i] >= 'A' && ch[i] <= 'Z')
            ch[i] = (char)(ch[i] + 'a' - 'A');
    }
    String st = new String(ch);
    return st;
}

Activity Code

public void onDataChange(DataSnapshot dataSnapshot) {
final String userName = dataSnapshot.child("Name").getValue().toString();
final String userStatus = dataSnapshot.child("Status").getValue().toString();
String userImage = dataSnapshot.child("Image").getValue().toString();

if (!userImage.equals("Default")) {

new DownloadImage().execute(userImage);

     File file = getActivity().getFileStreamPath(uid + ".jpg");
     String imageFullPath = file.getAbsolutePath();

     Log.d("YDTJVYJGJFBUGN2", String.valueOf(file));

     holder.mButton.setImageBitmap(loadImageBitmap(getActivity(), uid + ".jpg"));


                byte[] thumb = new byte[0];
                   try {
                        Bitmap thumb_bitmap = new Compressor(getActivity())
                               .setMaxWidth(75)
                               .setMaxHeight(75)
                               .setQuality(50)
                               .compressToBitmap(file);
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                thumb_bitmap.compress(Bitmap.CompressFormat.JPEG, 75, baos);
                thumb = baos.toByteArray();
            }catch (IOException e) {
                  e.printStackTrace();
       }

Error on this Line .compressToBitmap(file);

Error

java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference
                                                   at id.zelory.compressor.ImageUtil.decodeSampledBitmapFromFile(ImageUtil.java:70)
                                                   at id.zelory.compressor.Compressor.compressToBitmap(Compressor.java:65)
                                                   at com.appmaster.akash.messageplus.Search$3$1$1.onDataChange(Search.java:225)
                                                   at com.google.android.gms.internal.zzduz.zza(Unknown Source)
                                                   at com.google.android.gms.internal.zzdwu.zzbvb(Unknown Source)
                                                   at com.google.android.gms.internal.zzdxa.run(Unknown Source)
                                                   at android.os.Handler.handleCallback(Handler.java:742)
                                                   at android.os.Handler.dispatchMessage(Handler.java:95)
                                                   at android.os.Looper.loop(Looper.java:157)
                                                   at android.app.ActivityThread.main(ActivityThread.java:5571)
                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:745)
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:635)

This makes it weird because this very code works totally fine in emulator. It downloads the data saves ait and fetches it without any problem but not working in real device. Have I missed out something for it to work on a real device?

According to the error the file fath not doesn't exists

Logcat Output

/data/user/0/com.appmaster.akash.messageplus/files/7vqqeDNMeRaAarVxgvLHoY61lHV2.jpg: open failed: ENOENT (No such file or directory)

Why is this happening only in real device and not in emulator? Someone please help me solve this issue. Thanks



Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots
Share:

No comments:

Recent