这是将图像插入数据库的代码。问题是当使用函数“requestWritePermission();”时然后我不能从另一个进入这个活动但是当我删除这个功能..这个活动开始但是当点击上传图片按钮它进入画廊但没有显示图像..我没有得到它。请帮忙。
package com.example.tariqziad.myapplication;
import android.Manifest;
import android.annotation.TargetApi;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.File;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import static android.R.attr.id;
public class ImageUpload extends AppCompatActivity {
Button uploadbutton, insertbutton;
ImageView imageView;
String mediaPath;
private static final int WRITE_PERMISSION = 0x01;
private static final int PICK_IMAGE = 100;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_upload);
//requestWritePermission();
uploadbutton= (Button)findViewById(R.id.upload);
insertbutton= (Button)findViewById(R.id.insert);
insertbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
uploadpic();
}
});
uploadbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent,PICK_IMAGE);
}
});
imageView=(ImageView)findViewById(R.id.imageview);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
// When an Image is picked
if (requestCode == 0 && resultCode == RESULT_OK && null != data) {
// Get the Image from data
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
assert cursor != null;
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
mediaPath = cursor.getString(columnIndex);
// Set the Image in ImageView for Previewing the Media
imageView.setImageBitmap(BitmapFactory.decodeFile(mediaPath));
cursor.close();
} else {
Toast.makeText(this, "You haven't picked Image/Video", Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG).show();
}
}
private void uploadpic() {
File file = new File(mediaPath);
RequestBody requestBody = RequestBody.create(MediaType.parse("*/*"), file);
MultipartBody.Part fileToUpload = MultipartBody.Part.createFormData("file", file.getName(), requestBody);
RequestBody filename = RequestBody.create(MediaType.parse("text/plain"), file.getName());
SchoolInterfaces t=SchoolApi.getClient().create(SchoolInterfaces.class);
Call<ImageResponse> call=t.uploadFile(fileToUpload,filename);
call.enqueue(new Callback<ImageResponse>() {
@Override
public void onResponse(Call<ImageResponse> call, Response<ImageResponse> response) {
ImageResponse serverResponse = response.body();
if (serverResponse != null) {
if (serverResponse.getSuccess()) {
/* Intent intent=new Intent(PostArea.this,SellerActivity.class);
intent.putExtra("id",id);
startActivity(intent); */
//finish();
} else {
Toast.makeText(getApplicationContext(), serverResponse.getMessage(),Toast.LENGTH_SHORT).show();
}
} else {
assert serverResponse != null;
Log.v("Response", serverResponse.toString());
}
}
@Override
public void onFailure(Call<ImageResponse> call, Throwable t) {
}
});
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
if(requestCode == WRITE_PERMISSION){
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "You must allow permission write external storage to your mobile device.", Toast.LENGTH_SHORT).show();
}
}
}
@TargetApi(Build.VERSION_CODES.M)
private void requestWritePermission() {
if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, WRITE_PERMISSION);
}
}
}