1

I have already checked the solutions mentioned here, but my app doesn't have most of those settings, including the manifest one. The ones I could try changing, I did. Still no luck.

Below is my code, and this is part of one of my tabs in TabHost. I am able to choose an image from gallery, but any statement after that isn't getting executed. No sysout statements are printed.

All my variables, except for the Button, are static. Could that be a problem?

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message);

        int permission = ActivityCompat.checkSelfPermission(messageActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
        if (permission != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(
                    messageActivity.this,
                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    101
            );
        }
        databaseRef = FirebaseDatabase.getInstance().getReference();
        imgStorageRef = FirebaseStorage.getInstance().getReference();
        imgRef = databaseRef.child("images");
        user = MainActivity.sendUser();
        et = findViewById(R.id.msg);
        et.setText("");
        send = findViewById(R.id.sendBtn);
        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
                startActivityForResult(gallery, PICK_IMAGE);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        System.out.println("Entered onActivityResult");
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == PICK_IMAGE){
            imageUri = data.getData();
            try {
                bmp = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
                bmp=bmp.copy(Bitmap.Config.ARGB_8888 , true);
                encode();
                System.out.println("Encoded");
                upload();
                System.out.println("Uploaded");
                /*Code continues...*/

            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

This is the manifest file :

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".messageActivity"/>
        <activity android:name=".chatActivity" />
        <activity android:name=".usersActivity" />
        <activity android:name=".afterRegister" />
        <activity android:name=".activityTwo" />
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

What's surprising is that, this exact same code works for me in all apps. But most of them are just 2 actvities long. Over here I've included it within a TabHost's activity, but based on my internet search, that shouldn't be a problem.

Dinesh
  • 1,410
  • 2
  • 16
  • 29
SonOfEl
  • 175
  • 1
  • 10

3 Answers3

0

Did you add the necessary permissions in the Manifest file?

idobarabi
  • 63
  • 4
0

test this code :

Intent intent = new Intent(Intent.ACTION_PICK);
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
startActivityForResult(intent, PICK_IMAGE);

specially this :

MediaStore.Images.Media.EXTERNAL_CONTENT_URI
mohosyny
  • 962
  • 1
  • 9
  • 19
0

Try this to pick:

Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, PICK_IMAGE);

For testing purpose:

If you are on android 6 and higher go to the apps manager on the device and then go to permissions and check if they are enabled read and write.

For reality:

If you are on android 6 and higher you must add runtime permissions instead of doing it manual.

more details here

Hasan Bou Taam
  • 4,017
  • 2
  • 12
  • 22
  • Yeah, I've included those lines in the manifest. I've included the manifest code. I'm also checking for those permissions within the code, as shown. – SonOfEl May 26 '20 at 13:10
  • Also as I said if you are running android 6 and higher, go to device app manager and enable permissions in your app for testing purpose. – Hasan Bou Taam May 26 '20 at 13:15
  • Thanks for the suggestion, sadly even that doesn't work. Just tried it. – SonOfEl May 26 '20 at 13:17
  • Can you tell me if you are running a device android 6 or higher. – Hasan Bou Taam May 26 '20 at 13:18
  • My emulator is running android 7.0 – SonOfEl May 26 '20 at 13:23
  • Okay look go to the app manager on the device and find your app, you know I mean the place where you clear cache and uninstall the app. – Hasan Bou Taam May 26 '20 at 13:24
  • And click on permissions of the app, and make sure to enable permissions read and write. – Hasan Bou Taam May 26 '20 at 13:24
  • Yes, I did that. Storage permission is enabled. – SonOfEl May 26 '20 at 13:30
  • private static int PICK_IMAGE = 100; private static int random=0; private static Uri imageUri; private static Bitmap bmp; static int currentPos = 3; private static EditText et; Button send; static DatabaseReference databaseRef; static DatabaseReference imgRef; static StorageReference imgStorageRef; static String user=""; FirebaseUser fUser; – SonOfEl May 26 '20 at 13:33