MainActivity.Java中未调用onActivityResult

时间:2018-08-15 14:00:13

标签: java android onactivityresult

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

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>

我是新手,感谢您的任何帮助

1 个答案:

答案 0 :(得分:0)

使用android sdk >= 23,某些权限例如WRITE_EXTERNAL_STORE, READ_EXTERNAL_STORE, etc.必须使用Java代码请求。

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