• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Bitmap create

Okay I've been working on this app for about a week now and I've just about got everything worked out. The only problem I have left is once I take the picture I need to resize it, but I can't seem to figure out what I'm doing wrong. When I tried to use the BitmapFactory to get the file everything looks like it's working fine until it gets to the Bitmap.createScaledBitmap portion of the resized function. If force closes when it gets to that line. Any help with this resize function would be greatly appreciated.

Code:
webView.setWebChromeClient(new WebChromeClient() { 
            //Capture image with camera on Android > 3.0 
            @SuppressWarnings("unused")
               public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
            openFileChooser(uploadMsg);
             }

            //Capture image with camera on Android 3.0+ 
            @SuppressWarnings("unused")
            public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
                openFileChooser(uploadMsg);
            }

            //Capture image with camera on Android 4.1 to 4.3 
            public void openFileChooser(ValueCallback<Uri> uploadMsg) {
                mUploadMessage = uploadMsg;
                File imageStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "Mobile311_Citizen");
                fullUrl = imageStorageDir + File.separator + "IMG_" + String.valueOf(System.currentTimeMillis()) + ".jpg";
                file = new File(fullUrl);
                Intent capture = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                capture.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
                MainActivity.this.startActivityForResult(capture, CAPTURE_RESULTCODE);
                
            }

Code:
@Override
     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
           // TODO Auto-generated method stub
         super.onActivityResult(requestCode, resultCode, data);
         if(resultCode == RESULT_OK){
             resize(fullUrl);
             if(null == this.mUploadMessage){
                 this.mUploadMessage.onReceiveValue(null);
             }else{
                 ContentValues values = new ContentValues();
                 values.put(MediaStore.Images.Media.DATA, fullUrl);
                 this.mUploadMessage.onReceiveValue(this.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values));
            }
             this.mUploadMessage = null;
         }
     }

Code:
private Bitmap resize(String path){
        // create the options
            BitmapFactory.Options opts = new BitmapFactory.Options();

        //just decode the file
            opts.inJustDecodeBounds = true;
            Bitmap bp = BitmapFactory.decodeFile(path, opts);

        //get the original size
            int orignalHeight = opts.outHeight;
            int orignalWidth = opts.outWidth;
            
            if(orignalWidth > 1000) {
                int x = orignalWidth / 2;
                int y = orignalHeight / 2;
                Bitmap scaled = Bitmap.createScaledBitmap(bp, x, y, true);
                bp.recycle();
                bp = scaled;
                } 
            return bp;
    }
 
So I was able to figure out where the issue was today at work. I'll try and post the updated fixed code. The first thing I had to do was change the true to a false when it came to the opts.inJustDecodeBounds. The second thing I had to do was make sure that I was creating the new bitmap and saving it back to the sd card. Then I just had to provide that path as the file to be uploaded to the server.
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones