我的应用程序叫MyGram,是一个学校项目。我希望这个应用程序的功能像Instagram一样,每次你选择一张图片,它都应该显示在顶部。
我的密码是
package com.example.mygram;
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.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.example.mygram.utils.SpacingItemDecoder;
import java.util.ArrayList;
import java.util.Collections;
import de.hdodenhof.circleimageview.CircleImageView;
public class MainActivity extends AppCompatActivity {
private static final int IMAGE_PICK_CODE=1000;
private static final int PERMISSION_CODE=1001;
RecyclerView recyclerView;
TextView textView;
Button pick, gallery;
CircleImageView profpic;
ArrayList <Uri> uri = new ArrayList<>();
RecyclerAdapter adapter ;
private static final int Read_Permission = 101;
private static final int PICK_IMAGE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
profpic = findViewById(R.id.profile_image);
gallery = findViewById(R.id.select);
gallery.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
galleryActivityResultLauncher.launch(intent);
}
});
textView = findViewById(R.id.totalPhotos);
recyclerView = findViewById(R.id.recyclerView_Gallery_Images);
pick = findViewById(R.id.pick);
adapter = new RecyclerAdapter(uri);
recyclerView.setLayoutManager(new GridLayoutManager(MainActivity.this,3, LinearLayoutManager.VERTICAL, true));
SpacingItemDecoder itemDecorator = new SpacingItemDecoder(10);
recyclerView.addItemDecoration(itemDecorator);
recyclerView.setAdapter(adapter);
pick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, Read_Permission);
return;
}
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2){
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
}
//intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);
}
});
}
private ActivityResultLauncher<Intent> galleryActivityResultLauncher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
if (result.getResultCode() == Activity.RESULT_OK){
Intent data = result.getData();
Uri imageUri = data.getData();
profpic.setImageURI(imageUri);
}
else{
Toast.makeText(MainActivity.this, "Cancelled", Toast.LENGTH_SHORT).show();
}
}
}
);
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE && resultCode == RESULT_OK && null != data){
if(data.getClipData()!=null){
int countofImages = data.getClipData().getItemCount();
for(int i=0; i<countofImages; i++){
Uri imageuri = data.getClipData().getItemAt(i).getUri();
uri.add(imageuri);
}
adapter.notifyDataSetChanged();
textView.setText("");
}else{
Uri imageuri = data.getData();
uri.add(imageuri);
}
adapter.notifyDataSetChanged();
textView.setText("");
}else{
Toast.makeText(this, "You haven't pick any images", Toast.LENGTH_SHORT).show();
}
}
}
我想让我选择的图像出现在第一个框中,但是setstackfromend似乎不起作用。我试过反转集合,但是它没有任何作用。请帮助我。
我的应用程序是一个instagram启发的应用程序,所选的图像应该显示在顶部的第一个网格。
1条答案
按热度按时间lnvxswe21#
您应该调用
uri.add(0,imageuri);
以确保所选图像位于第一个位置。