Click here to Skip to main content
15,884,628 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
package com.example.xyz;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.github.barteksc.pdfviewer.PDFView;

import java.io.InputStream;

public class MainActivity extends AppCompatActivity {

    private static final int REQUEST_CODE_STORAGE_PERMISSION =1;
    private static final int PICKFILE_RESULT_CODE = 2;
    TextView textView;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


         textView = (TextView) findViewById(R.id.textView);

        findViewById(R.id.button1).setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View arg0)
            {

                if (ContextCompat.checkSelfPermission(
                        getApplicationContext(), Manifest.permission.READ_EXTERNAL_STORAGE
                ) != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(
                            MainActivity.this,
                            new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                            REQUEST_CODE_STORAGE_PERMISSION
                    );

                } else {
              

                    selectImage();
                }
            }
        });
    }



    public void onRequestPermissionResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults){
        super.onRequestPermissionsResult(requestCode, permissions ,grantResults);
        if(requestCode == REQUEST_CODE_STORAGE_PERMISSION && grantResults.length > 0){


            if(grantResults[0]== PackageManager.PERMISSION_GRANTED){

                selectImage();

            }else{
                Toast.makeText(this, "Permission denied!", Toast.LENGTH_SHORT).show();
            }
        }
    }
    private void selectImage()
    {
        Toast.makeText(this, "selectimage", Toast.LENGTH_SHORT).show();

        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);

        intent.setType("*/*");
        textView.setText("this works");

        startActivityForResult(intend,10);

    }
    protected void OnActivityResult(int requestCode, int resultCode, @Nullable Intent data){
    

            switch(requestCode){

                case 10:
                    if(resultCode==RESULT_OK){
                        textView.setText("no this not");
                    }
                    break;
            }
    
    }


}


What I have tried:

I have write the above code to pick a file and do something but it is not working. In the end i have used textview to check. The textview is changing text everwhere expect in
OnActivityResult
function.Can someone help to check what i am doing wrong.
Posted
Updated 22-Jun-20 5:24am
v2
Comments
David Crow 22-Jun-20 8:54am    
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
...
startActivityForResult(intend, 10);

While likely not the source of the problem, shouldn't these match?
Richard MacCutchan 22-Jun-20 10:32am    
I would think that is exactly the source of the problem. The intent never gets started so the relevant handler never gets called. And what you wrote is the solution.
David Crow 22-Jun-20 11:06am    
But it shouldn't have compiled, let alone run.
Richard MacCutchan 22-Jun-20 11:25am    
It is unclear whether the poster has actually managed a clean build. And so many do not know the difference between compile, link, execute.
David Crow 22-Jun-20 11:37am    
I assumed his "The textview is changing text everwhere expect OnActivityResult" meant otherwise. Oh well. 'Tis true about knowing the difference between compile and execute.

1 solution

You have also mis-spelled onActivityResult; see Activity  |  Android Developers[^]
 
Share this answer
 
Comments
Vivek Kansal 22-Jun-20 14:37pm    
thank you sir it is spelling mistake "o" .Now it is running

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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