Erfahrungen mit chatGPT ?

  • Habt Ihr Erfahrung mit der KI chatGPT ?


    Meine Frage im primitiven Englisch : "show android java code for pick image from gallery without startActivityForResult"


    brachte, so denke ich, ein brauchbares Eregbnis:


    import android.Manifest;

    import android.app.Activity;

    import android.content.ContentResolver;

    import android.content.Intent;

    import android.content.pm.PackageManager;

    import android.net.Uri;

    import android.os.Bundle;

    import android.provider.MediaStore;

    import android.widget.Toast;


    import androidx.activity.result.ActivityResult;

    import androidx.activity.result.ActivityResultCallback;

    import androidx.activity.result.ActivityResultLauncher;

    import androidx.activity.result.contract.ActivityResultContracts;

    import androidx.annotation.NonNull;

    import androidx.appcompat.app.AppCompatActivity;

    import androidx.core.app.ActivityCompat;

    import androidx.core.content.ContextCompat;


    public class MainActivity extends AppCompatActivity {


    private static final int REQUEST_PERMISSION = 1;

    private static final int REQUEST_IMAGE_PICK = 2;


    private ActivityResultLauncher<String> requestPermissionLauncher;

    private ActivityResultLauncher<Intent> pickImageLauncher;


    @Override

    protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);


    requestPermissionLauncher = registerForActivityResult(new ActivityResultContracts.RequestPermission(),

    isGranted -> {

    if (isGranted) {pickImage();

    } else {

    Toast.makeText(this, "Permission denied", Toast.LENGTH_SHORT).show();

    }

    });

    pickImageLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),

    new ActivityResultCallback<ActivityResult>() {

    @Override

    public void onActivityResult(ActivityResult result) {

    if (result.getResultCode() == Activity.RESULT_OK) {

    Intent data = result.getData();

    if (data != null) {

    Uri imageUri = data.getData();

    // Do something with the image URI

    }

    }

    }

    });


    // Request permission to access external storage

    if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {

    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},REQUEST_PERMISSION);

    } else {

    pickImage();

    }

    }


    private void pickImage() {

    Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

    pickImageLauncher.launch(intent);

    }


    @Override

    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,@NonNull int[] grantResults) {

    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    if (requestCode == REQUEST_PERMISSION) {

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

    pickImage();

    } else {

    Toast.makeText(this, "Permission denied", Toast.LENGTH_SHORT).show();

    }

    }

    }

    }


    In this code, we first check if the READ_EXTERNAL_STORAGE permission is granted. If not, we request it using ActivityCompat.requestPermissions(). If the permission is already granted, or once the user grants the permission, we launch the image picker using pickImageLauncher.launch(intent).


    When the user selects an image from the gallery, the onActivityResult() method will be called, and you can retrieve the image URI from the returned Intent.


    Note that you need to have the READ_EXTERNAL_STORAGE permission declared in your app's manifest file.

Jetzt mitmachen!

Sie haben noch kein Benutzerkonto auf unserer Seite? Registrieren Sie sich kostenlos und nehmen Sie an unserer Community teil!