2016-03-19 11 views
0

Good Dayandroid: Ausgabe in der Anzeige aufgenommene Bild in gridview

Ich versuche, ein Bild mit der Kamera zu erfassen, wenn ich die Taste drücken, dann speichern Sie es in SQLLite und es dann in gridview angezeigt .. aber nach Ich erstelle das Bild, das nicht in der Rasteransicht angezeigt wird:/Es wird kein Fehler in logcat angezeigt, außer nichts in der Gridview angezeigt. Ich weiß nicht, ob das Problem in der Kamera oder in der Gridview kann jemand helfen?

hier ist mein Code

public class CameraFragment extends Fragment { 

    public static final int MEDIA_TYPE_IMAGE = 1; 
    private static final int CAMERA_CAPTURE_IMAGE_REQUEST_CODE = 100; 
    private static final String IMAGE_DIRECTORY_NAME = "Myfiles";// directory name to store captured images 
    private static String mCurrentPhotoPath; // file path to the last image captured 
    View view; 
    private Uri fileUri;// file url to store image 
    private GridView myGridView; 
    private ImageListAdapter adapter; 
    private List<Images> myLists;// image list 
    Images images; 
    DatabaseAdapter DBadapter; 


    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 

     if (view != null) { 
      ViewGroup parent = (ViewGroup) view.getParent(); 
      if (parent != null) 
       parent.removeView(view); 
     } 
     try { 
      // Inflate the layout for this fragment 
      view = inflater 
        .inflate(R.layout.fragment_camera, container, false); 


     } catch (InflateException e) { 
      /* map is already there, just return view as it is */ 
      e.printStackTrace(); 
     } 
     if (view != null) 
      init(); 
     return view; 

    } 

    // initialize all the variables 
    private void init() { 
     DatabaseAdapter DBadapter =new DatabaseAdapter(getActivity()); 
     DBadapter.open(); 
     myLists = new ArrayList<Images>(); 
     myLists=DBadapter.getImagesList(); 
     adapter = new ImageListAdapter(getActivity(), R.layout.img_list_view, myLists); 
     // imageView = (ImageView) view.findViewById(R.id.imageListView); 
     Button myButton = (Button) view.findViewById(R.id.camerabutton); 
     myButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
       fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);// create a file to save the image 
       intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name 
       startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE); // start the image capture Intent 
      } 
     }); 
     myGridView = (GridView) view.findViewById(R.id.gridView); 
     myGridView.setAdapter(adapter); 
/** 
    * Create a file Uri for saving an image or video 
    */ 
    public Uri getOutputMediaFileUri(int type) { 

     return Uri.fromFile(getOutputMediaFile(type)); 
    } 
    /** 
    * returning image/
    */ 
    private static File getOutputMediaFile(int type) { 

     // External sdcard location 
     File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), IMAGE_DIRECTORY_NAME); 

     // Create the storage directory if it does not exist 
     if (!mediaStorageDir.exists()) { 
      mediaStorageDir.mkdirs(); 
      Log.d(IMAGE_DIRECTORY_NAME, "Oops! Failed create " 
        + IMAGE_DIRECTORY_NAME + " directory"); 
      //return null; 

     } 

     // Create a media file name 
     String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", 
       Locale.getDefault()).format(new Date()); 
     File mediaFile; 
     if (type == MEDIA_TYPE_IMAGE) { 
      mediaFile = new File(mediaStorageDir.getPath() + File.separator 
        + "IMG_" + timeStamp + ".jpg"); 
     } else { 
      return null; 
     } 


     return mediaFile; 


    } 

    @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     switch (requestCode) { 
      case 1: 
       if (resultCode == Activity.RESULT_OK) { 
        Uri selectedImage = data.getData(); 
        String[] filePathColumn = {MediaStore.Images.Media.DATA}; 
        Cursor cursor = getActivity().getContentResolver().query(selectedImage, filePathColumn, null, null, null); 
        cursor.moveToFirst(); 

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
        //file path of captured image 
        String filePath = cursor.getString(columnIndex); 
        //file path of captured image 
        File f = new File(filePath); 
        String filename = f.getName(); 
        cursor.close(); 
        DBadapter.insertImage(images); //insaert image to database 
        myLists=DBadapter.getImagesList(); 
        adapter = new ImageListAdapter(getActivity(), R.layout.img_list_view, myLists); 
        myGridView.setAdapter(adapter); 
        myGridView.invalidateViews(); 
    break; 
} 

      case 2: 
     if (resultCode == getActivity().RESULT_CANCELED) { 
      // user cancelled Image capture 
      Toast.makeText(getActivity(), "User cancelled image capture", Toast.LENGTH_SHORT) 
        .show(); 
     } 
     else { 
      // failed to capture image 
      Toast.makeText(getActivity(), "Sorry! Failed to capture image", Toast.LENGTH_SHORT) 
        .show(); 
      } break; 
    }} 
} 

hier ist xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.labon.invoicemanger.CameraFragment"> 

    <!-- TODO: Update blank fragment layout --> <RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="My Invoice" 
     android:layout_gravity="center" 
     android:layout_margin="20dp" 
     android:id="@+id/textView16" 
     android:textColor="@color/colorPrimary" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" /> 
    <GridView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/gridView" 
     android:layout_above="@+id/camerabutton" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:numColumns="auto_fit"/> 

    <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Add New Invoice" 
     android:textSize="18dp" 
     android:textStyle="bold" 
     android:textColor="@android:color/white" 
     android:id="@+id/camerabutton" 
     android:drawableRight="@drawable/screenshot" 
     android:background="@color/colorAccent" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" /> 
    <!--android:drawableRight="@drawable/screen_shot"--> </RelativeLayout> </FrameLayout> 

Antwort

0

In startActivityForResult Sie CAMERA_CAPTURE_IMAGE_REQUEST_CODE als Anforderungs-Code verwenden, aber in onActivityResult Sie haben keinen Fall für CAMERA_CAPTURE_IMAGE_REQUEST_CODE (100). Sie benötigen einen Fall für CAMERA_CAPTURE_IMAGE_REQUEST_CODE in Ihrem Switch Fälle hinzufügen oder ändern:

case 1 

mit

case CAMERA_CAPTURE_IMAGE_REQUEST_CODE 

Update:

try this:

static final int REQUEST_IMAGE_CAPTURE = 1; 

private void dispatchTakePictureIntent() { 
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) { 
     startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE); 
    } 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) { 
     Bundle extras = data.getExtras(); 
     Bitmap imageBitmap = (Bitmap) extras.get("data"); 
     mImageView.setImageBitmap(imageBitmap); 
    } 
} 

für mehr Informationen schauen here

+0

es Fall Ausnahme .RuntimeException: Failure Ergebnisweitergabeweg result, nachdem ich die Schalter Fällen ändern @Neel – labon

+0

ich seine denken, weil Sie verwenden intent.putExtra (MediaStore.EXTRA_OUTPUT, fileURI) und der Versuch, die uri von der Absicht zu bekommen . Sie müssen fileUri anstelle von data.getData() verwenden. – OShiffer

+0

Wie ist es @OShiffer – labon

Verwandte Themen