-1

I have an activity where I want the user to press a view and take a picture several places and theyre then inserted for each place he "presses". Like a photo folder.

So I have made a class to handle taking a picture and making a bitmap.

But I cannot figure out how to decode the bitmap in the activity I need it, I just get a static cannot be referenced from non-static error, because Im not instantiating it properly I guess.

Heres my approach:

            case R.id.pButton2:
                new CameraActivity();
                Bitmap bitmap = BitmapFactory.decodeStream(CameraActivity.openFileInput("myImage"));
                mImageView = (ImageView)findViewById(R.id.imageView2);
                mImageView.setImageBitmap(bitmap);
                mImageView.setRotation(180);
                mImageView.setVisibility(View.VISIBLE);
                break;

Calling this class:

public class CameraActivity extends Activity{
    private String mCurrentPhotoPath;
    private ImageView mImageView;
    private Bitmap mImageBitmap;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        dispatchTakePictureIntent();
    }
    public void dispatchTakePictureIntent() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        // Ensure that there's a camera activity to handle the intent
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            // Create the File where the photo should go
            File photoFile = null;
            try {
                photoFile = createImageFile();
            } catch (IOException ex) {
                // Error occurred while creating the File
                System.out.println("ERR CANNOT CREATE FILE");
            }
            // Continue only if the File was successfully created
            if (photoFile != null) {
                Uri photoURI = FileProvider.getUriForFile(this,
                        "com.example.android.fileprovider",
                        photoFile);
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
            }
        }
    }
    private File createImageFile() throws IOException {
        // Create an image file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";
        File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);

        File image = File.createTempFile(
                imageFileName,  /* prefix */
                ".jpg",         /* suffix */
                storageDir      /* directory */
        );

        // Save a file: path for use with ACTION_VIEW intents
        mCurrentPhotoPath = "file:" + image.getAbsolutePath();
        return image;
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
            try {
                mImageBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse(mCurrentPhotoPath));
                createImageFromBitmap(mImageBitmap);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public String createImageFromBitmap(Bitmap bitmap) {
        String fileName = "myImage";//no .png or .jpg needed
        try {
            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
            FileOutputStream fo = openFileOutput(fileName, Context.MODE_PRIVATE);
            fo.write(bytes.toByteArray());
            // remember close file output
            fo.close();
        } catch (Exception e) {
            e.printStackTrace();
            fileName = null;
        }
        return fileName;
    }
}

Im using a sample from this; how do you pass images (bitmaps) between android activities using bundles?

Community
  • 1
  • 1
NicklasN
  • 334
  • 1
  • 6
  • 18

1 Answers1

0

Delete new CameraActivity().

Most likely, you can then replace:

Bitmap bitmap = BitmapFactory.decodeStream(CameraActivity.openFileInput("myImage"));

with:

Bitmap bitmap = BitmapFactory.decodeStream(openFileInput("myImage"));

since this case seems to be UI code and therefore probably is in an activity already. If it is in a fragment, use getActivity().openFileInput(). If it is somewhere else use mImageView.getContext().openFileInput().

CommonsWare
  • 910,778
  • 176
  • 2,215
  • 2,253
  • @NicklasN: Nobody can help you if you limit your explanations to statements like "none of it works". Explain, in detail, what "none of it works" means, in terms of what you tried and what specific problems you encountered. – CommonsWare Oct 30 '16 at 18:03
  • Sorry, I know its a bad reply. I tried replacing as you suggested. That gives me fileNotFoundException. Its not in a fragment, I tried extending fragmentactivity to no prevail, and mImageView.getContext gives the same static non static error. – NicklasN Oct 30 '16 at 18:05
  • I dont know how I provide the code with the correct context when it wont take an intent – NicklasN Oct 30 '16 at 18:06
  • @NicklasN: "That gives me fileNotFoundException" -- presumably, you have not saved the file there. You only create this file if the user visits `CameraActivity`. – CommonsWare Oct 30 '16 at 18:07
  • Thats true, yes, it only creates a file when the user goes to the activity, on that switch. Thats why I tried with an intent – NicklasN Oct 30 '16 at 18:09
  • @NicklasN: "when the user goes to the activity, on that switch" -- you are not going to the activity in that `case` block. You are trying to read the image in that `case` block. If you want to start the activity in that `case` block, delete all the code that you have there and call `startActivity()` to start `CameraActivity`. You can call `startActivity()` on whatever `Context` you used for your repaired call to `openFileInput()`, as both `openFileInput()` and `startActivity()` are methods on `Context`. – CommonsWare Oct 30 '16 at 18:13
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/127006/discussion-between-nicklasn-and-commonsware). – NicklasN Oct 30 '16 at 18:17