onActivityResult 未在 MainActivity.Java 中被调用

onActivityResult is not getting called in MainActivity.Java

我正在尝试制作一个显示用户选择的图像的应用程序,可以通过双击按钮进行设置。该应用程序确实要求我提供图像(这很好),但如果未调用此 onActivityResult,我不知道如何在 ImageView 上设置它。

主要活动:

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>

我是新手,欢迎提供任何帮助

使用 android sdk >= 23,一些权限,例如WRITE_EXTERNAL_STORE, READ_EXTERNAL_STORE, etc. 您必须在 java 代码中请求它。

因此,当用户开始使用需要权限的功能时,android 将显示一个对话框,用户可以授予或取消权限。您可以在 developer.android.com.

上阅读更多相关信息