我试图从onclicklistener中调用它,该onclicklistener是在我编写的适配器中设置的
这是我的onclicklistener
imgCamera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Intent intent = new Intent(context, ScanActivity.class);
// ((Activity) context).startActivity(intent);
Intent intent =new Intent(context, ScanAdaptor.class);
}
}
我希望它能够调用我写的一个使用startactivityforresult的类:
public class ScanAdaptor extends Activity {
private Context context;
private static final int REQUEST_CODE = 99;
String ba1;
public String URL = "http:";
@Override
public void onCreate(Bundle savedInstanceState) {
int REQUEST_CODE = 99;
int preference = ScanConstants.OPEN_CAMERA;
Intent intent = new Intent(this, ScanActivity.class);
intent.putExtra(ScanConstants.OPEN_INTENT_PREFERENCE, preference);
startActivityForResult(intent, REQUEST_CODE);
context = this;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK) {
Uri uri = data.getExtras().getParcelable(ScanConstants.SCANNED_RESULT);
Bitmap bitmap = null;
try {
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
getContentResolver().delete(uri, null, null);
// scannedImageView.setImageBitmap(bitmap);
upload(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
private Bitmap convertByteArrayToBitmap(byte[] data) {
return BitmapFactory.decodeByteArray(data, 0, data.length);
}
private void upload(Bitmap bm) {
// Image location URL
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 90, bao);
byte[] ba = bao.toByteArray();
int flag = 0;
ba1 = Base64.encodeToString(ba, flag);
Log.e("base64", "-----" + ba1);
// Upload image to server
new uploadToServer().execute();
}
public class uploadToServer extends AsyncTask<Void, Void, String> {
private ProgressDialog pd = new ProgressDialog(((Activity) context));
protected void onPreExecute() {
super.onPreExecute();
pd.setMessage("Wait image uploading!");
pd.show();
}
@Override
protected String doInBackground(Void... params) {
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("base64", ba1));
nameValuePairs.add(new BasicNameValuePair("ImageName", System.currentTimeMillis() + ".jpg"));
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(URL);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
String st = EntityUtils.toString(response.getEntity());
Log.v("log_tag", "In the try Loop" + st);
} catch (Exception e) {
Log.v("log_tag", "Error in http connection " + e.toString());
}
return "Success";
}
protected void onPostExecute(String result) {
super.onPostExecute(result);
pd.hide();
pd.dismiss();
Intent myIntent = new Intent(((Activity) context), MainActivity.class);
myIntent.putExtra("dir", "BS"); //Optional parameters
((Activity) context).startActivity(myIntent);
}
}
}
当这是配置时,按下按钮时没有任何反应。
它让我发疯,请帮忙答案 0 :(得分:1)
您的<p:dialog resizable="false"
fitViewport="true"
widgetVar="imageDialog"
style="height: 95vh;width: 95vw;"
id="imageDialogForm"
modal="true"
showEffect="clip"
hideEffect="slide"
closeOnEscape="true">
<p:layout style="height: 90vh; width: 90vw;" >
<p:layoutUnit position="center" collapsible="true" gutter="0">
<h:graphicImage value="" width="80%" styleClass="image-lightboxer"/>
</p:layoutUnit>
<p:layoutUnit position="east" size="15%" gutter="0" collapsible="true" style="text-align: right;">
<h:form prependId="false">
<!-- how to pass to these labels
// the ACTIVE image
//which has class type .active
// which is always showing the same information
// which turns out to be the las [i] variable
//info from the contentFlow var="i" -->
<p:outputLabel value="#{i.activeImageId}"/>
<p:separator/>
<p:outputLabel value="#{i.imageName}"/>
<p:separator/>
<p:outputLabel value="#{i.imageDescription}"/>
<p:separator/>
<p:commandButton value="Eliminar Foto" icon="ui-icon-trash" iconPos="right"
onclick="PF('imageDialog').hide()"
oncomplete="PF('delImg').show()"/>
</h:form>
</p:layoutUnit>
</p:layout>
</p:dialog>
方法无法启动活动,只会创建意图。试试这个:
onClick