【问题标题】:having same name in all text view when selecting image different images选择图像不同图像时在所有文本视图中具有相同名称
【发布时间】:2021-10-30 16:29:34
【问题描述】:

我在从图库中选择不同图像时尝试设置不同的名称,但只选择一个名称并在所有文本视图中设置..

public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            if (requestCode == SELECT_PICTURE) {
                selectedImageUri = data.getData();
                imageName = selectedImageUri.getPath();

                if (mbinding.includeMainUploadIdcard.filenamePancard.getText().toString().equals("")) {
                    mbinding.includeMainUploadIdcard.filenamePancard.setText(imageName);
                    mbinding.includeMainUploadIdcard.selectPancard.setVisibility(View.VISIBLE);
                }

                if (mbinding.includeMainUploadIdcard.filenameAdharcard.getText().toString().equals("")) {
                    mbinding.includeMainUploadIdcard.filenameAdharcard.setText(imageName);
                    mbinding.includeMainUploadIdcard.selectAdhar.setVisibility(View.VISIBLE);

                }

                if (mbinding.includeMainUploadIdcard.filenameElectricityBill.getText().toString().equals("")) {
                    mbinding.includeMainUploadIdcard.filenameElectricityBill.setText(imageName);
                    mbinding.includeMainUploadIdcard.selectBill.setVisibility(View.VISIBLE);

                }
}
}

【问题讨论】:

  • 想从选中的图片中获取图片名称?

标签: android image android-intent textview implicit


【解决方案1】:

当您选择单个图像时,您会通过 data.getData() 获取图像路径。 而在多张图片的情况下,通过data.clipData获取所有图片路径。

您可以使用以下代码:

if (resultCode == RESULT_OK) {
        if (requestCode == SELECT_PICTURE) {

            // For multiple images
            if(data.clipData!=null)
            {
               for(int i=0;i<data.clipData.ietmCount;i++){
                   imageUri=data.clipData.getItemAt(i).uri
                   }
            
            }else {
            selectedImageUri = data.getData();
            imageName = selectedImageUri.getPath();
            }

            }
}

【讨论】:

    【解决方案2】:

    我用这个代码来显示图片,它的名字来自画廊:

    主要活动

    public class MainActivity extends AppCompatActivity {
    
    ImageView imageView ;
    
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        imageView = (ImageView) findViewById(R.id.imageView);
        
        /this code will let user to pick image from gallery
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("image/*");
        Intent.createChooser(intent,"title");
        startActivityForResult(intent,10);
    
    }
    
    // when we get back, the image we have selected will be here :
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    
        if(resultCode == RESULT_OK) {
            // the image data
            Uri uri = data.getData();
    
            String[] projection = {MediaStore.Images.Media.DISPLAY_NAME};
            Cursor cursor = managedQuery(uri, projection, null, null, null);
    
            // the image Name
            String name = null;
    
            while (cursor != null && cursor.moveToNext()) {
                // here we get the name
                name = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME));
            }
    
            // here to display image and it's name
            Toast.makeText(this, name, Toast.LENGTH_SHORT).show();
            imageView.setImageURI(uri);
        }
    }
    
    }
    

    activity_mail.xml

    <androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"/>
    
    </androidx.drawerlayout.widget.DrawerLayout>
    

    更多详情,请访问:Android Developers

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-31
      • 2018-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多