Click here to Skip to main content
14,879,246 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
yesterday, i make a code how to send image to pc
here is my code...

upload_gambar = (ImageView) findViewById(;

this is button for uploading image
pengajuan.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (editText.getText().toString().length() == 0) {
                    editText.setError("Masukkan Tanggal!");
                } else if (keterangan.getText().toString().length() == 0) {
                    keterangan.setError("Masukkan Keterangan!");
                } else if (upload_gambar.getDrawable() == null) {
                    Toast.makeText(getApplicationContext(), "Upload gambar terlebih dahulu", Toast.LENGTH_SHORT).show();
                } else {
                    try {
                    } catch (IOException e) {

for choosing image
upload = (Button) findViewById(;
        upload.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                ActivityCompat.requestPermissions(sakit.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, CODE_GALLERY_REQUEST);

    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

        if (requestCode == CODE_GALLERY_REQUEST) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Intent intent = new Intent(Intent.ACTION_PICK);
                startActivityForResult(Intent.createChooser(intent, "Select Image"), CODE_GALLERY_REQUEST);
            } else {
                Toast.makeText(getApplicationContext(), "You don't have permission to access gallery!", Toast.LENGTH_LONG).show();
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CODE_GALLERY_REQUEST && resultCode == RESULT_OK && data != null) {
            Uri path = data.getData();
            try {
                InputStream inputStream = getContentResolver().openInputStream(path);
                bitmap = BitmapFactory.decodeStream(inputStream);
            } catch (IOException e) {
            Toast.makeText(sakit.this, "Gambar sudah diupload", Toast.LENGTH_SHORT).show();
        super.onActivityResult(requestCode, resultCode, data);

this is a it's command
 private void imagetoString(Bitmap bitmap) throws IOException {
        String gambar = nopengajuan.getText().toString();

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 50, baos);
        byte[] imageBytes = baos.toByteArray();
        String imageString = Base64.encodeToString(imageBytes, Base64.DEFAULT);

        imageBytes = Base64.decode(imageString, Base64.DEFAULT);
        Bitmap decodedImage = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
        FileOutputStream fos = new FileOutputStream ("C:\\Users\\ICT-PROGRAMER\\Pictures\\" + gambar + ".jpeg");

What I have tried:

- the imageview already encoded
- string encoded has been decoded
- the image doesn't appears in file
Updated 2-Nov-20 20:01pm

1 solution

Firstly, your naming conventions leave a lot to be desired: "imagetoString" accepts an image, but it doesn't return a string, and it actually tries to write it to a file rather then do anything with a string. Make your method names reflect what the code actually does, and you code be omes easier to read and work with.

Second, it's not at all obvious what you are actually doing, and what might be going wrong - you need to code running with your actual data in order to try working out what is happening and we just don't have access to either.

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
David Crow 3-Nov-20 12:06pm
"...send image to pc"

From where? If it's from an Android device, I do not think this will work:
FileOutputStream fos = new FileOutputStream ("C:\\Users\\ICT-PROGRAMER\\Pictures\\" + gambar + ".jpeg");
Can you confirm?
OriginalGriff 3-Nov-20 12:08pm
It won't. I'm assuming he has no idea what he is doing and has randomly found code on t'interwebz.
David Crow 3-Nov-20 12:10pm
"and has randomly found code on t'interwebz."

People do that?
Komang Putra 3-Nov-20 12:15pm
Yes it doesnt work
OriginalGriff 3-Nov-20 12:44pm
What does it do that you didn't expect, or not do that you did?
What have you tried to do to find out why?
Are there any error messages, and if so, where and when? What did you do to make them happen?

This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900