2016-08-09 38 views
0

Ich arbeite auf der Kameraseite. Android: Foto machen und dieses Bild im internen/externen Speicher mit spezifischem Namen des Gerätes programmgesteuert speichern? Wenn ich Schaltfläche Speichern mein Code klicken Sie darauf nicht in speicherung bitte helfen Sie mir hier:Android: Foto machen und dieses Bild im internen/externen Speicher mit spezifischem Namen programmgesteuert speichern?

Hauptaktivität ist

public class MainActivity extends AppCompatActivity { 
    Button cButton, cSavaBtn; 
    ImageView ivCamera; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     cButton = (Button) findViewById(R.id.button); 
     cSavaBtn = (Button) findViewById(R.id.buttonSave); 
     ivCamera = (ImageView) findViewById(R.id.imageView); 
     cButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Toast.makeText(getApplication(), "Camera Clicked", Toast.LENGTH_LONG); 
       Intent cIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
       startActivityForResult(cIntent, 0); 

      } 
     }); 

     cSavaBtn.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Toast.makeText(getApplication(), "Save btn Clicked", Toast.LENGTH_LONG); 
       AfterSaveClick(); 
      } 
     }); 

    } 
    protected void onActivityResult(int requestCode, int resultCode, Intent data){ 
     super.onActivityResult(requestCode,resultCode,data); 
     Bitmap bm = (Bitmap) data.getExtras().get("data"); 
     ivCamera.setImageBitmap(bm); 

    } 
    public void AfterSaveClick() { 

     ivCamera.buildDrawingCache(); 
     Bitmap bm = ivCamera.getDrawingCache(); 


     OutputStream fOut = null; 

     try { 
      Toast.makeText(this, "Correct now", 
        Toast.LENGTH_SHORT).show(); 
      File root = new File(Environment.getExternalStorageDirectory() 
        + File.separator + "sharathFolder" + File.separator); 
      root.mkdirs(); 
      File sdImageMainDirectory = new File(root, "myPicName.jpg"); 
      fOut = new FileOutputStream(sdImageMainDirectory); 

      Toast.makeText(this, "Correct end now", 
        Toast.LENGTH_SHORT).show(); 

     } catch (Exception e) { 
      Toast.makeText(this, "Error occured. Please try again later.", 
        Toast.LENGTH_SHORT).show(); 
     } 

     try { 
      bm.compress(Bitmap.CompressFormat.PNG, 100, fOut); 
      fOut.flush(); 
      fOut.close(); 
     } catch (Exception e) { 
      Toast.makeText(this, "Exception.", 
        Toast.LENGTH_SHORT).show(); 
     } 
    } 
} 

XML-Datei ist:

<RelativeLayout 
    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" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    tools:context=".MainActivity"> 
    <TextView 
     android:text="Camera API" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/textview" 
     android:textSize="35dp" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" /> 
    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/imageView" 
     android:src="@mipmap/ic_launcher" 
     android:layout_below="@+id/textview" 
     android:layout_centerHorizontal="true" /> 
    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="camera" 
     android:id="@+id/button" 
     android:layout_below="@+id/imageView" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="86dp" /> 
    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="SAVE" 
     android:id="@+id/buttonSave" 
     android:layout_below="@+id/button" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="86dp" /> 
</RelativeLayout> 

Meine Manifest-Datei:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.kgsharat" 
    android:versionCode="1" 
    android:versionName="1.0"> 

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

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <activity android:name=".MainActivity"> 
      android:screenOrientation="portrait" 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 
</manifest> 
+0

Stehen Sie vor einem Fehler? – RajSharma

+0

Nein, ich sehe keinen Fehler, aber es erstellt keinen Ordner im internen/externen Speicher und speichert das angeklickte Bild nicht. –

+0

Mit welcher Geräte-/Betriebssystemversion haben Sie das getestet? –

Antwort

0

Nach einem Klick Schaltfläche Speichern, haben Sie diese Methode aufrufen. Es funktioniert gut für mich

public void AfterSaveClick() { 

     ivCamera.buildDrawingCache(); 
     Bitmap bm = ivCamera.getDrawingCache(); 


     OutputStream fOut = null; 

     try { 
      Toast.makeText(this, "Correct now", 
        Toast.LENGTH_SHORT).show(); 
      File root = new File(Environment.getExternalStorageDirectory() 
        + File.separator + "sharathFolder" + File.separator); 
      root.mkdirs(); 
      File sdImageMainDirectory = new File(root, "myPicName.jpg"); 
      fOut = new FileOutputStream(sdImageMainDirectory); 

      Toast.makeText(this, "Correct end now", 
        Toast.LENGTH_SHORT).show(); 

     } catch (Exception e) { 
      Toast.makeText(this, "Error occured. Please try again later.", 
        Toast.LENGTH_SHORT).show(); 
     } 

     try { 
      bm.compress(Bitmap.CompressFormat.PNG, 100, fOut); 
      fOut.flush(); 
      fOut.close(); 
     } catch (Exception e) { 
      Toast.makeText(this, "Exception.", 
        Toast.LENGTH_SHORT).show(); 
     } 
    } 
0
public String storeSdCard(Bitmap bitmap) { 
    FileOutputStream fos = null; 
    String status = Environment.getExternalStorageState(); 
    if (status.equals(Environment.MEDIA_MOUNTED)) { 
     File root = new File(Environment.getExternalStorageDirectory(), 
       "FolderName"); 
     if (!root.exists()) { 
      root.mkdirs(); 
     } 
     String filename = "" + System.currentTimeMillis(); 
     File file = new File(root, filename + ".jpg"); 

     try { 
      fos = new FileOutputStream(file); 
      if (fos != null) { 
       bitmap.compress(Bitmap.CompressFormat.JPEG, 70, fos); 
       fos.close(); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     Uri muri = Uri.fromFile(file); 
     String path = muri.getPath(); 

     return path; 
    } 
    return null; 
} 

Nachdem Sie die Bitmap bekamen, mit dieser Methode können Sie das Bild im internen Speicher speichern. Und geben Sie Berechtigungen in Manifest-Datei

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

Danke .. @rams aber in meinem Code nur seine Arbeit gut, aber nicht sofort gespeichert ... es wird einige Zeit dauern zu erstellen ... warum ?? Hast du eine Idee .. ?? –

+0

Willkommen .. In meiner Anwendung funktioniert es gut. Bitmap sofort gespeichert. – rams