StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
как переместить StrictMode.ThreadPolicy.Builder
в AsyncTask
или в IntentService
?? если намерение означает, что это было бы очень полезно.как переместить StrictMode.ThreadPolicy.Builder в задачу Async или в службу Intent? если намерение означает, что было бы очень полезно
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addbeneficiary);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
b1 = (ImageButton) findViewById(R.id.profilephotobutton);
b2 = (ImageButton) findViewById(R.id.idcardicon);
b3 = (ImageButton) findViewById(R.id.formphotoicon);
b4=(ImageView) findViewById(R.id.closeclick);
up=(Button)findViewById(R.id.submitbutton);
iv1=(ImageView)findViewById(R.id.beneficiaryprofilepic);
iv2=(ImageView)findViewById(R.id.idcardpic);
iv3=(ImageView)findViewById(R.id.beneficiaryformpic);
beneficiaryname=(EditText)findViewById(R.id.tbeneficiaryname);
proofname=(EditText)findViewById(R.id.tvprooftype);
idproofno=(EditText)findViewById(R.id.idproofnumber);
StrictMode.setThreadPolicy(policy);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
mMediaUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
if (mMediaUri == null) {
// display an error
Toast.makeText(MainActivity.this, R.string.error_external_storage,
Toast.LENGTH_LONG).show();
} else {
takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, mMediaUri);
startActivityForResult(takePhotoIntent, 0);
}
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
mMediaUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
if (mMediaUri == null) {
// display an error
Toast.makeText(MainActivity.this, R.string.error_external_storage,
Toast.LENGTH_LONG).show();
} else {
takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, mMediaUri);
startActivityForResult(takePhotoIntent, 1);
}
}
});
b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
mMediaUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
if (mMediaUri == null) {
// display an error
Toast.makeText(MainActivity.this, R.string.error_external_storage,
Toast.LENGTH_LONG).show();
} else {
takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, mMediaUri);
startActivityForResult(takePhotoIntent, 2);
}
}
});
b4.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent ii = new Intent(MainActivity.this, ListOfProjectsActivity.class);
finish();
startActivity(ii);
}
});
up.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
doFileUpload();
/* Toast.makeText(MainActivity.this,"First Images Path="+ivs,Toast.LENGTH_LONG).show();
Toast.makeText(MainActivity.this,"2="+ivs2,Toast.LENGTH_LONG).show();
Toast.makeText(MainActivity.this,"3="+ivs3,Toast.LENGTH_LONG).show();*/
}
});
}
private Uri getOutputMediaFileUri(int mediaType) {
// To be safe, you should check that the SDCard is mounted
// using Environment.getExternalStorageState() before doing this.
if (isExternalStorageAvailable()) {
// get the URI
// 1. Get the external storage directory
String appName = MainActivity.this.getString(R.string.app_name);
File mediaStorageDir = new File(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
appName);
// 2. Create our subdirectory
if (! mediaStorageDir.exists()) {
if (! mediaStorageDir.mkdirs()) {
Log.e(TAG, "Failed to create directory.");
return null;
}
}
// 3. Create a file name
// 4. Create the file
Date now = new Date();
String timestamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(now);
String path = mediaStorageDir.getPath() + File.separator;
if (mediaType == MEDIA_TYPE_IMAGE) {
mediaFile = new File(path + "IMG_" + timestamp + ".jpg");
}
else {
return null;
}
Log.d(TAG, "File: " + Uri.fromFile(mediaFile));
// 5. Return the file's URI
return Uri.fromFile(mediaFile);
}
else {
return null;
}
}
private boolean isExternalStorageAvailable() {
String state = Environment.getExternalStorageState();
if (state.equals(Environment.MEDIA_MOUNTED)) {
return true;
}
else {
return false;
}
}
private void doFileUpload(){
String tv1= beneficiaryname.getText().toString();
String tv2=proofname.getText().toString();
String tv3=idproofno.getText().toString();
File file1 = new File(ivs);
File file2 = new File(ivs2);
File file3=new File(ivs3);
String urlString = "http://webdev.xerago.com/habitat/upload.php";
try
{
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(urlString);
FileBody bin1 = new FileBody (file1);
FileBody bin2 = new FileBody (file2);
FileBody bin3=new FileBody (file3);
MultipartEntity reqEntity = new MultipartEntity();
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("bname", tv1));
nameValuePairs.add(new BasicNameValuePair("bproof", tv2));
nameValuePairs.add(new BasicNameValuePair("bproof_no", tv3));
reqEntity.addPart("bname", new StringBody(tv1));
reqEntity.addPart("bproof", new StringBody(tv2));
reqEntity.addPart("bproof_no", new StringBody(tv3));
reqEntity.addPart("beneficiarypic", bin1);
reqEntity.addPart("idproof", bin2);
reqEntity.addPart("beneform",bin3);
// reqEntity.addPart("bname", new StringBody(benename));
// reqEntity.addPart("bproof", new StringBody(prooftype));
// reqEntity.addPart("bproof_no", new StringBody(prooofnumber));
post.setEntity(reqEntity);
HttpResponse response = client.execute(post);
resEntity = response.getEntity();
final String response_str = EntityUtils.toString(resEntity);
if (resEntity != null) {
Log.i("RESPONSE",response_str);
runOnUiThread(new Runnable(){
public void run() {
try {
Toast.makeText(getApplicationContext(),"Upload Complete. Check the server uploads directory.", Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
catch (Exception ex){
Log.e("Debug", "error: " + ex.getMessage(), ex);
}
}
private void preview(int req){
try
{
BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize=8;
final Bitmap bitmap=BitmapFactory.decodeFile(mMediaUri.getPath(),options);
switch(req)
{
case 0:
iv1.setImageBitmap(bitmap);
iv1.setTag(mMediaUri.getPath());
ivs=(String) iv1.getTag();
break;
case 1:
iv2.setImageBitmap(bitmap);
iv2.setTag(mMediaUri.getPath());
ivs2=(String) iv2.getTag();
break;
case 2:
iv3.setImageBitmap(bitmap);
iv3.setTag(mMediaUri.getPath());
ivs3=(String)iv3.getTag();
break;
default:
break;
}
}
catch(NullPointerException e)
{
e.printStackTrace();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
// add it to the Gallery
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
mediaScanIntent.setData(mMediaUri);
sendBroadcast(mediaScanIntent);
preview(requestCode);
}
else if (resultCode != RESULT_CANCELED) {
Toast.makeText(this, R.string.general_error, Toast.LENGTH_LONG).show();
}
}
}
Почему вы хотите использовать его в службе асинхронности или намерения? Вы можете выполнять связанные с сетью операции в обоих из них. – Emil
расскажите нам о своей цели использования этой задачи async – Emil