问题描述
这是我从相机拍摄图像的逻辑onCreate方法: -
This is my oncreate method with logic of capturing image from camera:-
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_driverregister);
app = (MyApplication) getApplication();
btnTackPic = (Button) findViewById(R.id.btnTakePic);
driverPhoto = (ImageView) findViewById(R.id.ivThumbnailPhoto);
driverName = (EditText) findViewById(R.id.driverName);
address = (EditText) findViewById(R.id.address);
contactNumber = (EditText) findViewById(R.id.contactNumber);
licenseNumber = (EditText) findViewById(R.id.licenseNumber);
btnTackPic.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// create intent with ACTION_IMAGE_CAPTURE action
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// to save picture remove comment
File file = new File(Environment.getExternalStorageDirectory(),
"my-photo.jpg");
Uri photoPath = Uri.fromFile(file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, photoPath);
intent.putExtra("data", true);
setResult(1, intent);
// start camera activity
startActivityForResult(intent, TAKE_PICTURE);
System.out.println("driverName: "+driverName.getText());
System.out.println("Address: "+address.getText());
System.out.println("String is"+encodedImage);
System.out.println("photo path is"+photoPath);
}
这是我在哪里的图像转换为base64写逻辑的onActivityResult()函数,然后串。我想在应用程序/ JSON格式发布在我的形式我的其他表单字段一起提交的一部分。
当尝试打印连接codeDIMAGE
这表明空每但这将在的onActivityResult()这是如下: -
This is the onActivityResult() function where I am writing logic for converting the image to base64 and then to string. I want to post in application/json format along with my other form fields in my form submit part.When try to print the
encodedImage
it shows null every time but this will be printed in the onActivityResult()
which is below:-
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == TAKE_PICTURE && resultCode== RESULT_OK && intent != null){
// get bundle
Bundle extras = intent.getExtras();
// get
bitMap = (Bitmap) extras.get("data");
driverPhoto.setImageBitmap(bitMap);
/* Code for Image */
Bitmap bitmap = ((BitmapDrawable)driverPhoto.getDrawable()).getBitmap();
ByteArrayOutputStream stream=new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream);
byte[] image=stream.toByteArray();
System.out.println("byte array:"+image);
String encodedImage = Base64.encodeToString(image, Base64.DEFAULT);
// img_str = Base64.encodeToString(image, 0);
// System.out.println("string:"+img_str);
// app.setImgString(img_str);
System.out.println("string:"+encodedImage);
}
}
我要访问此EN codeDIMAGE它的外面,并用它作为全局变量,这样我可以以JSON格式而不是多部分张贴。
I want to access this encodedImage outside of it and use it as global variable so that I can post in JSON format instead of multipart.
推荐答案
您是否尝试过使用的?
Have you tried using the Broadcast Notifications?
在
的onActivityResult
广播方法的结果:
Intent intent = new Intent();
intent.setAction("ACTIVITY_RESULT");
intent.putExtra("requestCode", requestCode);
intent.putExtra("resultCode", resultCode);
intent.putExtra("encodedImage", encodedImage);
getBaseContext().sendBroadcast(intent);
在任何活动你可以为你这样的广播信息进行注册:
In any Activity you can register for your Broadcast message like this:
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
// stufs...
IntentFilter intentFilter = new IntentFilter("ACTIVITY_RESULT");
this.registerReceiver(mReceiver, intentFilter);
// other stufs...
}
//The BroadcastReceiver that listens for Notification broadcasts
public final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent)
{
final String action = intent.getAction();
// get notification action
if (action.equals("ACTIVITY_RESULT")) {
// get encoded image extra parameter
String encodedImage = intent.getStringExtra("encodedImage");
// get others parameters...
}
}
希望这有助于为更多的信息,你可以查看任何教程你发现了。
这篇关于如何访问的onActivityResult()在它之外的成员在Android的任何其他功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!