これは私のMainActivity
ファイルです。私はImage
ファイルを外部ストレージから選択して、パーセント進捗状況でサーバーにアップロードしようとしています。 ProgressDialog
を追加したいのですが、追加しようとしましたが、実行時エラーが追加されました。アンドロイドでokhttp3の進行状況ダイアログをリスナの外に追加する方法
package com.example.panchayat.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.support.v4.app.ActivityCompat;
import android.os.Bundle;
import android.Manifest;
import android.util.Log;
import android.widget.Toast;
import android.content.pm.PackageManager;
import android.content.Intent;
import android.net.Uri;
import java.io.File;
import java.io.IOException;
import android.database.Cursor;
import android.provider.MediaStore;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.OkHttpClient;
import okhttp3.Callback;
import okhttp3.Call;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import android.os.Handler;
import android.app.ProgressDialog;
import com.example.panchayat.myapplication.CountingFileRequestBody;
import com.example.panchayat.myapplication.CountingFileRequestBody.ProgressListener;
import com.example.panchayat.myapplication.ProgressRequestBody.Listener;
import com.example.panchayat.myapplication.ProgressRequestBody;
public class MainActivity extends AppCompatActivity
{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, 1);
ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},1);
}
@Override
public void onRequestPermissionsResult(int requestCode,String permissions[], int[] grantResults) {
switch (requestCode) {
case 1: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// contacts-related task you need to do.
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
Toast.makeText(MainActivity.this, "Permission denied to read your External storage", Toast.LENGTH_SHORT).show();
//finishAffinity();
//System.exit(0);
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == this.RESULT_CANCELED) {
return;
}
if (requestCode == 1) {
if (data != null) {
Uri uri = data.getData();
File myFile = new File(getRealPathFromURI(uri));
final OkHttpClient client = new OkHttpClient();
final MediaType MEDIA_TYPE_PNG = MediaType.parse("image/png");
RequestBody requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("title", "Square Logo")
.addFormDataPart("image", "logo-square.png", RequestBody.create(MEDIA_TYPE_PNG, myFile))
.build();
//CountingFileRequestBody ok = new CountingFileRequestBody(requestBody, "image/jpg",progressListener);
ProgressRequestBody ioo = new ProgressRequestBody(requestBody,progressListener);
Toast.makeText(MainActivity.this, myFile.getAbsolutePath(), Toast.LENGTH_SHORT).show();
Request request = new Request.Builder()
.url("http://bmi.ir/multi.php")
.post(ioo)
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.d("ok",e.getMessage());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Log.d("ok",response.body().string());
//Toast.makeText(MainActivity.this, response.body().string(), Toast.LENGTH_SHORT).show();
}
}
);
}
}
}
final Listener progressListener = new Listener() {
@Override public void onProgress(int bytesRead) {
//Toast.makeText(MainActivity.this, "", Toast.LENGTH_SHORT).show();
final String myString = Long.toString(bytesRead);
final int per = bytesRead;
Log.d("ok", myString);
}
};
private String getRealPathFromURI(Uri contentURI)
{
String result = null;
Cursor cursor = getContentResolver().query(contentURI, null, null, null, null);
if (cursor == null)
{ // Source is Dropbox or other similar local file path
result = contentURI.getPath();
}
else
{
if(cursor.moveToFirst())
{
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
result = cursor.getString(idx);
}
cursor.close();
}
return result;
}
}
リスナーから進捗%を取得してProgressDialog
を更新する方法はありますか?
あなたはエラーを報告できますか? –