2017-04-12 53 views
0
package com.example.ishan.complainbox; 

import android.content.Intent; 
import android.os.Bundle; 
import android.support.v7.widget.AppCompatImageButton; 
import android.view.View; 
import android.widget.EditText; 
import java.io.ByteArrayOutputStream; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.DialogInterface; 
import android.content.pm.PackageManager; 
import android.graphics.Bitmap; 
import android.os.Environment; 
import android.provider.MediaStore; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.ImageButton; 
import android.widget.ImageView; 
import android.widget.Toast; 
import android.support.v7.widget.AppCompatImageView; 
import android.location.Criteria; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.support.v4.app.FragmentActivity; 
import android.widget.TextView; 
import com.google.android.gms.common.ConnectionResult; 
import com.google.android.gms.common.GooglePlayServicesUtil; 
import com.google.android.gms.maps.CameraUpdateFactory; 
import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.SupportMapFragment; 
import com.google.android.gms.maps.model.LatLng; 
import com.google.android.gms.maps.model.MarkerOptions; 
import android.graphics.drawable.BitmapDrawable; 


public class Crime extends MainActivity implements 
View.OnClickListener,LocationListener { 

GoogleMap googleMap; 
private int REQUEST_CAMERA = 0, SELECT_FILE = 1; 
private Button btnSelect; 
private ImageView imgView,ivImage; 
private String userChosenTask; 
EditText street, city, pincode, detail; 
Button btnsave; 
crimeDBHandler dbHandler; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    if (!isGooglePlayServicesAvailable()) { 
     finish(); 
     setContentView(R.layout.activity_crime); 
     // Get References of Views 
     street = (EditText) findViewById(R.id.str); 
     city = (EditText) findViewById(R.id.city); 
     pincode = (EditText) findViewById(R.id.pin); 
     detail = (EditText) findViewById(R.id.detail); 
     imgView = (ImageView)findViewById(R.id.imgView); 
     btnsave = (Button) findViewById(R.id.save); 
     btnSelect = (Button) findViewById(R.id.uploadpic); 
     dbHandler = new crimeDBHandler(this, null, null, 1); 

     btnSelect.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       String strt = street.getText().toString(); 
       String cty = city.getText().toString(); 
       String pin = pincode.getText().toString(); 
       String det = detail.getText().toString(); 
       Bitmap bitmap = 
    ((BitmapDrawable)imageView.getDrawable()).getBitmap(); 
       ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
       bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); 
       byte[] imageInByte = baos.toByteArray(); 
       btnsave = (Button) findViewById(R.id.save); 
       selectImage(); 
       dbHandler.insertEntry(strt, cty, pin, det,imageInByte); 
       Toast.makeText(getApplicationContext(), "Complaint 
Successfully Filed ", Toast.LENGTH_LONG).show(); 
      } 

     }); 
     ivImage = (ImageView) findViewById(R.id.img); 
    } 
    } 


    @Override 
    public void onRequestPermissionsResult (int requestCode, String[] 
permissions, 
    int[] grantResults){ 
     switch (requestCode) { 
      case Utility.MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE: 
       if (grantResults.length > 0 && grantResults[0] == 
PackageManager.PERMISSION_GRANTED) { 
        if (userChosenTask.equals("Take Photo")) 
         cameraIntent(); 
        else if (userChosenTask.equals("Choose from Library")) 
         galleryIntent(); 
       } else 
        break; 
     } 
    } 



    private boolean isGooglePlayServicesAvailable() { 
    int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); 
    if (ConnectionResult.SUCCESS == status) { 
     return true; 
    } else { 
     GooglePlayServicesUtil.getErrorDialog(status, this, 0).show(); 
     return false; 
    } 
} 


    private void selectImage() { 
    final CharSequence[] items = {"Take Photo", "Choose from Library", 
      "Cancel" }; 
    AlertDialog.Builder builder = new AlertDialog.Builder(Crime.this); 
    builder.setTitle("Add Photo!"); 
    builder.setItems(items, new DialogInterface.OnClickListener() { 

     @Override 

     public void onClick(DialogInterface dialog, int item) { 
      boolean result=userChosenTask.checkPermission(Crime.this); 
      if (items[item].equals("Take Photo")) 
      { 
       userChosenTask ="Take Photo"; 
       if(result) 
        cameraIntent(); 
      } 
      else if (items[item].equals("Choose from Library")) 
      { 
       userChosenTask ="Choose from Library"; 
       if(result) 
        galleryIntent(); 
      } 
      else if (items[item].equals("Cancel")) 
      { 
       dialog.dismiss(); 

      } 
     } 

    }); 
    builder.show(); 
    } 
    private void galleryIntent() 
    { 
    Intent intent = new Intent(); 
    intent.setType("image/*video/*"); 
    intent.setAction(Intent.ACTION_GET_CONTENT); 
    startActivityForResult(Intent.createChooser(intent, "Select 
File"),SELECT_FILE); 
} 
    private void cameraIntent() 
    { 
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
    startActivityForResult(intent, REQUEST_CAMERA); 
} 

    @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent 
data) 
    { 
    super.onActivityResult(requestCode, resultCode, data); 
    if (resultCode == Activity.RESULT_OK) { 
     if (requestCode == SELECT_FILE) 
      onSelectFromGalleryResult(data); 
     else if (requestCode == REQUEST_CAMERA) 
      onCaptureImageResult(data); 
    } 
    } 
    private void onCaptureImageResult(Intent data) { 
    Bitmap thumbnail = (Bitmap) data.getExtras().get("data"); 
    ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
    thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes); 
    File destination = new 
    File(Environment.getExternalStorageDirectory(),System.currentTimeMillis() 
    + ".jpg"); 
    FileOutputStream fo; 
    try { 
     destination.createNewFile(); 
     fo = new FileOutputStream(destination); 
     fo.write(bytes.toByteArray()); 
     fo.close(); 
    } 
    catch (FileNotFoundException e) 
    { 
     e.printStackTrace(); 
     } 
     catch (IOException e) 
     { 
     e.printStackTrace(); 
     } 

    ivImage.setImageBitmap(thumbnail); 
    } 

    @SuppressWarnings("deprecation") 
    private void onSelectFromGalleryResult(Intent data) { 
    Bitmap bm=null; 
    if (data != null) { 
     try { 
      bm = 
      MediaStore.Images.Media.getBitmap 
      (getApplicationContext().getContentResolver(), data.getData()); 
     } 
      catch (IOException e) { 
      e.printStackTrace(); 
     } 
     } 

     ivImage.setImageBitmap(bm); 
     } 

    @Override 
    public void onLocationChanged(Location location) { 
    TextView locationTv = (TextView) findViewById(R.id.latlongLocation); 
    double latitude = location.getLatitude(); 
    double longitude = location.getLongitude(); 
    LatLng latLng = new LatLng(latitude, longitude); 
    googleMap.addMarker(new MarkerOptions().position(latLng)); 
    googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 
    googleMap.animateCamera(CameraUpdateFactory.zoomTo(15)); 
    locationTv.setText("Latitude:" + latitude + ", Longitude:" + longitude); 


    } 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) 
    { 

    } 

    @Override 
    public void onProviderEnabled(String provider) { 

    } 

    @Override 
    public void onProviderDisabled(String provider) { 

    } 
} 

