save image in Database

 handled this by saving the image to the ContentProvider
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI and then
saving the created URI to the database along with my item.  An example
of how to do this can be found here:

  1. /*
  2.  * Copyright (C) 2008 Google Inc.
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  * http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */
  16.  package com.google.android.photostream;
  17.  import android.app.Activity;
  18.  import android.content.Context;
  19.  import android.content.Intent;
  20.  import android.content.ActivityNotFoundException;
  21.  import android.os.Bundle;
  22.  import android.widget.TextView;
  23.  import android.widget.ImageView;
  24.  import android.widget.ViewAnimator;
  25.  import android.widget.LinearLayout;
  26.  import android.widget.Toast;
  27.  import android.graphics.Bitmap;
  28.  import android.graphics.BitmapFactory;
  29.  import android.graphics.drawable.Drawable;
  30.  import android.graphics.drawable.BitmapDrawable;
  31.  import android.view.View;
  32.  import android.view.ViewGroup;
  33.  import android.view.ViewTreeObserver;
  34.  import android.view.Menu;
  35.  import android.view.MenuItem;
  36.  import android.view.animation.AnimationUtils;
  37.  import android.net.Uri;
  38.  import java.io.File;
  39.  import java.io.IOException;
  40.  import java.io.OutputStream;
  41.  import java.io.InputStream;
  42.  import java.io.FileNotFoundException;
  43.  /**
  44.  * Activity that displays a photo along with its title and the date at which it was taken.
  45.  * This activity also lets the user set the photo as the wallpaper.
  46.  */
  47.  public class ViewPhotoActivity extends Activity implements View.OnClickListener,
  48.  ViewTreeObserver.OnGlobalLayoutListener {
  49.  static final String ACTION = "com.google.android.photostream.FLICKR_PHOTO";
  50.  private static final String RADAR_ACTION = "com.google.android.radar.SHOW_RADAR";
  51.  private static final String RADAR_EXTRA_LATITUDE = "latitude";
  52.  private static final String RADAR_EXTRA_LONGITUDE = "longitude";
  53.  private static final String EXTRA_PHOTO = "com.google.android.photostream.photo";
  54.  private static final String WALLPAPER_FILE_NAME = "wallpaper";
  55.  private static final int REQUEST_CROP_IMAGE = 42;
  56.  private Flickr.Photo mPhoto;
  57.  private ViewAnimator mSwitcher;
  58.  private ImageView mPhotoView;
  59.  private ViewGroup mContainer;
  60.  private UserTask<?, ?, ?> mTask;
  61.  private TextView mPhotoTitle;
  62.  private TextView mPhotoDate;
  63.  @Override
  64.  protected void onCreate(Bundle savedInstanceState) {
  65.  super.onCreate(savedInstanceState);
  66.  mPhoto = getPhoto();
  67.  setContentView(R.layout.screen_photo);
  68.  setupViews();
  69.  }
  70.  /**
  71.  * Starts the ViewPhotoActivity for the specified photo.
  72.  *
  73.  * @param context The application's environment.
  74.  * @param photo The photo to display and optionally set as a wallpaper.
  75.  */
  76.  static void show(Context context, Flickr.Photo photo) {
  77.  final Intent intent = new Intent(context, ViewPhotoActivity.class);
  78.  intent.putExtra(EXTRA_PHOTO, photo);
  79.  context.startActivity(intent);
  80.  }
  81.  @Override
  82.  protected void onDestroy() {
  83.  super.onDestroy();
  84.  if (mTask != null && mTask.getStatus() != UserTask.Status.RUNNING) {
  85.  mTask.cancel(true);
  86.  }
  87.  }
  88.  private void setupViews() {
  89.  mContainer = (ViewGroup) findViewById(R.id.container_photo);
  90.  mSwitcher = (ViewAnimator) findViewById(R.id.switcher_menu);
  91.  mPhotoView = (ImageView) findViewById(R.id.image_photo);
  92.  mPhotoTitle = (TextView) findViewById(R.id.caption_title);
  93.  mPhotoDate = (TextView) findViewById(R.id.caption_date);
  94.  findViewById(R.id.menu_back).setOnClickListener(this);
  95.  findViewById(R.id.menu_set).setOnClickListener(this);
  96.  mPhotoTitle.setText(mPhoto.getTitle());
  97.  mPhotoDate.setText(mPhoto.getDate());
  98.  mContainer.setVisibility(View.INVISIBLE);
  99.  // Sets up a view tree observer. The photo will be scaled using the size
  100.  // of one of our views so we must wait for the first layout pass to be
  101.  // done to make sure we have the correct size.
  102.  mContainer.getViewTreeObserver().addOnGlobalLayoutListener(this);
  103.  }
  104.  /**
  105.  * Loads the photo after the first layout. The photo is scaled using the
  106.  * dimension of the ImageView that will ultimately contain the photo's
  107.  * bitmap. We make sure that the ImageView is laid out at least once to
  108.  * get its correct size.
  109.  */
  110.  public void onGlobalLayout() {
  111.  mContainer.getViewTreeObserver().removeGlobalOnLayoutListener(this);
  112.  loadPhoto(mPhotoView.getMeasuredWidth(), mPhotoView.getMeasuredHeight());
  113.  }
  114.  /**
  115.  * Loads the photo either from the last known instance or from the network.
  116.  * Loading it from the last known instance allows for fast display rotation
  117.  * without having to download the photo from the network again.
  118.  *
  119.  * @param width The desired maximum width of the photo.
  120.  * @param height The desired maximum height of the photo.
  121.  */
  122.  private void loadPhoto(int width, int height) {
  123.  final Object data = getLastNonConfigurationInstance();
  124.  if (data == null) {
  125.  mTask = new LoadPhotoTask().execute(mPhoto, width, height);
  126.  } else {
  127.  mPhotoView.setImageBitmap((Bitmap) data);
  128.  mSwitcher.showNext();
  129.  }
  130.  }
  131.  /**
  132.  * Loads the {@link com.google.android.photostream.Flickr.Photo} to display
  133.  * from the intent used to start the activity.
  134.  *
  135.  * @return The photo to display, or null if the photo cannot be found.
  136.  */
  137.  public Flickr.Photo getPhoto() {
  138.  final Intent intent = getIntent();
  139.  final Bundle extras = intent.getExtras();
  140.  Flickr.Photo photo = null;
  141.  if (extras != null) {
  142.  photo = extras.getParcelable(EXTRA_PHOTO);
  143.  }
  144.  return photo;
  145.  }
  146.  @Override
  147.  public boolean onCreateOptionsMenu(Menu menu) {
  148.  getMenuInflater().inflate(R.menu.view_photo, menu);
  149.  return super.onCreateOptionsMenu(menu);
  150.  }
  151.  @Override
  152.  public boolean onMenuItemSelected(int featureId, MenuItem item) {
  153.  switch (item.getItemId()) {
  154.  case R.id.menu_item_radar:
  155.  onShowRadar();
  156.  break;
  157.  }
  158.  return super.onMenuItemSelected(featureId, item);
  159.  }
  160.  private void onShowRadar() {
  161.  new ShowRadarTask().execute(mPhoto);
  162.  }
  163.  public void onClick(View v) {
  164.  switch (v.getId()) {
  165.  case R.id.menu_back:
  166.  onBack();
  167.  break;
  168.  case R.id.menu_set:
  169.  onSet();
  170.  break;
  171.  }
  172.  }
  173.  private void onSet() {
  174.  mTask = new CropWallpaperTask().execute(mPhoto);
  175.  }
  176.  private void onBack() {
  177.  finish();
  178.  }
  179.  /**
  180.  * If we successfully loaded a photo, send it to our future self to allow
  181.  * for fast display rotation. By doing so, we avoid reloading the photo
  182.  * from the network when the activity is taken down and recreated upon
  183.  * display rotation.
  184.  *
  185.  * @return The Bitmap displayed in the ImageView, or null if the photo
  186.  * wasn't loaded.
  187.  */
  188.  @Override
  189.  public Object onRetainNonConfigurationInstance() {
  190.  final Drawable d = mPhotoView.getDrawable();
  191.  return d != null ? ((BitmapDrawable) d).getBitmap() : null;
  192.  }
  193.  @Override
  194.  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  195.  // Spawns a new task to set the wallpaper in a background thread when/if
  196.  // we receive a successful result from the image cropper.
  197.  if (requestCode == REQUEST_CROP_IMAGE) {
  198.  if (resultCode == RESULT_OK) {
  199.  mTask = new SetWallpaperTask().execute();
  200.  } else {
  201.  cleanupWallpaper();
  202.  showWallpaperError();
  203.  }
  204.  }
  205.  }
  206.  private void showWallpaperError() {
  207.  Toast.makeText(ViewPhotoActivity.this, R.string.error_cannot_save_file,
  208.  Toast.LENGTH_SHORT).show();
  209.  }
  210.  private void showWallpaperSuccess() {
  211.  Toast.makeText(ViewPhotoActivity.this, R.string.success_wallpaper_set,
  212.  Toast.LENGTH_SHORT).show();
  213.  }
  214.  private void cleanupWallpaper() {
  215.  deleteFile(WALLPAPER_FILE_NAME);
  216.  mSwitcher.showNext();
  217.  }
  218.  /**
  219.  * Background task to load the photo from Flickr. The task loads the bitmap,
  220.  * then scale it to the appropriate dimension. The task ends by readjusting
  221.  * the activity's layout so that everything aligns correctly.
  222.  */
  223.  private class LoadPhotoTask extends UserTask<Object, Void, Bitmap> {
  224.  public Bitmap doInBackground(Object... params) {
  225.  Bitmap bitmap = ((Flickr.Photo) params[0]).loadPhotoBitmap(Flickr.PhotoSize.MEDIUM);
  226.  if (bitmap == null) {
  227.  bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.not_found);
  228.  }
  229.  final int width = (Integer) params[1];
  230.  final int height = (Integer) params[2];
  231.  final Bitmap framed = ImageUtilities.scaleAndFrame(bitmap, width, height);
  232.  bitmap.recycle();
  233.  return framed;
  234.  }
  235.  @Override
  236.  public void onPostExecute(Bitmap result) {
  237.  mPhotoView.setImageBitmap(result);
  238.  // Find by how many pixels the title and date must be shifted on the
  239.  // horizontal axis to be left aligned with the photo
  240.  final int offsetX = (mPhotoView.getMeasuredWidth() - result.getWidth()) / 2;
  241.  // Forces the ImageView to have the same size as its embedded bitmap
  242.  // This will remove the empty space between the title/date pair and
  243.  // the photo itself
  244.  LinearLayout.LayoutParams params;
  245.  params = (LinearLayout.LayoutParams) mPhotoView.getLayoutParams();
  246.  params.height = result.getHeight();
  247.  params.weight = 0.0f;
  248.  mPhotoView.setLayoutParams(params);
  249.  params = (LinearLayout.LayoutParams) mPhotoTitle.getLayoutParams();
  250.  params.leftMargin = offsetX;
  251.  mPhotoTitle.setLayoutParams(params);
  252.  params = (LinearLayout.LayoutParams) mPhotoDate.getLayoutParams();
  253.  params.leftMargin = offsetX;
  254.  mPhotoDate.setLayoutParams(params);
  255.  mSwitcher.showNext();
  256.  mContainer.startAnimation(AnimationUtils.loadAnimation(ViewPhotoActivity.this,
  257.  R.anim.fade_in));
  258.  mContainer.setVisibility(View.VISIBLE);
  259.  mTask = null
  260.  }
  261.  }
  262.  /**
  263.  * Background task to crop a large version of the image. The cropped result will
  264.  * be set as a wallpaper. The tasks sarts by showing the progress bar, then
  265.  * downloads the large version of hthe photo into a temporary file and ends by
  266.  * sending an intent to the Camera application to crop the image.
  267.  */
  268.  private class CropWallpaperTask extends UserTask<Flickr.Photo, Void, Boolean> {
  269.  private File mFile;
  270.  @Override
  271.  public void onPreExecute() {
  272.  mFile = getFileStreamPath(WALLPAPER_FILE_NAME);
  273.  mSwitcher.showNext();
  274.  }
  275.  public Boolean doInBackground(Flickr.Photo... params) {
  276.  boolean success = false;
  277.  OutputStream out = null;
  278.  try {
  279.  out = openFileOutput(mFile.getName(), MODE_WORLD_READABLE | MODE_WORLD_WRITEABLE);
  280.  Flickr.get().downloadPhoto(params[0], Flickr.PhotoSize.LARGE, out);
  281.  success = true;
  282.  } catch (FileNotFoundException e) {
  283.  android.util.Log.e(Flickr.LOG_TAG, "Could not download photo", e);
  284.  success = false;
  285.  } catch (IOException e) {
  286.  android.util.Log.e(Flickr.LOG_TAG, "Could not download photo", e);
  287.  success = false;
  288.  } finally {
  289.  if (out != null) {
  290.  try {
  291.  out.close();
  292.  } catch (IOException e) {
  293.  success = false;
  294.  }
  295.  }
  296.  }
  297.  return success;
  298.  }
  299.  @Override
  300.  public void onPostExecute(Boolean result) {
  301.  if (!result) {
  302.  cleanupWallpaper();
  303.  showWallpaperError();
  304.  } else {
  305.  final int width = getWallpaperDesiredMinimumWidth();
  306.  final int height = getWallpaperDesiredMinimumHeight();
  307.  final Intent intent = new Intent("com.android.camera.action.CROP");
  308.  intent.setClassName("com.android.camera""com.android.camera.CropImage");
  309.  intent.setData(Uri.fromFile(mFile));
  310.  intent.putExtra("outputX", width);
  311.  intent.putExtra("outputY", height);
  312.  intent.putExtra("aspectX", width);
  313.  intent.putExtra("aspectY", height);
  314.  intent.putExtra("scale"true);
  315.  intent.putExtra("noFaceDetection"true);
  316.  intent.putExtra("output", Uri.parse("file:/" + mFile.getAbsolutePath()));
  317.  startActivityForResult(intent, REQUEST_CROP_IMAGE);
  318.  }
  319.  mTask = null;
  320.  }
  321.  }
  322.  /**
  323.  * Background task to set the cropped image as the wallpaper. The task simply
  324.  * open the temporary file and sets it as the new wallpaper. The task ends by
  325.  * deleting the temporary file and display a message to the user.
  326.  */
  327.  private class SetWallpaperTask extends UserTask<Void, Void, Boolean> {
  328.  public Boolean doInBackground(Void... params) {
  329.  boolean success = false;
  330.  InputStream in = null;
  331.  try {
  332.  in = openFileInput(WALLPAPER_FILE_NAME);
  333.  setWallpaper(in);
  334.  success = true;
  335.  } catch (IOException e) {
  336.  success = false;
  337.  } finally {
  338.  if (in != null) {
  339.  try {
  340.  in.close();
  341.  } catch (IOException e) {
  342.  success = false;
  343.  }
  344.  }
  345.  }
  346.  return success;
  347.  }
  348.  @Override
  349.  public void onPostExecute(Boolean result) {
  350.  cleanupWallpaper();
  351.  if (!result) {
  352.  showWallpaperError();
  353.  } else {
  354.  showWallpaperSuccess();
  355.  }
  356.  mTask = null;
  357.  }
  358.  }
  359.  private class ShowRadarTask extends UserTask<Flickr.Photo, Void, Flickr.Location> {
  360.  public Flickr.Location doInBackground(Flickr.Photo... params) {
  361.  return Flickr.get().getLocation(params[0]);
  362.  }
  363.  @Override
  364.  public void onPostExecute(Flickr.Location location) {
  365.  if (location != null) {
  366.  final Intent intent = new Intent(RADAR_ACTION);
  367.  intent.putExtra(RADAR_EXTRA_LATITUDE, location.getLatitude());
  368.  intent.putExtra(RADAR_EXTRA_LONGITUDE, location.getLongitude());
  369.  try {
  370.  startActivity(intent);
  371.  } catch (ActivityNotFoundException e) {
  372.  Toast.makeText(ViewPhotoActivity.this, R.string.error_cannot_find_radar,
  373.  Toast.LENGTH_SHORT).show();
  374.  }
  375.  } else {
  376.  Toast.makeText(ViewPhotoActivity.this, R.string.error_cannot_find_location,
  377.  Toast.LENGTH_SHORT).show();
  378.  }
  379.  }
  380.  }
  381.  }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将图片和UI文本框中的内容保存到已经写好的`database.db`数据库中,你可以按照以下步骤进行操作: 1. 确保你已经创建了一个可用的数据库连接对象,并且已经正确设置了数据库的路径和名称。你可以使用之前提到的代码段中的`m_database`对象。 2. 在保存按钮的点击事件处理函数中,获取图片的保存路径。你已经使用了`QFileDialog::getSaveFileName()`方法来获取保存路径,并将路径保存在`fileName`变量中。 3. 检查保存路径是否为空。如果路径不为空,继续执行下一步;否则,显示一个错误消息框,提示用户选择有效的保存路径。 4. 如果路径不为空,首先保存图片。你可以使用`imag->save(fileName)`将图片保存到指定路径。如果保存成功,显示一个成功的消息框;否则,显示一个失败的消息框。 5. 接下来,将UI文本框中的内容保存到数据库中。根据你提供的信息,你需要获取`name`、`num`和`phone`文本框的内容,并将其存储到数据库中。可以使用以下代码示例来执行此操作: ```cpp QString name = ui->nameLineEdit->text(); QString num = ui->numLineEdit->text(); QString phone = ui->phoneLineEdit->text(); QSqlQuery query; query.prepare("INSERT INTO your_table_name (name, num, phone) VALUES (:name, :num, :phone)"); query.bindValue(":name", name); query.bindValue(":num", num); query.bindValue(":phone", phone); if (query.exec()) { QMessageBox::information(this, tr("Success"), tr("Data saved successfully!")); } else { QMessageBox::information(this, tr("Failed"), tr("Failed to save data!")); } ``` 请确保替换代码中的`your_table_name`为你实际的表名,并根据你的UI设计替换文本框的对象名称。 这样,当用户点击保存按钮时,图片和UI文本框中的内容将保存在已经写好的`database.db`数据库中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值