gpt4 book ai didi

java - 使用索引位置发送图像

转载 作者:行者123 更新时间:2023-12-01 13:42:29 26 4
gpt4 key购买 nike

我通过蓝牙将图像发送到其他设备以获取单击的列表项,但每次我的设备发送最后一个索引图像时,例如:我的 ListView 中有 3 个图像,它总是发送第三个图像,即使我点击第一个图像的列表中的发送按钮....

String fileName;
String strPath;
int position ;
List <String> ImageList;
File f;
File file;


public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub

LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

if (convertView == null) {
convertView = inflater.inflate(R.layout.list_upload, null);
}

// ColImgName
TextView txtName = (TextView) convertView.findViewById(R.id.ColImgName);
strPath = ImageList.get(position).toString();

// Get File Name
fileName = strPath.substring( strPath.lastIndexOf('/')+1, strPath.length() );
file = new File(strPath);
@SuppressWarnings("unused")
long length = file.length();
txtName.setText(fileName);

// Image Resource
ImageView imageView = (ImageView) convertView.findViewById(R.id.ColImgPath);
final BitmapFactory.Options options = new BitmapFactory.Options();

options.inSampleSize = 8;

Bitmap bm = BitmapFactory.decodeFile(strPath,options);
imageView.setImageBitmap(bm);


// ColStatus
final ImageView txtStatus = (ImageView) convertView.findViewById(R.id.ColStatus);
txtStatus.setImageResource(R.drawable.bullet_button);

// progressBar
final ProgressBar progress = (ProgressBar) convertView.findViewById(R.id.progressBar);
progress.setVisibility(View.GONE);

//btnUpload
final ImageButton btnUpload = (ImageButton) convertView.findViewById(R.id.btnUpload);
btnUpload.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Upload
btnUpload.setEnabled(false);

startUpload(position);
}
});

// Data
final ImageButton btnData = (ImageButton) convertView.findViewById(R.id.btnData);
btnData.setOnClickListener(new View.OnClickListener() {
@SuppressWarnings("deprecation")
public void onClick(View v) {

fileName=ImageList.get(position).toString().substring(strPath.lastIndexOf('/')+1, strPath.length());
showDialog(DIALOG_LOGIN);
}
});


final ImageButton btnSend = (ImageButton) convertView.findViewById(R.id.btnSend);
btnSend.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {

Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setComponent(new ComponentName(
"com.android.bluetooth",
"com.android.bluetooth.opp.BluetoothOppLauncherActivity"));
intent.setType("image/jpeg");
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
startActivity(intent);

}
});

return convertView;

}
}

最佳答案

您应该使用setTag()方法并将File对象分配给按钮。然后,当单击按钮时,getTag() File 对象并使用它..

更新要附加 onClickListener 的代码部分,如下所示:

final ImageButton btnSend = (ImageButton) convertView.findViewById(R.id.btnSend);
btnSend.setTag(new File(strPath)); // btn knows which file is related to it
btnSend.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
File newFile = (File) btnSend.getTag(); // get the related file on click

Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setComponent(new ComponentName(
"com.android.bluetooth",
"com.android.bluetooth.opp.BluetoothOppLauncherActivity"));
intent.setType("image/jpeg");
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(newFile)); // use it
startActivity(intent);

}
});

关于java - 使用索引位置发送图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20604360/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com