這是一個活動課我的Android項目和本次活動依賴於其他實用工具類....這是下面提到:logcat的文件顯示錯誤

聲明Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();顯示「ImageView的」錯誤及本聲明boolean result=userChosenTask.checkPermission(Crime.this);顯示了「的checkPermission」錯誤......但我不明白爲什麼...因爲在的checkPermission公用事業類已經被定義....

package com.example.ishan.complainbox; 

/** 
* Created by ishan on 11/04/2017. 
*/ 
import android.os.Build; 
import android.content.Context; 
import android.support.v4.app.ActivityCompat; 
import android.support.v4.content.ContextCompat; 
import android.support.v7.app.AlertDialog; 
import android.Manifest; 
import android.content.pm.PackageManager; 
import android.content.DialogInterface; 
public class Utility { 
public static final int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 123; 

@TargetApi(Build.VERSION_CODES.JELLY_BEAN) 
public static boolean checkPermission(final Context context) 
{ 
int currentAPIVersion = Build.VERSION.SDK_INT; 
if(currentAPIVersion>=android.os.Build.VERSION_CODES.M) 
    { 
    if (ContextCompat.checkSelfPermission(context, 
Manifest.permission.READ_EXTERNAL_STORAGE) != 
    PackageManager.PERMISSION_GRANTED) 
    { 
     if 
(ActivityCompat.shouldShowRequestPermissionRationale((MainActivity) context, 
Manifest.permission.READ_EXTERNAL_STORAGE)) 
     { 
      AlertDialog.Builder alertBuilder = new 
AlertDialog.Builder(context); 
      alertBuilder.setCancelable(true); 
      alertBuilder.setTitle("Permission necessary"); 
      alertBuilder.setMessage("External storage permission is 
necessary"); 
      alertBuilder.setPositiveButton(android.R.string.yes, new 
DialogInterface.OnClickListener() { 
       @TargetApi(Build.VERSION_CODES.JELLY_BEAN) 
       public void onClick(DialogInterface dialog, int which) { 
        ActivityCompat.requestPermissions((MainActivity) 
context, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 
    MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE); 
       } 
      }); 
      AlertDialog alert = alertBuilder.create(); 
      alert.show(); 

     } else 
      { 
      ActivityCompat.requestPermissions((MainActivity) context, 
    new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 
    MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE); 
     } 
     return false; 
    } else { 
     return true; 
    } 
    } else 
    { 
    return true; 
} 
} 
} 
+0

您能告訴我們錯誤是什麼嗎?否則,真的不可能幫助你。你不會去看醫生,也不會告訴她你的症狀是什麼,對嗎? –

+0

我已經提到了兩個代碼塊之間的錯誤 –

+0

您已經提到了錯誤的位置,但沒有提到錯誤消息的內容。 –

回答

0

在你的清單。 XML,您需要包含權限:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />