当我点击图库时,我正在实现相机和图库意图的代码 它完美地工作并在服务器上图像上传。但是,当我点击相机时,我得到一个路径,当我上传它显示错误像"没有这样的文件或目录"。在所有代码检查后我找不到错误,请建议我代码在这里: -
活动代码是: -
public class Preception_upload_only extends AppCompatActivity implements View.OnClickListener {
ImageView imageView;
private String userChoosenTask;
private int REQUEST_CAMERA = 0, SELECT_FILE = 1;
LinearLayout mainLayout;
Bitmap bitmap;
Uri gelleryUri,fileUri,ur;
Button submit;
String user_id,imagepath;
RadioGroup radioGroup;
TypedFile avtarfile=null;
RadioButton radioButton;
ProgressBar progressBar;
public Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
return Uri.parse(path);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_preception_upload_only);
user_id = com.hp.medwaala.Util.M.getNumber(getApplicationContext());
FINDVIEWBYID();
CLICKLISTINER();
}
private void FINDVIEWBYID() {
imageView = (ImageView) findViewById(R.id.upload_prection_only_icon);
mainLayout = (LinearLayout) findViewById(R.id.mainLayout_only);
radioGroup= (RadioGroup) findViewById(R.id.radio_selected);
submit= (Button) findViewById(R.id.uploadprecption_button);
progressBar= (ProgressBar) findViewById(R.id.progressbar_uploadprescription);
}
private void CLICKLISTINER() {
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
progressBar.setVisibility(View.VISIBLE);
int selectedId = radioGroup.getCheckedRadioButtonId();
radioButton = (RadioButton) findViewById(selectedId);
String getext= (String) radioButton.getText();
avtarfile = new TypedFile("image/*", new File(imagepath));
RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint(AppConst.MAIN).build();
UsersAPI uploadpreception = restAdapter.create(UsersAPI.class);
uploadpreception.uploadpreception(user_id, getext, avtarfile, avtarfile, avtarfile, avtarfile, new Callback<Upload_preception>() {
@Override
public void success(Upload_preception upload_preception, Response response) {
String sucessss=upload_preception.getSuccess();
if (sucessss.equalsIgnoreCase("1"))
{
progressBar.setVisibility(View.GONE);
Toast.makeText(Preception_upload_only.this, "Customer care Will call you in 24 hours thanks ", Toast.LENGTH_LONG).show();
Intent intent=new Intent(Preception_upload_only.this,MainActivity.class);
startActivity(intent);
}
}
@Override
public void failure(RetrofitError error) {
Toast.makeText(Preception_upload_only.this, "Failureee"+error, Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
}
});
}
});
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
selectImage();
}
});
}
private void selectImage() {
final CharSequence[] items = {"Take Photo", "Choose from Library",
"Cancel"};
AlertDialog.Builder builder = new AlertDialog.Builder(Preception_upload_only.this,R.style.MyAlertDialogStyle1);
builder.setTitle("Add Photo!");
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
boolean result = Utility.checkPermission(Preception_upload_only.this);
if (items[item].equals("Take Photo")) {
userChoosenTask = "Take Photo";
if (result)
cameraIntent();
} else if (items[item].equals("Choose from Library")) {
userChoosenTask = "Choose from Library";
if (result)
galleryIntent();
} else if (items[item].equals("Cancel")) {
dialog.dismiss();
}
}
});
builder.show();
}
private void galleryIntent() {
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, SELECT_FILE);
}
private void cameraIntent() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = FileProvider.getUriForFile(Preception_upload_only.this,
"com.hp.medwaala.FileProvider",
getOutputMediaFile(MEDIA_TYPE_IMAGE));
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(intent, REQUEST_CAMERA);
}
private static File getOutputMediaFile(int type) {
File mediaStorageDir = new File(Environment.getExternalStorageDirectory() + "/.Dope/");
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
return null;
}
}
File mediaFile;
java.util.Date date = new java.util.Date();
if (type == MEDIA_TYPE_IMAGE) {
mediaFile = new File(mediaStorageDir, Long.toString(System.currentTimeMillis()) + "IMG_" + new Timestamp(date.getTime()).toString() + ".jpg");
} else {
return null;
}
return mediaFile;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == REQUEST_CAMERA) {
previewCapturedImage();
onCaptureImageResult(bitmap);
getimage();
}
if (requestCode == SELECT_FILE) {
gelleryUri = Uri.parse(String.valueOf(FilePath.getPath(Preception_upload_only.this, data.getData())));
onSelectFromGalleryResult(gelleryUri);
getimage();
}
}
}
private void previewCapturedImage() {
try {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
InputStream ims = getContentResolver().openInputStream(fileUri);
bitmap = BitmapFactory.decodeStream(ims);
} catch (NullPointerException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
private void onCaptureImageResult(Bitmap mUri) {
RelativeLayout r1 = new RelativeLayout(getApplicationContext());
r1.setLayoutParams(new RelativeLayout.LayoutParams(300, 400));
ImageView img = new ImageView(getApplicationContext());
img.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));
img.setScaleType(ImageView.ScaleType.FIT_XY);
img.setPadding(10, 10, 10, 10);
img.setTag(mUri);
img.setImageBitmap(mUri);
ImageView cross = new ImageView(getApplicationContext());
cross.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
cross.setClickable(true);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) cross.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
cross.setImageResource(R.drawable.cross_icon);
cross.setId('1');
cross.setOnClickListener(this);
r1.addView(img);
r1.addView(cross);
mainLayout.addView(r1);
}
@SuppressWarnings("deprecation")
private void onSelectFromGalleryResult(Uri mUri) {
RelativeLayout r1 = new RelativeLayout(getApplicationContext());
r1.setLayoutParams(new RelativeLayout.LayoutParams(300, 400));
ImageView img = new ImageView(getApplicationContext());
img.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));
img.setScaleType(ImageView.ScaleType.FIT_XY);
img.setDrawingCacheEnabled(true);
img.setPadding(10, 10, 10, 10);
img.setTag(mUri);
img.setImageURI(mUri);
ImageView cross = new ImageView(getApplicationContext());
cross.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
cross.setClickable(true);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) cross.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
cross.setImageResource(R.drawable.cross_icon);
cross.setId('1');
cross.setOnClickListener(this);
r1.addView(img);
r1.addView(cross);
mainLayout.addView(r1);
}
@Override
public void onClick(View v) {
if (v.getId() == '1') {
LinearLayout linearParent = (LinearLayout) v.getParent().getParent();
RelativeLayout linearChild = (RelativeLayout) v.getParent();
linearParent.removeView(linearChild);
}
}
private void getimage() {
RelativeLayout ra =(RelativeLayout) mainLayout.getChildAt(0);
int childCount = ra.getChildCount();
List<String> mStrings = new ArrayList<String>();
for(int i = 0; i < childCount; i++) {
ImageView imG = (ImageView)ra.getChildAt(0);
String path = imG.getTag().toString();
if(imG.getTag().toString().contains("Bitmap"))
{
Bitmap path1 = (Bitmap)imG.getTag();
ur= getImageUri(getApplicationContext(),path1);
}
else {
ur =(Uri)imG.getTag();
}
imagepath=ur.toString();
mStrings.add(ur.toString());
}
}
}