我怎么能得到我在Imageview为Android工作室选择的图像文件名?



我在android studio上有这个简单的图像查看器,当我选择一个图像时,我想要吐司显示图像名称(例如。"image.jpeg"。我应该在祝酒词的部分加上哪句话?制作文本,使其显示图片的名称?我试过做selectedImageUri.getLastPathSegment(),但它给了我文档id。

package com.example.pushnotifications;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
// One Button
Button BSelectImage;
// One Preview Image
ImageView IVPreviewImage;
// constant to compare
// the activity result code
int SELECT_PICTURE = 200;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// register the UI widgets with their appropriate IDs
BSelectImage = findViewById(R.id.BSelectImage);
IVPreviewImage = findViewById(R.id.IVPreviewImage);
// handle the Choose Image button to trigger
// the image chooser function
BSelectImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
imageChooser();
}
});
}
// this function is triggered when
// the Select Image Button is clicked
void imageChooser() {
// create an instance of the
// intent of the type image
Intent i = new Intent();
i.setType("image/*");
i.setAction(Intent.ACTION_GET_CONTENT);
// pass the constant to compare it
// with the returned requestCode
startActivityForResult(Intent.createChooser(i, "Select Picture"), SELECT_PICTURE);
}
// this function is triggered when user
// selects the image from the imageChooser
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
// compare the resultCode with the
// SELECT_PICTURE constant
if (requestCode == SELECT_PICTURE) {
// Get the url of the image from data
Uri selectedImageUri = data.getData();
if (null != selectedImageUri) {
// update the preview image in the layout
IVPreviewImage.setImageURI(selectedImageUri);
}

Toast.makeText(getApplicationContext(), "Selected Image: " + WHAT DO I PUT HERE??, Toast.LENGTH_LONG).show();

}
}
}

}

使用此方法获取uri名称

public String getFileName(Uri uri) {
String result = null;
if (uri.getScheme().equals("content")) {
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
try {
if (cursor != null && cursor.moveToFirst()) {
result = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
}
} finally {
cursor.close();
}
}
if (result == null) {
result = uri.getPath();
int cut = result.lastIndexOf('/');
if (cut != -1) {
result = result.substring(cut + 1);
}
}
return result;
}

如果name未得到,则syn此依赖项

implementation 'andhradroid.dev:aFilechooser:1.0.1'

和onactivity result

中的这一行
val file2 = FileUtils.getFile(context, uri)
Toast.makeText(context, file2.name, Toast.LENGTH_SHORT).show()

根据Android文档,你可以使用下面的代码:

private String getSelectedImageName(Uri returnUri){
Uri returnUri = data.getData();
if(returnUri != null){
Cursor returnCursor =
getContentResolver().query(returnUri, null, null, null, null);
/*
* Get the column indexes of the data in the Cursor,
* move to the first row in the Cursor, get the data,
* and display it.
*/
int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
String fileName = returnCursor.getString(nameIndex)

return fileName ;

}else{
return "No data found"
}
}

相关内容

  • 没有找到相关文章

最新更新