简体   繁体   中英

onActivityResult is not getting called in MainActivity.Java

I am trying to make an app which shows a user selected image which can be set by double-clicking on the button. The app does ask me for an image (which is good) but I don't know how to set it on ImageView if this onActivityResult is not getting called.

MainActivity:

package com.forever.bobby.cheater20;

import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.tomerrosenfeld.customanalogclockview.CustomAnalogClock;

public class MainActivity extends AppCompatActivity {

    private static final int PICK_IMAGE = 100;
    Button hiddenOnClickListener;

    ImageView img;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        img = findViewById(R.id.imageView);
        CustomAnalogClock customAnalogClock = (CustomAnalogClock) findViewById(R.id.analog_clock);
        customAnalogClock.setAutoUpdate(true);
        hiddenOnClickListener = (Button) findViewById(R.id.touchButtonHidden);

        hiddenOnClickListener.setOnClickListener(new DoubleClickListener() {
            @Override
            public void onSingleClick(View v) {
                Log.e ("mPressesOnce -_-","..");
            }

            @Override
            public void onDoubleClick(View v) {    
                Log.e ("mPressesTwice -_-","..");
                openGallery();
            }
        });

    }

    private void openGallery () {
        Intent gallery = new Intent(Intent.ACTION_PICK,
                            MediaStore.Images.Media.INTERNAL_CONTENT_URI);
        startActivityForResult(gallery, 0);
    }

    @Override
    protected void onActivityResult (int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode,data);
        Log.e ("SHould Be Steeted","");

        if (requestCode == 0) {
            Uri imgUri;
            imgUri = data.getData();
            // img.setImageURI(imgUri);
        }
    }
}

AndroidManifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.forever.bobby.cheater20">
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <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=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

    </application>


</manifest>

Any Kind of help is appreciated as I am a newbie

With android sdk >= 23 , some permissions eg WRITE_EXTERNAL_STORE, READ_EXTERNAL_STORE, etc. you must request it in java code.

Therefore, when user starts using feature need permission, android will show a dialog and user can grant or cancel a permission. You can read more about it on developer.android.com .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM